Merge "Provide Karaf 4 features"
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / MappingSystem.java
index 4f52cc8224f36dc15a8f17f6b0004e2fccfe35dc..9005491813ded5773f1c3c0c447a578ac6bd089e 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Cisco Systems, Inc.  All rights reserved.
+ * Copyright (c) 2015, 2017 Cisco Systems, Inc.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -8,25 +8,36 @@
 
 package org.opendaylight.lispflowmapping.implementation;
 
+import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.EnumMap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+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;
+import org.opendaylight.lispflowmapping.implementation.timebucket.interfaces.ISouthBoundMappingTimeoutService;
 import org.opendaylight.lispflowmapping.implementation.util.DSBEInputUtil;
+import org.opendaylight.lispflowmapping.implementation.util.MSNotificationInputUtil;
 import org.opendaylight.lispflowmapping.implementation.util.MappingMergeUtil;
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
+import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
+import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
+import org.opendaylight.lispflowmapping.interfaces.mapcache.IAuthKeyDb;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.ILispMapCache;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMappingSystem;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.type.MappingData;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
+import org.opendaylight.lispflowmapping.mapcache.AuthKeyDb;
 import org.opendaylight.lispflowmapping.mapcache.MultiTableMapCache;
 import org.opendaylight.lispflowmapping.mapcache.SimpleMapCache;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.SimpleAddress;
@@ -36,14 +47,19 @@ import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.addres
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.ServicePath;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.explicit.locator.path.explicit.locator.path.Hop;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.inet.binary.types.rev160303.IpAddressBinary;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv4PrefixBinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.Ipv6PrefixBinaryAfi;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.rloc.container.Rloc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChange;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.AuthenticationKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.db.instance.Mapping;
@@ -55,24 +71,36 @@ import org.slf4j.LoggerFactory;
  * mapping lookups.
  *
  * @author Florin Coras
