Bug 6529: Remove registration date when deleting mapping
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / MultiTableMapCache.java
index 1ba18b55e2c6b49ef6f28f9cb0a4dfb187ead23e..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) {
@@ -69,7 +68,7 @@ public class MultiTableMapCache implements IMapCache {
         ILispDAO table = getOrInstantiateVniTable(key);
 
         if (eid.getAddress() instanceof SourceDestKey) {
-            Eid srcKey = SourceDestKeyHelper.getSrc(eid);
+            Eid srcKey = SourceDestKeyHelper.getSrcBinary(eid);
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(eid, table);
             srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.REGDATE, new Date(System.currentTimeMillis())));
             srcDstDao.put(srcKey, new MappingEntry<>(SubKeys.RECORD, value));
@@ -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;
                 }
             }
@@ -162,8 +125,8 @@ public class MultiTableMapCache implements IMapCache {
 
         // a map-request for an actual SrcDst LCAF, ignore src eid
         if (dstEid.getAddress() instanceof SourceDestKey) {
-            Eid srcAddr = SourceDestKeyHelper.getSrc(dstEid);
-            Eid dstAddr = SourceDestKeyHelper.getDst(dstEid);
+            Eid srcAddr = SourceDestKeyHelper.getSrcBinary(dstEid);
+            Eid dstAddr = SourceDestKeyHelper.getDstBinary(dstEid);
             return getMappingLpmSD(srcAddr, dstAddr, table);
         }
 
@@ -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);
@@ -181,11 +153,14 @@ public class MultiTableMapCache implements IMapCache {
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
-                db.removeSpecific(SourceDestKeyHelper.getSrc(key),
+                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);
         }
     }
 
@@ -195,7 +170,7 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getOrInstantiateSDInnerDao(key, table);
-            srcDstDao.put(SourceDestKeyHelper.getSrc(key), new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
+            srcDstDao.put(SourceDestKeyHelper.getSrcBinary(key), new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
         } else {
             table.put(key, new MappingEntry<>(SubKeys.AUTH_KEY, authKey));
         }
@@ -225,7 +200,7 @@ public class MultiTableMapCache implements IMapCache {
                 // NOTE: this is an exact match, not a longest prefix match
                 ILispDAO srcDstDao = getSDInnerDao(eid, table);
                 if (srcDstDao != null) {
-                    return getAuthKeyLpm(SourceDestKeyHelper.getSrc(eid), srcDstDao);
+                    return getAuthKeyLpm(SourceDestKeyHelper.getSrcBinary(eid), srcDstDao);
                 }
                 return null;
             } else {
@@ -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) {
-        Eid dstKey = SourceDestKeyHelper.getDst(address);
-        ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, SubKeys.LCAF_SRCDST);
+    private ILispDAO getOrInstantiateSDInnerDao(Eid address, ILispDAO mappingsDb) {
+        Eid dstKey = SourceDestKeyHelper.getDstBinary(address);
+        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.getDst(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.getSrc(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));
         }
     }
 
@@ -361,7 +336,7 @@ public class MultiTableMapCache implements IMapCache {
 
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO srcDstDao = getSDInnerDao(key, table);
-            return srcDstDao.getSpecific(SourceDestKeyHelper.getSrc(key), subKey);
+            return srcDstDao.getSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
         } else {
             return table.getSpecific(key, subKey);
         }
@@ -377,7 +352,7 @@ public class MultiTableMapCache implements IMapCache {
         if (key.getAddress() instanceof SourceDestKey) {
             ILispDAO db = getSDInnerDao(key, table);
             if (db != null) {
-                db.removeSpecific(SourceDestKeyHelper.getSrc(key), subKey);
+                db.removeSpecific(SourceDestKeyHelper.getSrcBinary(key), subKey);
             }
         } else {
             table.removeSpecific(key, subKey);