+ @Test
+ public void testNegativePrefix() throws UnknownHostException {
+ insertMappings();
+ testGapIntersection();
+ testMultipleMappings();
+ }
+
+ private void testMultipleMappings() throws UnknownHostException {
+ final InstanceIdType iid = new InstanceIdType(1L);
+ final String prefix1 = "1.1.127.10/32"; // prefix from the intersection of NB and SB gaps
+ final String prefix2 = "1.1.200.255/32"; // prefix with existing mapping in NB
+ final String prefix3 = "1.3.255.255/32";
+
+ final MapRequest mapRequest = new MapRequestBuilder().setEidItem(Lists.newArrayList(
+ new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixBinaryEid(prefix1, iid))
+ .build(),
+ new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixBinaryEid(prefix2, iid))
+ .build(),
+ new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixBinaryEid(prefix3, iid))
+ .build()))
+ .build();
+ final MapReply mapReply = lms.handleMapRequest(mapRequest);
+
+ // expected result
+ final String resultPrefix1 = "1.1.64.0";
+ final Address resultNegMapping1 = new Ipv4PrefixBinaryBuilder()
+ .setIpv4AddressBinary(new Ipv4AddressBinary(InetAddress.getByName(resultPrefix1).getAddress()))
+ .setIpv4MaskLength((short) 18).build();
+
+ final String resultPrefix2 = "1.1.192.0";
+ final Address resultMapping2 = new Ipv4PrefixBinaryBuilder()
+ .setIpv4AddressBinary(new Ipv4AddressBinary(InetAddress.getByName(resultPrefix2).getAddress()))
+ .setIpv4MaskLength((short) 18).build();
+
+ final String resultPrefix3 = "1.3.0.0";
+ final Address resultNegMapping3 = new Ipv4PrefixBinaryBuilder()
+ .setIpv4AddressBinary(new Ipv4AddressBinary(InetAddress.getByName(resultPrefix3).getAddress()))
+ .setIpv4MaskLength((short) 16).build();
+
+ assertEquals(resultNegMapping1, mapReply.getMappingRecordItem().get(0).getMappingRecord().getEid()
+ .getAddress());
+ assertEquals(resultMapping2, mapReply.getMappingRecordItem().get(1).getMappingRecord().getEid()
+ .getAddress());
+ assertEquals(resultNegMapping3, mapReply.getMappingRecordItem().get(2).getMappingRecord().getEid()
+ .getAddress());
+ }
+
+ /**
+ * Tests a negative mapping from an intersection of gaps in northbound and southbound.
+ */
+ private void testGapIntersection() throws UnknownHostException {
+ final InstanceIdType iid = new InstanceIdType(1L);
+
+ // request an Eid from a gap between mappings
+ final MapRequest mapRequest = new MapRequestBuilder().setEidItem(Lists.newArrayList(
+ new EidItemBuilder().setEid(LispAddressUtil.asIpv4PrefixBinaryEid("1.1.127.10/32", iid))
+ .build()))
+ .build();
+ final MapReply mapReply = lms.handleMapRequest(mapRequest);
+
+ // expected negative mapping
+ final Address resultNegMapping = new Ipv4PrefixBinaryBuilder()
+ .setIpv4AddressBinary(new Ipv4AddressBinary(InetAddress.getByName("1.1.64.0").getAddress()))
+ .setIpv4MaskLength((short) 18).build();
+ assertEquals(resultNegMapping, mapReply.getMappingRecordItem().get(0).getMappingRecord().getEid()
+ .getAddress());
+ }
+
+ private void insertMappings() {
+ cleanUP();
+ mapService.setLookupPolicy(IMappingService.LookupPolicy.NB_AND_SB);
+
+ final InstanceIdType iid = new InstanceIdType(1L);
+ final String prefixNbLeft = "1.2.0.0/16";
+ final String prefixNbRight = "1.1.128.0/17";
+ final String prefixSbLeft = "1.1.32.0/19";
+ final String prefixSbRight = "1.0.0.0/8";
+
+ final MappingRecord mapRecordNbLeft = newMappingRecord(prefixNbLeft, iid);
+ final MappingRecord mapRecordNbRight = newMappingRecord(prefixNbRight, iid);
+ final MappingRecord mapRecordSbLeft = newMappingRecord(prefixSbLeft, iid);
+ final MappingRecord mapRecordSbRight = newMappingRecord(prefixSbRight, iid);
+
+ /* set auth */
+ final Eid eid = LispAddressUtil.asIpv4PrefixBinaryEid("0.0.0.0/0", iid);
+ mapService.addAuthenticationKey(eid, NULL_AUTH_KEY);
+
+ mapService.addMapping(MappingOrigin.Northbound, mapRecordNbLeft.getEid(), null, mapRecordNbLeft, false);
+ mapService.addMapping(MappingOrigin.Northbound, mapRecordNbRight.getEid(), null, mapRecordNbRight, false);
+ mapService.addMapping(MappingOrigin.Southbound, mapRecordSbLeft.getEid(), null, mapRecordSbLeft, false);
+ mapService.addMapping(MappingOrigin.Southbound, mapRecordSbRight.getEid(), null, mapRecordSbRight, false);
+
+ restartSocket();
+ sleepForSeconds(2);
+ }
+
+ /**
+ * Creates a new MappingRecord object.
+ *
+ * @param prefix The Eid prefix
+ * @param iid VNI
+ * @return new MappingRecord object
+ */
+ private MappingRecord newMappingRecord(String prefix, InstanceIdType iid) {
+ final Eid prefixBinary = LispAddressUtil.asIpv4PrefixBinaryEid(prefix, iid);
+ return new MappingRecordBuilder()
+ .setEid(prefixBinary)
+ .setLocatorRecord(Lists.newArrayList(new LocatorRecordBuilder()
+ .setRloc(LispAddressUtil.asIpv4Rloc("2.2.2.2")).setLocatorId("loc_id").build()))
+ .setTimestamp(System.currentTimeMillis()).setRecordTtl(1440).build();
+ }
+