Bug 9116: SMR children of a prefix too
[lispflowmapping.git] / mappingservice / mapcache / src / main / java / org / opendaylight / lispflowmapping / mapcache / SimpleMapCache.java
index 93a80966d277c54c9288ca3312babd733ed1b169..4310e35cbd438c47b3a1ab9fad12bc1c38020965 100644 (file)
@@ -10,6 +10,8 @@ package org.opendaylight.lispflowmapping.mapcache;
 
 import java.util.AbstractMap.SimpleImmutableEntry;
 import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -39,23 +41,24 @@ public class SimpleMapCache implements ILispMapCache {
         this.dao = dao;
     }
 
-    private ILispDAO getVniTable(Eid eid) {
-        long vni = 0;
+    private long getVni(Eid eid) {
         if (eid.getVirtualNetworkId() == null) {
-            vni = 0;
+            return 0;
         } else {
-            vni = eid.getVirtualNetworkId().getValue();
+            return eid.getVirtualNetworkId().getValue();
         }
-        return (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
+    }
+
+    private ILispDAO getVniTable(Eid eid) {
+        return (ILispDAO) dao.getSpecific(getVni(eid), SubKeys.VNI);
+    }
+
+    private void removeVniTable(Eid eid) {
+        dao.removeSpecific(getVni(eid), SubKeys.VNI);
     }
 
     private ILispDAO getOrInstantiateVniTable(Eid eid) {
-        long vni = 0;
-        if (eid.getVirtualNetworkId() == null) {
-            vni = 0;
-        } else {
-            vni = eid.getVirtualNetworkId().getValue();
-        }
+        long vni = getVni(eid);
         ILispDAO table = (ILispDAO) dao.getSpecific(vni, SubKeys.VNI);
         if (table == null) {
             table = dao.putNestedTable(vni, SubKeys.VNI);
@@ -63,10 +66,10 @@ public class SimpleMapCache implements ILispMapCache {
         return table;
     }
 
-    private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO dao) {
-        ILispDAO table = (ILispDAO) dao.getSpecific(eid, SubKeys.XTRID_RECORDS);
+    private ILispDAO getOrInstantiateXtrIdTable(Eid eid, ILispDAO lispDAO) {
+        ILispDAO table = (ILispDAO) lispDAO.getSpecific(eid, SubKeys.XTRID_RECORDS);
         if (table == null) {
-            table = dao.putNestedTable(eid, SubKeys.XTRID_RECORDS);
+            table = lispDAO.putNestedTable(eid, SubKeys.XTRID_RECORDS);
         }
         return table;
     }
@@ -96,8 +99,8 @@ public class SimpleMapCache implements ILispMapCache {
 
     // 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, XtrId xtrId, ILispDAO dao) {
-        SimpleImmutableEntry<Eid, Map<String, ?>> daoEntry = dao.getBestPair(MaskUtil.normalize(eid));
+    private Object getMappingLpmEid(Eid eid, XtrId xtrId, ILispDAO lispDAO) {
+        SimpleImmutableEntry<Eid, Map<String, ?>> daoEntry = lispDAO.getBestPair(MaskUtil.normalize(eid));
         if (daoEntry != null) {
             if (xtrId != null) {
                 ILispDAO xtrIdTable = (ILispDAO) daoEntry.getValue().get(SubKeys.XTRID_RECORDS);
@@ -131,10 +134,10 @@ public class SimpleMapCache implements ILispMapCache {
     }
 
     // Returns the list of mappings stored in an xTR-ID DAO
-    private List<Object> getXtrIdMappingList(ILispDAO dao) {
-        if (dao != null) {
+    private List<Object> getXtrIdMappingList(ILispDAO lispDAO) {
+        if (lispDAO != null) {
             final List<Object> records = new ArrayList<>();
-            dao.getAll(new IRowVisitor() {
+            lispDAO.getAll(new IRowVisitor() {
                 public void visitRow(Object keyId, String valueKey, Object value) {
                     if (valueKey.equals(SubKeys.RECORD)) {
                         records.add(value);
@@ -162,14 +165,25 @@ public class SimpleMapCache implements ILispMapCache {
         return null;
     }
 
+    // Returns null for positive mappings, and 0/0 for empty cache.
+    @Override
     public Eid getWidestNegativeMapping(Eid eid) {
         ILispDAO table = getVniTable(eid);
         if (table == null) {
-            return null;
+            return MaskUtil.normalize(eid, (short) 0);
         }
         return table.getWidestNegativePrefix(MaskUtil.normalize(eid));
     }
 
+    @Override
+    public Eid getCoveringLessSpecific(Eid eid) {
+        ILispDAO table = getVniTable(eid);
+        if (table == null) {
+            return null;
+        }
+        return table.getCoveringLessSpecific(MaskUtil.normalize(eid));
+    }
+
     @Override
     public Eid getParentPrefix(Eid eid) {
         ILispDAO table = getVniTable(eid);
@@ -198,28 +212,32 @@ public class SimpleMapCache implements ILispMapCache {
     }
 
     @Override
-    public void removeMapping(Eid eid) {
+    public Set<Eid> getSubtree(Eid eid) {
         ILispDAO table = getVniTable(eid);
         if (table == null) {
-            return;
+            return Collections.emptySet();
         }
-
-        Eid key = MaskUtil.normalize(eid);
-        table.remove(key);
+        return table.getSubtree(eid);
     }
 
     @Override
-    public void removeMapping(Eid eid, XtrId xtrId) {
+    public void removeMapping(Eid eid) {
         ILispDAO table = getVniTable(eid);
         if (table == null) {
             return;
         }
+
         Eid key = MaskUtil.normalize(eid);
-        ILispDAO xtrIdTable = (ILispDAO) table.getSpecific(key, SubKeys.XTRID_RECORDS);
-        if (xtrIdTable == null) {
-            return;
+        table.remove(key);
+        if (table.isEmpty()) {
+            removeVniTable(eid);
         }
-        xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
+    }
+
+    @Override
+    public void removeMapping(Eid eid, XtrId xtrId) {
+        List<XtrId> xtrIds = Arrays.asList(xtrId);
+        removeXtrIdMappings(eid, xtrIds);
     }
 
     @Override
@@ -236,6 +254,12 @@ public class SimpleMapCache implements ILispMapCache {
         for (XtrId xtrId : xtrIds) {
             xtrIdTable.removeSpecific(xtrId, SubKeys.RECORD);
         }
+        if (xtrIdTable.isEmpty()) {
+            table.removeSpecific(key, SubKeys.XTRID_RECORDS);
+            if (table.isEmpty()) {
+                removeVniTable(eid);
+            }
+        }
     }
 
     @Override
@@ -247,7 +271,7 @@ public class SimpleMapCache implements ILispMapCache {
 
     @Override
     public Object getData(Eid eid, String subKey) {
-        ILispDAO table = getOrInstantiateVniTable(eid);
+        ILispDAO table = getVniTable(eid);
         if (table == null) {
             return null;
         }
@@ -257,12 +281,15 @@ public class SimpleMapCache implements ILispMapCache {
 
     @Override
     public void removeData(Eid eid, String subKey) {
-        ILispDAO table = getOrInstantiateVniTable(eid);
+        ILispDAO table = getVniTable(eid);
         if (table == null) {
             return;
         }
         Eid key = MaskUtil.normalize(eid);
         table.removeSpecific(key, subKey);
+        if (table.isEmpty()) {
+            removeVniTable(eid);
+        }
     }
 
     @Override