Bug 9127: Make IT more robust when receiving packets
[lispflowmapping.git] / integrationtest / src / test / java / org / opendaylight / lispflowmapping / integrationtest / MappingServiceIntegrationTest.java
index 68caed3e6cb27f5a5739737987fdb55558f6bca0..cb5a6830a634757d8f81d78a34ff04701899bb1e 100644 (file)
@@ -173,7 +173,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
 
     public static final String ODL = "org.opendaylight.controller";
     public static final String YANG = "org.opendaylight.yangtools";
-    private static final int MULTI_SITE_SLEEP_TIME = 1;
+    private static final int MULTI_SITE_SLEEP_TIME = 2;
     private static final int MAX_NOTIFICATION_RETRYS = 20;
     private static final MappingAuthkey NULL_AUTH_KEY = new MappingAuthkeyBuilder().setKeyType(0).build();
 
@@ -409,7 +409,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     @Test
     public void testSmr() throws Exception {
         registerQueryRegisterWithSmr();
-        testRepeatedSmr();
+        //testRepeatedSmr();
     }
 
     @Test
@@ -419,15 +419,24 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     }
 
     @Test
-    public void testNegativePrefix_gapIntersection() throws UnknownHostException {
+    public void testNbAndSbNegativePrefix() throws UnknownHostException {
         insertMappings();
         testGapIntersection();
 
         insertMappings();
         testMultipleMappings();
+    }
 
+    @Test
+    public void testExplicitSbNegativePrefixes() {
         // https://bugs.opendaylight.org/show_bug.cgi?id=8679
         testNegativePrefix();
+
+        // https://bugs.opendaylight.org/show_bug.cgi?id=9023
+        testPositiveMappingRemoval();
+
+        // https://bugs.opendaylight.org/show_bug.cgi?id=9037
+        testPositivePrefixOverlappingNegativePrefix();
     }
 
     private void testRepeatedSmr() throws SocketTimeoutException, UnknownHostException {
@@ -518,8 +527,11 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         final List<MapRequest> requests = Lists.newArrayList();
         byte[][] buffers = reader.getBuffers(expectedSmrs);
         for (byte[] buf : buffers) {
-            MapRequest request = MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(buf), inetAddress);
-            requests.add(request);
+            ByteBuffer packet = ByteBuffer.wrap(buf);
+            if (checkType(packet, MessageType.MapRequest)) {
+                MapRequest request = MapRequestSerializer.getInstance().deserialize(packet, inetAddress);
+                requests.add(request);
+            }
         }
         return requests;
     }
@@ -619,6 +631,66 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         assertTrue(MappingRecordUtil.isNegativeMapping(mr));
     }
 
+    private void testPositiveMappingRemoval() {
+        cleanUP();
+
+        insertNBMappings(1L, "192.167.0.0/16", "192.169.0.0/16");
+        insertSBMappings(1L, "192.168.32.0/19");
+
+        MapReply mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.0.1/32"));
+        Eid expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.0.0/19");
+        MappingRecord mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+
+        mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.64.1/32"));
+        expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.64.0/18");
+        mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+
+        mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.128.1/32"));
+        expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.128.0/17");
+        mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+
+        printMapCacheState();
+
+        mapService.removeMapping(MappingOrigin.Southbound, LispAddressUtil.asIpv4PrefixBinaryEid(
+                1L, "192.168.32.0/19"));
+
+        printMapCacheState();
+
+        mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.32.1/32"));
+        expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.0.0/16");
+        mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+
+        printMapCacheState();
+    }
+
+    private void testPositivePrefixOverlappingNegativePrefix() {
+        cleanUP();
+
+        insertNBMappings(1L, "192.167.0.0/16", "192.169.0.0/16");
+
+        MapReply mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.0.1/32"));
+        Eid expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.0.0/16");
+        MappingRecord mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+
+        insertNBMappings(1L, "192.168.1.0/24");
+
+        mapReply = lms.handleMapRequest(newMapRequest(1L, "192.168.0.1/32"));
+        expectedNegativePrefix = LispAddressUtil.asIpv4PrefixBinaryEid(1L, "192.168.0.0/24");
+        mr = mapReply.getMappingRecordItem().get(0).getMappingRecord();
+        assertEquals(expectedNegativePrefix, mr.getEid());
+        assertTrue(MappingRecordUtil.isNegativeMapping(mr));
+    }
+
     private void insertMappings() {
         cleanUP();
         mapService.setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
@@ -631,14 +703,18 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     }
 
     private void insertNBMappings(long iid, String ... prefixes) {
+        LOG.debug("Adding Northbound mappings for prefixes: {}", prefixes);
         final InstanceIdType iiType = new InstanceIdType(iid);
         for (String prefix : prefixes) {
             MappingRecord record = newMappingRecord(prefix, iiType);
             mapService.addMapping(MappingOrigin.Northbound, record.getEid(), null, new MappingData(record));
         }
+        sleepForMilliseconds(25);
+        printMapCacheState();
     }
 
     private void insertSBMappings(long iid, String ... prefixes) {
+        LOG.debug("Adding Southbound mappings for prefixes: {}", prefixes);
         final InstanceIdType iiType = new InstanceIdType(iid);
         Eid eid = LispAddressUtil.asIpv4PrefixBinaryEid("0.0.0.0/0", iiType);
         mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
@@ -648,6 +724,11 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
             mapService.addMapping(MappingOrigin.Southbound, record.getEid(), null,
                     new MappingData(record, System.currentTimeMillis()));
         }