+ * @author Lorand Jakab
  *
  */
 public class MappingSystem implements IMappingSystem {
     private static final Logger LOG = LoggerFactory.getLogger(MappingSystem.class);
-    private boolean notificationService;
-    private boolean overwrite;
+    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 = 15;
+    private NotificationPublishService notificationPublishService;
+    private boolean mappingMerge;
     private ILispDAO dao;
+    private ILispDAO sdao;
     private ILispMapCache smc;
     private IMapCache pmc;
+    private IAuthKeyDb akdb;
     private final EnumMap<MappingOrigin, IMapCache> tableMap = new EnumMap<>(MappingOrigin.class);
     private DataStoreBackEnd dsbe;
     private boolean isMaster = false;
 
-    public MappingSystem(ILispDAO dao, boolean iterateMask, boolean notifications, boolean overwrite) {
+    private ISouthBoundMappingTimeoutService sbMappingTimeoutService;
+
+    public MappingSystem(ILispDAO dao, boolean iterateMask, NotificationPublishService nps, boolean mappingMerge) {
         this.dao = dao;
-        this.notificationService = notifications;
-        this.overwrite = overwrite;
+        this.notificationPublishService = nps;
+        this.mappingMerge = mappingMerge;
         buildMapCaches();
+
+        sbMappingTimeoutService = new TimeBucketMappingTimeoutService(ConfigIni.getInstance()
+                .getNumberOfBucketsInTimeBucketWheel(), ConfigIni.getInstance().getRegistrationValiditySb(),
+                this);
     }
 
     public void setDataStoreBackEnd(DataStoreBackEnd dsbe) {
@@ -80,8 +108,8 @@ public class MappingSystem implements IMappingSystem {
     }
 
     @Override
-    public void setOverwritePolicy(boolean overwrite) {
-        this.overwrite = overwrite;
+    public void setMappingMerge(boolean mappingMerge) {
+        this.mappingMerge = mappingMerge;
     }
 
     @Override
@@ -102,13 +130,18 @@ public class MappingSystem implements IMappingSystem {
          * but that option is no longer supported in the code, since it was never tested and may lead to unexpected
          * results.
          */
-        smc = new SimpleMapCache(dao.putTable(MappingOrigin.Southbound.toString()));
+        sdao = dao.putTable(MappingOrigin.Southbound.toString());
         pmc = new MultiTableMapCache(dao.putTable(MappingOrigin.Northbound.toString()));
+        smc = new SimpleMapCache(sdao);
+        akdb = new AuthKeyDb(dao.putTable(AUTH_KEY_TABLE));
         tableMap.put(MappingOrigin.Northbound, pmc);
         tableMap.put(MappingOrigin.Southbound, smc);
     }
 
     public void addMapping(MappingOrigin origin, Eid key, MappingData mappingData) {
+
+        sbMappingTimeoutService.removeExpiredMappings();
+
         if (mappingData == null) {
             LOG.warn("addMapping() called with null mapping, ignoring");
             return;
@@ -116,44 +149,101 @@ public class MappingSystem implements IMappingSystem {
 
         if (origin == MappingOrigin.Southbound) {
             XtrId xtrId = mappingData.getXtrId();
-            if (xtrId == null && mappingData.isMergeEnabled()) {
+            if (xtrId == null && mappingMerge && mappingData.isMergeEnabled()) {
                 LOG.warn("addMapping() called will null xTR-ID in MappingRecord, while merge is set, ignoring");
                 return;
             }
-            if (xtrId != null && !overwrite) {
-                smc.addMapping(key, xtrId, mappingData);
-            }
-            if (xtrId != null && mappingData.isMergeEnabled()) {
-                List<XtrId> expiredMappings = new ArrayList<>();
-                Set<IpAddressBinary> sourceRlocs = new HashSet<>();
-                MappingData mergedMappingData = MappingMergeUtil.mergeXtrIdMappings(smc.getAllXtrIdMappings(key),
-                        expiredMappings, sourceRlocs);
-                smc.removeXtrIdMappings(key, expiredMappings);
-                if (mergedMappingData != null) {
-                    smc.addMapping(key, mergedMappingData, sourceRlocs);
+            if (xtrId != null && mappingMerge) {
+                if (mappingData.isMergeEnabled()) {
+                    smc.addMapping(key, xtrId, mappingData);
+                    handleMergedMapping(key);
+                    return;
+                } else {
+                    clearPresentXtrIdMappings(key);
+                    smc.addMapping(key, xtrId, mappingData);
                 }
-                return;
             }
+            addOrRefreshMappingInTimeoutService(key, mappingData);
         }
 
         tableMap.get(origin).addMapping(key, mappingData);
     }
 
+    private void clearPresentXtrIdMappings(Eid key) {
+        List<MappingData> allXtrMappingList = (List<MappingData>) (List<?>) smc.getAllXtrIdMappings(key);
+
+        if (((MappingData) smc.getMapping(key, (XtrId) null)).isMergeEnabled()) {
+            LOG.trace("Different xTRs have different merge configuration!");
+        }
+
+        for (MappingData mappingData : allXtrMappingList) {
+            removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
+        }
+    }
+
+    private void addOrRefreshMappingInTimeoutService(Eid key, MappingData mappingData) {
+        Integer oldBucketId = (Integer) smc.getData(key, SubKeys.TIME_BUCKET_ID);
+        Integer updatedBucketId;
+
+        if (oldBucketId != null) {
+            //refresh mapping
+            updatedBucketId = sbMappingTimeoutService.refreshMapping(key, mappingData, oldBucketId);
+        } else {
+            updatedBucketId = sbMappingTimeoutService.addMapping(key, mappingData);
+        }
+
+        smc.addData(key, SubKeys.TIME_BUCKET_ID, updatedBucketId);
+    }
+
+    @Override
+    public MappingData addNegativeMapping(Eid key) {
+        MappingRecord mapping = buildNegativeMapping(key);
+        MappingData mappingData = new MappingData(mapping);
+        smc.addMapping(mapping.getEid(), mappingData);
+        dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, mapping.getEid(), null, mappingData));
+        return mappingData;
+    }
+
+    private MappingRecord buildNegativeMapping(Eid eid) {
+        MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
+        recordBuilder.setAuthoritative(false);
+        recordBuilder.setMapVersion((short) 0);
+        recordBuilder.setEid(eid);
+        if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
+                || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
+            Eid widestNegativePrefix = getWidestNegativePrefix(eid);
+            if (widestNegativePrefix != null) {
+                recordBuilder.setEid(widestNegativePrefix);
+            }
+        }
+        recordBuilder.setAction(LispMessage.NEGATIVE_MAPPING_ACTION);
+        if (getAuthenticationKey(eid) != null) {
+            recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
+        } else {
+            recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
+        }
+        return recordBuilder.build();
+    }
+
     /*
      * 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.
      */
     public void refreshMappingRegistration(Eid key, XtrId xtrId, Long timestamp) {
+
+        sbMappingTimeoutService.removeExpiredMappings();
+
         if (timestamp == null) {
             timestamp = System.currentTimeMillis();
         }
         MappingData mappingData = (MappingData) smc.getMapping(null, key);
         if (mappingData != null) {
             mappingData.setTimestamp(new Date(timestamp));
+            addOrRefreshMappingInTimeoutService(key, mappingData);
         } else {
             LOG.warn("Could not update timestamp for EID {}, no mapping found", LispAddressStringifier.getString(key));
         }
-        if (xtrId != null) {
+        if (mappingMerge && xtrId != null) {
             MappingData xtrIdMappingData = (MappingData) smc.getMapping(key, xtrId);
             if (xtrIdMappingData != null) {
                 xtrIdMappingData.setTimestamp(new Date(timestamp));
@@ -204,6 +294,27 @@ public class MappingSystem implements IMappingSystem {
         }
     }
 
+    private MappingData handleMergedMapping(Eid key) {
+        List<MappingData> expiredMappingDataList = new ArrayList<>();
+        Set<IpAddressBinary> sourceRlocs = new HashSet<>();
+
+        MappingData mergedMappingData = MappingMergeUtil.mergeXtrIdMappings(smc.getAllXtrIdMappings(key),
+                expiredMappingDataList, sourceRlocs);
+
+        for (MappingData mappingData : expiredMappingDataList) {
+            removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
+        }
+
+        if (mergedMappingData != null) {
+            smc.addMapping(key, mergedMappingData, sourceRlocs);
+            dsbe.addMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key, mergedMappingData));
+            addOrRefreshMappingInTimeoutService(key, mergedMappingData);
+        } else {
+            removeSbMapping(key, mergedMappingData);
+        }
+        return mergedMappingData;
+    }
+
     @Override
     public MappingData getMapping(Eid src, Eid dst) {
         // NOTE: Currently we have two lookup algorithms implemented, which are configurable
@@ -278,25 +389,43 @@ 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)) {
-            removeExpiredMapping(dst, xtrId, mappingData);
-            return null;
+            return handleSbExpiredMapping(dst, xtrId, mappingData);
         } else {
             return mappingData;
         }
     }
 
-    /*
-     * This private method either removes the main mapping ONLY, or the xTR-ID mapping ONLY, based on xtrId being
-     * null or non-null. Caller functions should take care of removing both when necessary.
-     */
-    private void removeExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
-        if (xtrId == null) {
-            smc.removeMapping(key);
-            dsbe.removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, mappingData.getRecord().getEid(),
-                    new SiteId(mappingData.getRecord().getSiteId()), mappingData));
+    public MappingData handleSbExpiredMapping(Eid key, XtrId xtrId, MappingData mappingData) {
+        if (mappingMerge && mappingData.isMergeEnabled()) {
+            return handleMergedMapping(key);
+        }
+
+        if (xtrId != null) {
+            removeSbXtrIdSpecificMapping(key, xtrId, mappingData);
         } else {
-            smc.removeMapping(key, xtrId);
-            dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
+            removeSbMapping(key, mappingData);
+        }
+        return null;
+    }
+
+    private void removeSbXtrIdSpecificMapping(Eid key, XtrId xtrId, MappingData mappingData) {
+        smc.removeMapping(key, xtrId);
+        dsbe.removeXtrIdMapping(DSBEInputUtil.toXtrIdMapping(mappingData));
+    }
+
+    private void removeSbMapping(Eid key, MappingData mappingData) {
+        if (mappingData != null && mappingData.getXtrId() != null) {
+            removeSbXtrIdSpecificMapping(key, mappingData.getXtrId(), mappingData);
+        }
+        removeFromSbTimeoutService(key);
+        smc.removeMapping(key);
+        dsbe.removeMapping(DSBEInputUtil.toMapping(MappingOrigin.Southbound, key, mappingData));
+    }
+
+    private void removeFromSbTimeoutService(Eid key) {
+        Integer bucketId = (Integer) smc.getData(key, SubKeys.TIME_BUCKET_ID);
+        if (bucketId != null) {
+            sbMappingTimeoutService.removeMappingFromTimeoutService(key, bucketId);
         }
     }
 
@@ -322,17 +451,82 @@ public class MappingSystem implements IMappingSystem {
 
     @Override
     public void removeMapping(MappingOrigin origin, Eid key) {
+        Set<Subscriber> subscribers = null;
+        if (origin == MappingOrigin.Southbound) {
+            removeFromSbTimeoutService(key);
+            MappingData mapping = (MappingData) smc.getMapping(null, key);
+            if (mapping != null && !mapping.isNegative()) {
+                SimpleImmutableEntry<Eid, Set<Subscriber>> mergedNegativePrefix = computeMergedNegativePrefix(key);
+                if (mergedNegativePrefix != null) {
+                    addNegativeMapping(mergedNegativePrefix.getKey());
+                    subscribers = mergedNegativePrefix.getValue();
+                    try {
+                        notificationPublishService.putNotification(
+                                MSNotificationInputUtil.toMappingChanged(mapping, subscribers, MappingChange.Created));
+                    } catch (InterruptedException e) {
+                        LOG.warn("Notification publication interrupted!");
+                    }
+                }
+            }
+        }
         tableMap.get(origin).removeMapping(key);
-        if (notificationService) {
-            // TODO
+    }
+
+    @SuppressWarnings("unchecked")
+    /*
+     * Returns the "merged" prefix and the subscribers of the prefixes that were merged.
+     */
+    private SimpleImmutableEntry<Eid, Set<Subscriber>> computeMergedNegativePrefix(Eid eid) {
+        // Variable to hold subscribers we collect along the way
+        Set<Subscriber> subscribers = null;
+
+        // If prefix sibling has a negative mapping, save its subscribers
+        Eid sibling = smc.getSiblingPrefix(eid);
+        MappingData mapping = (MappingData) smc.getMapping(null, sibling);
+        if (mapping != null && mapping.isNegative()) {
+            subscribers = (Set<Subscriber>) getData(MappingOrigin.Southbound, eid, SubKeys.SUBSCRIBERS);
+        } else {
+            return null;
+        }
+
+        Eid currentNode = sibling;
+        Eid previousNode = sibling;
+        while ((currentNode = smc.getVirtualParentSiblingPrefix(currentNode)) != null) {
+            mapping = (MappingData) smc.getMapping(null, currentNode);
+            if (mapping != null && mapping.isNegative()) {
+                subscribers.addAll((Set<Subscriber>)
+                        getData(MappingOrigin.Southbound, currentNode, SubKeys.SUBSCRIBERS));
+                removeSbMapping(currentNode, mapping);
+            } else {
+                break;
+            }
+            previousNode = currentNode;
+        }
+        return new SimpleImmutableEntry<>(getVirtualParent(previousNode), subscribers);
+    }
+
+    private static Eid getVirtualParent(Eid eid) {
+        if (eid.getAddress() instanceof Ipv4PrefixBinary) {
+            Ipv4PrefixBinary prefix = (Ipv4PrefixBinary) eid.getAddress();
+            short parentPrefixLength = (short) (prefix.getIpv4MaskLength() - 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);
+            byte[] parentPrefix = MaskUtil.normalizeByteArray(prefix.getIpv6AddressBinary().getValue(),
+                    parentPrefixLength);
+            return LispAddressUtil.asIpv6PrefixBinaryEid(eid, parentPrefix, parentPrefixLength);
         }
+        return null;
     }
 
     @Override
     public void addAuthenticationKey(Eid key, MappingAuthkey authKey) {
         LOG.debug("Adding authentication key '{}' with key-ID {} for {}", authKey.getKeyString(), authKey.getKeyType(),
                 LispAddressStringifier.getString(key));
-        smc.addAuthenticationKey(key, authKey);
+        akdb.addAuthenticationKey(key, authKey);
     }
 
     @Override
@@ -340,7 +534,7 @@ public class MappingSystem implements IMappingSystem {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Retrieving authentication key for {}", LispAddressStringifier.getString(key));
         }
-        return smc.getAuthenticationKey(key);
+        return akdb.getAuthenticationKey(key);
     }
 
     @Override
@@ -348,7 +542,7 @@ public class MappingSystem implements IMappingSystem {
         if (LOG.isDebugEnabled()) {
             LOG.debug("Removing authentication key for {}", LispAddressStringifier.getString(key));
         }
-        smc.removeAuthenticationKey(key);
+        akdb.removeAuthenticationKey(key);
     }
 
     @Override
@@ -376,6 +570,11 @@ public class MappingSystem implements IMappingSystem {
         tableMap.get(origin).removeData(key, subKey);
     }
 
+    @Override
+    public Eid getParentPrefix(Eid key) {
+        return smc.getParentPrefix(key);
+    }
+
 
     /**
      * Restore all mappings and keys from mdsal datastore.
@@ -428,11 +627,24 @@ public class MappingSystem implements IMappingSystem {
         return sb.toString();
     }
 
+    @Override
+    public String printKeys() {
+        return akdb.printKeys();
+    }
+
     public void cleanCaches() {
         dao.removeAll();
         buildMapCaches();
     }
 
+    /*
+     * XXX  Mappings and keys should be separated for this to work properly, as is it will remove northbound originated
+     * authentication keys too, since they are currently stored in smc.
+     */
+    public void cleanSBMappings() {
+        smc = new SimpleMapCache(sdao);
+    }
+
     @Override
     public void setIsMaster(boolean isMaster) {
         this.isMaster = isMaster;