HONEYCOMB-307 - Prevent re-mapping of already mapped names 67/4067/3
authorJan Srnicek <[email protected]>
Fri, 2 Dec 2016 08:37:22 +0000 (09:37 +0100)
committerMarek Gradzki <[email protected]>
Fri, 2 Dec 2016 13:47:21 +0000 (13:47 +0000)
Prevent invoking addName while initializing data for names
that are already in mappings

Change-Id: I56063fb28cfb215bbdb1ed745aaa44544ffa77a0
Signed-off-by: Jan Srnicek <[email protected]>
routing/routing-impl/src/main/java/io/fd/hc2vpp/routing/write/RoutingProtocolCustomizer.java
routing/routing-impl/src/test/java/io/fd/hc2vpp/routing/helpers/RoutingRequestTestHelper.java
routing/routing-impl/src/test/java/io/fd/hc2vpp/routing/write/RoutingProtocolCustomizerTest.java

index 220f076..69b8e50 100644 (file)
@@ -61,9 +61,12 @@ public class RoutingProtocolCustomizer
                 // while using ip_add_del_table
                 routingProtocolContext.addName(tableId, newProtocolName, mappingContext);
             } else {
-                throw new IllegalStateException(String.format(
-                        "An attempt to assign protocol %s to table id %s. Table id already assigned to protocol %s",
-                        newProtocolName, tableId, routingProtocolContext.getName(tableId, mappingContext)));
+                // prevent to fail while restoring data(trying to remap already mapped name)
+                if (!newProtocolName.equals(routingProtocolContext.getName(tableId, mappingContext))) {
+                    throw new IllegalStateException(String.format(
+                            "An attempt to assign protocol %s to table id %s. Table id already assigned to protocol %s",
+                            newProtocolName, tableId, routingProtocolContext.getName(tableId, mappingContext)));
+                }
             }
         }
     }
index bcb8e68..a13364d 100644 (file)
@@ -39,6 +39,7 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.routing.rev
 public interface RoutingRequestTestHelper extends ByteDataTranslator, FutureProducer, RouteMapper {
 
     String ROUTE_PROTOCOL_NAME = "tst-protocol";
+    String ROUTE_PROTOCOL_NAME_2 = "tst-protocol-2";
     String ROUTE_NAME = "tst-route";
     String STATIC_ROUTE_PATH = "/ietf-routing:routing" +
             "/ietf-routing:routing-instance[ietf-routing:name='" + ROUTE_PROTOCOL_NAME + "']" +
index d734aab..21a2bdc 100644 (file)
@@ -17,6 +17,7 @@
 package io.fd.hc2vpp.routing.write;
 
 import static io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper.ROUTE_PROTOCOL_NAME;
+import static io.fd.hc2vpp.routing.helpers.RoutingRequestTestHelper.ROUTE_PROTOCOL_NAME_2;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -39,6 +40,7 @@ public class RoutingProtocolCustomizerTest extends WriterCustomizerTest {
 
     private InstanceIdentifier<RoutingProtocol> validId;
     private RoutingProtocol validData;
+    private RoutingProtocol validData2;
     private RoutingProtocol invalidData;
     private RoutingProtocolCustomizer customizer;
     private NamingContext routingProtocolContext;
@@ -56,6 +58,16 @@ public class RoutingProtocolCustomizerTest extends WriterCustomizerTest {
                         .build())
                 .build();
 
+        validData2= new RoutingProtocolBuilder()
+                .setName(ROUTE_PROTOCOL_NAME_2)
+                .setType(Static.class)
+                .addAugmentation(RoutingProtocolVppAttr.class, new RoutingProtocolVppAttrBuilder()
+                        .setVppProtocolAttributes(new VppProtocolAttributesBuilder()
+                                .setPrimaryVrf(new VniReference(1L))
+                                .build())
+                        .build())
+                .build();
+
         invalidData = new RoutingProtocolBuilder()
                 .setType(Direct.class)
                 .build();
@@ -74,11 +86,27 @@ public class RoutingProtocolCustomizerTest extends WriterCustomizerTest {
         }
     }
 
+    /**
+     * Should not fail, just ignore re-mapping same name
+     * */
     @Test
-    public void testWriteIsStaticAllreadyExist() throws WriteFailedException {
+    public void testWriteIsStaticSameAllreadyExist() throws WriteFailedException {
         defineMapping(mappingContext, ROUTE_PROTOCOL_NAME, 1, "routing-protocol-context");
         try {
             customizer.writeCurrentAttributes(validId, validData, writeContext);
+        } catch (Exception e) {
+            fail("Test should have passed without throwing exception");
+        }
+    }
+
+    /**
+     * Should fail, because of attempt to map different name to same index
+     * */
+    @Test
+    public void testWriteIsStaticOtherAllreadyExist() throws WriteFailedException {
+        defineMapping(mappingContext, ROUTE_PROTOCOL_NAME, 1, "routing-protocol-context");
+        try {
+            customizer.writeCurrentAttributes(validId, validData2, writeContext);
         } catch (Exception e) {
             assertTrue(e instanceof IllegalStateException);
             return;