+        printMapCacheState();
+    }
+
+    private void printMapCacheState() {
+        LOG.debug("Map-cache state:\n{}", mapService.prettyPrintMappings());
     }
 
     /**
@@ -664,6 +745,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
                 .setLocatorRecord(Lists.newArrayList(new LocatorRecordBuilder()
                         .setRloc(LispAddressUtil.asIpv4Rloc("2.2.2.2"))
                         .setLocatorId("loc_id")
+                        .setRouted(true)
                         .setPriority((short) 1)
                         .setWeight((short) 1).build()))
                 .setTimestamp(System.currentTimeMillis())
@@ -703,7 +785,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
 
         //TEST CASE 2
-        //following action should trigger generatting of SMR messages:
+        //following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.1/32
         multiSiteScenario.storeNorthMappingSrcDst(SITE_A, SITE_C);
@@ -714,7 +796,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         multiSiteScenario.assertPingFails(SITE_D4, 5, SITE_C, 4);
 
         //TEST CASE 3
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.1/32
         // 3) 192.0.1.5/32
@@ -727,76 +809,87 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         multiSiteScenario.assertPingWorks(SITE_D4, 5, SITE_C, 4);
 
         //TEST CASE 4
-        // following action should trigger generatting of SMR messages:
-        // 1) 192.0.4.5/32
+        // following action should trigger generating of SMR messages:
+        // 1) 192.0.2.5/32
+        // 2) 192.0.1.1/32
+        // 3) 192.0.1.5/32
+        // 4) 192.0.4.5/32
         multiSiteScenario.storeNorthMappingSrcDst(SITE_B, SITE_C_RLOC_10);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D4.getHost(5));
+        multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B.getHost(5), SITE_A.getHost(1),
+                SITE_A.getHost(5), SITE_D4.getHost(5));
         //way of testing ping - get RLOC for mapping src-dst and compare it with awaited value doesn't test
         //that ping won't be successfull
         multiSiteScenario.assertPingFails(SITE_B, 5, SITE_C, 4);
 
         //TEST CASE 5
-        // following action should trigger generatting of SMR messages:
-        // 1) 192.0.4.5/32
-        // 2) 192.0.2.5/32
+        // following action should trigger generating of SMR messages:
+        // 1) 192.0.2.5/32
+        // 2) 192.0.1.1/32
+        // 3) 192.0.1.5/32
+        // 4) 192.0.4.5/32
         multiSiteScenario.storeNorthMappingNegative(SITE_C, Action.Drop);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D4.getHost(5), SITE_B.getHost(5));
+        multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B.getHost(5), SITE_A.getHost(1),
+                SITE_A.getHost(5), SITE_D4.getHost(5));
         multiSiteScenario.assertPingFails(SITE_D4, 5, SITE_C, 4);
 
         //TEST CASE 6
         multiSiteScenario.assertPingFails(SITE_D5, 5, SITE_C, 3);
 
         //TEST CASE 7
-        multiSiteScenario.deleteNorthMapingSrcDst(SITE_A, SITE_C);
-        sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.4.5/32
         // 2) 192.0.2.5/32
         // 3) 192.0.5.5/32
+        // 4) 192.0.1.5/32
+        // 5) 192.0.1.1/32
+        multiSiteScenario.deleteNorthMapingSrcDst(SITE_A, SITE_C);
+        sleepForSeconds(MULTI_SITE_SLEEP_TIME);
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
-                SITE_B.getHost(5));
+                SITE_B.getHost(5), SITE_A.getHost(1), SITE_A.getHost(5));
 
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.4.5/32
         // 2) 192.0.2.5/32
         // 3) 192.0.5.5/32
+        // 4) 192.0.1.5/32
+        // 5) 192.0.1.1/32
         multiSiteScenario.storeNorthMappingSrcDst(SITE_B, SITE_C);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
-                SITE_B.getHost(5));
+                SITE_B.getHost(5), SITE_A.getHost(1), SITE_A.getHost(5));
 
         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
 
         //TEST CASE 8
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.4.5/32
         // 2) 192.0.2.5/32
         // 3) 192.0.5.5/32
-        // 4) 192.0.1.1/32
+        // 4) 192.0.1.5/32
+        // 5) 192.0.1.1/32
         multiSiteScenario.deleteNorthMapingSrcDst(SITE_B, SITE_C);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
-                SITE_B.getHost(5),
-                SITE_A.getHost(1));
+                SITE_B.getHost(5), SITE_A.getHost(1), SITE_A.getHost(5));
         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
         multiSiteScenario.assertPingFails(SITE_B, 5, SITE_C, 4);
         multiSiteScenario.assertPingFails(SITE_A, 1, SITE_C, 4);
 
         //TEST CASE 9
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.4.5/32
         // 2) 192.0.2.5/32
         // 3) 192.0.5.5/32
-        // 4) 192.0.1.1/32
+        // 4) 192.0.1.5/32
+        // 5) 192.0.1.1/32
         multiSiteScenario.deleteNorthMappingNegative(SITE_C);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_D5.getHost(5), SITE_D4.getHost(5),
-                SITE_B.getHost(5),
-                SITE_A.getHost(1));
+                SITE_B.getHost(5), SITE_A.getHost(1), SITE_A.getHost(5));
         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_B, 4);
         multiSiteScenario.assertPingWorks(SITE_B, 5, SITE_C, 4);
         multiSiteScenario.assertPingWorks(SITE_A, 5, SITE_C, 4);
@@ -831,14 +924,14 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         multiSiteScenario.assertPingWorks(SITE_B_SB, 5, SITE_C_WP_50_2_SB, 4, SITE_D_WP_50_2_SB);
 
         //TEST CASE 2
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.storeNorthMappingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB, SITE_D_WP_50_2_SB);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
 
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.storeNorthMappingNegative(SITE_C_SB, Action.Drop);
@@ -850,7 +943,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
 
 
         //TEST CASE 3
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.storeNorthMappingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB);
@@ -861,7 +954,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         //TEST CASE 4
         multiSiteScenario.storeNorthMappingSrcDst(SITE_B_SB, SITE_C_WP_50_2_SB, SITE_D_WP_50_2_SB);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
@@ -870,7 +963,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         //TEST CASE 5
         multiSiteScenario.deleteSouthboundMappings(SITE_D_DELETE_SB);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
@@ -880,7 +973,7 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         //TEST CASE 6
         multiSiteScenario.deleteNorthMapingSrcDst(SITE_A_SB, SITE_C_WP_50_2_SB);
         sleepForSeconds(MULTI_SITE_SLEEP_TIME);
-        // following action should trigger generatting of SMR messages:
+        // following action should trigger generating of SMR messages:
         // 1) 192.0.2.5/32
         // 2) 192.0.1.5/32
         multiSiteScenario.checkSMR(socketReader, SITE_C.getEidPrefix(), SITE_B_SB.getHost(5), SITE_A_SB.getHost(5));
@@ -1770,8 +1863,11 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
         mapRegisterBuilder.setMappingRecordItem(new ArrayList<MappingRecordItem>());
         mapRegisterBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(
                 etlrBuilder.build()).build());
-        sendMapRegister(mapRegisterBuilder.build());
+        MapRegister mapRegister = mapRegisterBuilder.build();
+        LOG.trace("Sending Map-Register via socket: {}", mapRegister);
+        sendMapRegister(mapRegister);
         MapNotify mapNotify = receiveMapNotify();
+        LOG.trace("Received Map-Notify via socket: {}", mapNotify);
         assertEquals(8, mapNotify.getNonce().longValue());
         // wait for the notifications to propagate
         sleepForSeconds(1);
@@ -2381,12 +2477,24 @@ public class MappingServiceIntegrationTest extends AbstractMdsalTestBase {
     }
 
     private MapRequest receiveMapRequest(DatagramSocket datagramSocket) throws SocketTimeoutException {
-        return MapRequestSerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket(
-                datagramSocket, 30000).getData()), null);
+        ByteBuffer packet = ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData());
+        while (!checkType(packet, MessageType.MapRequest)) {
+            packet = ByteBuffer.wrap(receivePacket(datagramSocket, 30000).getData());
+        }
+        return MapRequestSerializer.getInstance().deserialize(packet, null);
     }
 
     private MapNotify receiveMapNotify() throws SocketTimeoutException {
-        return MapNotifySerializer.getInstance().deserialize(ByteBuffer.wrap(receivePacket().getData()));
+        ByteBuffer packet = ByteBuffer.wrap(receivePacket().getData());
+        while (!checkType(packet, MessageType.MapNotify)) {
+            packet = ByteBuffer.wrap(receivePacket().getData());
+        }
+        return MapNotifySerializer.getInstance().deserialize(packet);
+    }
+
+    private static boolean checkType(ByteBuffer packet, MessageType type) {
+        final int receivedType = ByteUtil.getUnsignedByte(packet, LispMessage.Pos.TYPE) >> 4;
+        return MessageType.forValue(receivedType) == type;
     }
 
     private void sendMapRequest(MapRequest mapRequest) {