Magnesium platform upgrade
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / MappingSystem.java
index 922659bba43b4a8f40b97b541555819fcb72fed9..1c9799d7cc2bdfa7ab979bd44cb97d599e80b2fb 100644 (file)
@@ -19,8 +19,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.lispflowmapping.config.ConfigIni;
 import org.opendaylight.lispflowmapping.dsbackend.DataStoreBackEnd;
 import org.opendaylight.lispflowmapping.implementation.timebucket.implementation.TimeBucketMappingTimeoutService;
@@ -48,6 +46,8 @@ import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
 import org.opendaylight.lispflowmapping.mapcache.MultiTableMapCache;
 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
 import org.opendaylight.lispflowmapping.mapcache.lisp.LispMapCacheStringifier;
+import org.opendaylight.mdsal.binding.api.NotificationPublishService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ExplicitLocatorPath;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.Ipv4;
@@ -88,19 +88,19 @@ public class MappingSystem implements IMappingSystem {
     private static final String AUTH_KEY_TABLE = "authentication";
     //private static final int TTL_RLOC_TIMED_OUT = 1;
     private static final int TTL_NO_RLOC_KNOWN = ConfigIni.getInstance().getNegativeMappingTTL();
-    private NotificationPublishService notificationPublishService;
+    private final NotificationPublishService notificationPublishService;
     private boolean mappingMerge;
-    private ILispDAO dao;
+    private final ILispDAO dao;
     private ILispDAO sdao;
     private ILispMapCache smc;
     private IMapCache pmc;
-    private ConcurrentHashMap<Eid, Set<Subscriber>> subscriberdb = new ConcurrentHashMap<>();
+    private final ConcurrentHashMap<Eid, Set<Subscriber>> subscriberdb = new ConcurrentHashMap<>();
     private IAuthKeyDb akdb;
     private final EnumMap<MappingOrigin, IMapCache> tableMap = new EnumMap<>(MappingOrigin.class);
     private DataStoreBackEnd dsbe;
     private boolean isMaster = false;
 
-    private ISouthBoundMappingTimeoutService sbMappingTimeoutService;
+    private final ISouthBoundMappingTimeoutService sbMappingTimeoutService;
 
     public MappingSystem(ILispDAO dao, boolean iterateMask, NotificationPublishService nps, boolean mappingMerge) {
         this.dao = dao;
@@ -210,7 +210,7 @@ public class MappingSystem implements IMappingSystem {
     }
 
     private static MappingRecord getMappingRecord(MappingData mappingData) {
-        return (mappingData != null) ? mappingData.getRecord() : null;
+        return mappingData != null ? mappingData.getRecord() : null;
     }
 
     @SuppressWarnings("unchecked")
@@ -243,23 +243,19 @@ public class MappingSystem implements IMappingSystem {
     private void handleSbNegativeMappings(Eid key) {
         Set<Eid> childPrefixes = getSubtree(MappingOrigin.Southbound, key);
 
-        LOG.trace("handleSbNegativeMappings(): subtree prefix set for EID {} (excluding the EID itself): {}",
+        LOG.trace("handleSbNegativeMappings(): subtree prefix set for EID {}: {}",
                 LispAddressStringifier.getString(key),
                 LispAddressStringifier.getString(childPrefixes));
-        if (childPrefixes == null || childPrefixes.isEmpty()) {
-            // The assumption here is that negative prefixes are well maintained and never overlapping.
-            // If we have children for the EID, no parent lookup should thus be necessary.
-            Eid parentPrefix = smc.getCoveringLessSpecific(key);
-            LOG.trace("handleSbNegativeMappings(): parent prefix for EID {}: {}",
-                    LispAddressStringifier.getString(key),
-                    LispAddressStringifier.getString(parentPrefix));
-            handleSbNegativeMapping(parentPrefix);
-            return;
-        }
 
         for (Eid prefix : childPrefixes) {
             handleSbNegativeMapping(prefix);
         }
+
+        Eid parentPrefix = smc.getCoveringLessSpecific(key);
+        LOG.trace("handleSbNegativeMappings(): parent prefix for EID {}: {}",
+                LispAddressStringifier.getString(key),
+                LispAddressStringifier.getString(parentPrefix));
+        handleSbNegativeMapping(parentPrefix);
     }
 
     private void handleSbNegativeMapping(Eid key) {
@@ -333,6 +329,7 @@ public class MappingSystem implements IMappingSystem {
      * Since this method is only called when there is a hit in the southbound Map-Register cache, and that cache is
      * not used when merge is on, it's OK to ignore the effects of timestamp changes on merging for now.
      */
+    @Override
     public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
 
         sbMappingTimeoutService.removeExpiredMappings();
@@ -370,7 +367,7 @@ public class MappingSystem implements IMappingSystem {
         }
 
         LocatorRecord locatorRecord = mappingData.getRecord().getLocatorRecord().get(0);
-        long serviceIndex = ((ServicePath) eid.getAddress()).getServicePath().getServiceIndex();
+        long serviceIndex = ((ServicePath) eid.getAddress()).getServicePath().getServiceIndex().toJava();
         int index = LispAddressUtil.STARTING_SERVICE_INDEX - (int) serviceIndex;
         Rloc rloc = locatorRecord.getRloc();
         if (rloc.getAddress() instanceof Ipv4 || rloc.getAddress() instanceof Ipv6) {
@@ -498,11 +495,17 @@ public class MappingSystem implements IMappingSystem {
 
     private MappingData getSbMappingWithExpiration(Eid src, Eid dst, XtrId xtrId) {
         MappingData mappingData = (MappingData) smc.getMapping(dst, xtrId);
-        if (mappingData != null && MappingMergeUtil.mappingIsExpired(mappingData)) {
-            return handleSbExpiredMapping(dst, xtrId, mappingData);
-        } else {
-            return mappingData;
+        while (mappingData != null && MappingMergeUtil.mappingIsExpired(mappingData)) {
+            // If the mappingData is expired, handleSbExpiredMapping() will run merge for it if merge is enabled,
+            // otherwise it will remove the expired mapping, returning null.
+            MappingData mergedMappingData = handleSbExpiredMapping(dst, xtrId, mappingData);
+            if (mergedMappingData != null) {
+                return mergedMappingData;
+            }
+            // If the expired mapping was removed, we look up the original query again
+            mappingData = (MappingData) smc.getMapping(dst, xtrId);
         }
+        return mappingData;
     }
 
     public MappingData handleSbExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
@@ -739,13 +742,13 @@ public class MappingSystem implements IMappingSystem {
     private static Eid getVirtualParent(Eid eid) {
         if (eid.getAddress() instanceof Ipv4PrefixBinary) {
             Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress();
-            short parentPrefixLength = (short) (prefix.getIpv4MaskLength() - 1);
+            short parentPrefixLength = (short) (prefix.getIpv4MaskLength().toJava() - 1);
             byte[] parentPrefix = MaskUtil.normalizeByteArray(prefix.getIpv4AddressBinary().getValue(),
                     parentPrefixLength);
             return LispAddressUtil.asIpv4PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);
         } else if (eid.getAddress() instanceof Ipv6PrefixBinary) {
             Ipv6PrefixBinary prefix = (Ipv6PrefixBinary) eid.getAddress();
-            short parentPrefixLength = (short) (prefix.getIpv6MaskLength() - 1);
+            short parentPrefixLength = (short) (prefix.getIpv6MaskLength().toJava() - 1);
             byte[] parentPrefix = MaskUtil.normalizeByteArray(prefix.getIpv6AddressBinary().getValue(),
                     parentPrefixLength);
             return LispAddressUtil.asIpv6PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);