Bug 6529: Remove registration date when deleting mapping
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / MultiTableMapCache.java
index 3663c36f9c26ab4d9aad941bc1625f3226dfcbbd..0a2df133cd50cec5beb5d54e75e5e78be5198444 100644 (file)
@@ -5,22 +5,21 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.lispflowmapping.mapcache;
 
 import java.util.Date;
 import java.util.Map;
-
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.interfaces.mapcache.IMapCache;
+import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MaskUtil;
 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.address.SourceDestKey;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.mapping.authkey.container.MappingAuthkey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.authkey.container.MappingAuthkey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -30,10 +29,10 @@ import org.slf4j.LoggerFactory;
  * another for source, queried and populated in this exact order.
  *
  * @author Florin Coras
- *
  */
 public class MultiTableMapCache implements IMapCache {
     private static final Logger LOG = LoggerFactory.getLogger(MultiTableMapCache.class);
+
     private ILispDAO dao;
 
     public MultiTableMapCache(ILispDAO dao) {
@@ -79,50 +78,13 @@ public class MultiTableMapCache implements IMapCache {
         }
     }
 
-    // Method returns the DAO entry (hash) corresponding to either the longest prefix match of eid, if eid is maskable,
-    // or the exact match otherwise. eid must be a 'simple' address
-    private Map<String, ?> getDaoEntryBest(Eid eid, ILispDAO dao) {
-        Eid key;
-        if (MaskUtil.isMaskable(eid.getAddress())) {
-            short mask = MaskUtil.getMaskForAddress(eid.getAddress());
-            while (mask > 0) {
-                key = MaskUtil.normalize(eid, mask);
-                mask--;
-                Map<String, ?> entry = dao.get(key);
-                if (entry != null) {
-                    return entry;
-                }
-            }
-            return null;
-        } else {
-            key = MaskUtil.normalize(eid);
-            Map<String, ?> entry = dao.get(key);
-            return entry;
-        }
-    }
-
-    private Object getMappingExactSD(Eid srcEid, Eid dstEid, ILispDAO dao) {
-        Map<String, ?> daoEntry = dao.get(dstEid);
-        if (daoEntry != null) {
-            // try SrcDst eid lookup
-            ILispDAO srcDstDao = (ILispDAO) daoEntry.get(SubKeys.LCAF_SRCDST);
-            if (srcEid != null && srcDstDao != null) {
-                return srcDstDao.getSpecific(srcEid, SubKeys.RECORD);
-            }
-            // if lookup fails, return whatever is found for dst eid
-            return daoEntry.get(SubKeys.RECORD);
-        }
-        return null;
-    }
-
     // Returns the mapping corresponding to the longest prefix match for eid.
     // eid must be a simple (maskable or not) address
-    private Object getMappingLpmEid(Eid eid, ILispDAO dao) {
+    private Object getMappingLpmEid(Eid eid, ILispDAO mappingsDb) {
         if (eid == null) {
             return null;
         }
-        Eid key = MaskUtil.normalize(eid);
-        Map<String, ?> daoEntry = getDaoEntryBest(key, dao);
+        Map<String, ?> daoEntry = mappingsDb.getBest(MaskUtil.normalize(eid));
         if (daoEntry != null) {
             return daoEntry.get(SubKeys.RECORD);
         } else {
@@ -132,14 +94,15 @@ public class MultiTableMapCache implements IMapCache {
 
     // Returns a mapping corresponding to either the longest prefix match for both dstEid and srcEid,
     // if a SourceDest mapping exists, or to dstEid
-    private Object getMappingLpmSD(Eid srcEid, Eid dstEid, ILispDAO dao) {
-        Map<String, ?> daoEntry = getDaoEntryBest(dstEid, dao);
+    private Object getMappingLpmSD(Eid srcEid, Eid dstEid, ILispDAO mappingsDb) {
+        Map<String, ?> daoEntry = mappingsDb.getBest(MaskUtil.normalize(dstEid));
         if (daoEntry != null) {
             // try SrcDst eid lookup
             ILispDAO srcDstDao = (ILispDAO) daoEntry.get(SubKeys.LCAF_SRCDST);
-            if (srcDstDao != null) {
-                Object mapping = getMappingLpmEid(srcEid, srcDstDao);
-                if (mapping!= null) {
+            if (srcEid != null && srcDstDao != null) {
+                // make sure that srcEid is a prefix, not an IP and binary
+                Object mapping = getMappingLpmEid(LispAddressUtil.asIpPrefixBinaryEid(srcEid), srcDstDao);
+                if (mapping !=  null) {
                     return mapping;
                 }
             }
@@ -171,6 +134,15 @@ public class MultiTableMapCache implements IMapCache {
         return getMappingLpmSD(srcEid, dstEid, table);
     }
 
+    @Override
+    public Eid getWidestNegativeMapping(Eid key) {
+        ILispDAO table = getVniTable(key);
+        if (table == null) {
+            return null;
+        }
+        return table.getWidestNegativePrefix(key);
+    }
+
     public void removeMapping(Eid eid, boolean overwrite) {
         Eid key = MaskUtil.normalize(eid);
         ILispDAO table = getVniTable(key);
@@ -183,9 +155,12 @@ public class MultiTableMapCache implements IMapCache {
             if (db != null) {
                 db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key),
                         SubKeys.RECORD);
+                db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key),
+                        SubKeys.REGDATE);
             }
         } else {
             table.removeSpecific(key, SubKeys.RECORD);
+            table.removeSpecific(key, SubKeys.REGDATE);
         }
     }
 
@@ -262,20 +237,20 @@ public class MultiTableMapCache implements IMapCache {
 
     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
     // This method returns the DAO associated to a dst or creates it if it doesn't exist.
-    private ILispDAO getOrInstantiateSDInnerDao(Eid address, ILispDAO dao) {
+    private ILispDAO getOrInstantiateSDInnerDao(Eid address, ILispDAO mappingsDb) {
         Eid dstKey = SourceDestKeyHelper.getDstBinary(address);
-        ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, SubKeys.LCAF_SRCDST);
+        ILispDAO srcDstDao = (ILispDAO) mappingsDb.getSpecific(dstKey, SubKeys.LCAF_SRCDST);
         if (srcDstDao == null) {
             // inserts nested table for source
-            srcDstDao = dao.putNestedTable(dstKey, SubKeys.LCAF_SRCDST);
+            srcDstDao = mappingsDb.putNestedTable(dstKey, SubKeys.LCAF_SRCDST);
         }
         return srcDstDao;
     }
 
     // SrcDst LCAFs are stored in a 2-tier DAO with dst having priority over src.
     // This method returns the DAO associated to dst or null if it doesn't exist.
-    private ILispDAO getSDInnerDao(Eid address, ILispDAO dao) {
-        return (ILispDAO) dao.getSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
+    private ILispDAO getSDInnerDao(Eid address, ILispDAO mappingsDb) {
+        return (ILispDAO) mappingsDb.getSpecific(SourceDestKeyHelper.getDstBinary(address), SubKeys.LCAF_SRCDST);
     }
 
     public String printMappings() {
@@ -345,9 +320,9 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
-            srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<Object>(subKey, data));
+            srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<>(subKey, data));
         } else {
-            table.put(key, new MappingEntry<Object>(subKey, data));
+            table.put(key, new MappingEntry<>(subKey, data));
         }
     }