Move HashMapDb to its own bundle
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
index 310b9f43e4ce1c8af7f2424e73facbc62a163e06..ae807440110f07c513675b4ccb06b8b37f76af0c 100644 (file)
@@ -18,6 +18,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Random;
+import java.util.Map.Entry;
 
 import org.apache.commons.lang3.BooleanUtils;
 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
@@ -26,6 +27,7 @@ import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil
 import org.opendaylight.lispflowmapping.implementation.util.DAOMappingUtil;
 import org.opendaylight.lispflowmapping.implementation.util.LispAFIConvertor;
 import org.opendaylight.lispflowmapping.implementation.util.MapNotifyBuilderHelper;
+import org.opendaylight.lispflowmapping.inmemorydb.HashMapDb;
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
@@ -33,19 +35,23 @@ import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceRLOCGroup;
 import org.opendaylight.lispflowmapping.interfaces.dao.MappingServiceSubscriberRLOC;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRegister;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.MapRequest;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecordBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidtolocatorrecords.EidToLocatorRecord;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.LcafKeyValue;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispsimpleaddress.primitiveaddress.DistinguishedName;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.locatorrecords.LocatorRecord;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.mapnotifymessage.MapNotifyBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRloc;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.ItrRlocBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequest.SourceEidBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.maprequestnotification.MapRequestBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.LispAFIAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRegister;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.MapRequest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidtolocatorrecords.EidToLocatorRecordBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.LispAddressContainer;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafKeyValue;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispaddress.lispaddresscontainer.address.LcafSourceDest;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.lispsimpleaddress.primitiveaddress.DistinguishedName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.locatorrecords.LocatorRecord;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.mapnotifymessage.MapNotifyBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.ItrRloc;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.ItrRlocBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.SourceEidBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequestnotification.MapRequestBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -106,9 +112,10 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
         builder.setSmr(true);
         builder.setSmrInvoked(false);
 
-        builder.setEidRecord(new ArrayList<org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.eidrecords.EidRecord>());
+        builder.setEidRecord(new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord>());
         LispAddressContainer container = eidRecord.getLispAddressContainer();
-        builder.getEidRecord().add(new EidRecordBuilder().setMask((short) eidRecord.getMaskLength()).setLispAddressContainer(container).build());
+        short mask = (short) eidRecord.getMaskLength();
+        builder.getEidRecord().add(new EidRecordBuilder().setMask(mask).setLispAddressContainer(container).build());
 
         builder.setItrRloc(new ArrayList<ItrRloc>());
         builder.getItrRloc().add(new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getLocalAddress())).build());
@@ -137,26 +144,9 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
                     }
                 }
                 boolean mappingChanged = saveRlocs(eidRecord, smr);
-
                 if (smr && mappingChanged) {
-                    HashSet<MappingServiceSubscriberRLOC> subscribers = getSubscribers(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
-                    if (subscribers != null) {
-                        MapRequest mapRequest = buildSMR(eidRecord);
-                        LOG.trace("Built SMR packet: " + mapRequest.toString());
-                        for (MappingServiceSubscriberRLOC rloc : subscribers) {
-                            if (rloc.timedOut()) {
-                                LOG.trace("Lazy removing expired subscriber entry " + rloc.toString());
-                                subscribers.remove(rloc);
-                            } else {
-                                callback.handleSMR(mapRequest, rloc.getSrcRloc());
-                            }
-                        }
-                        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(),
-                                eidRecord.getMaskLength());
-                        dao.put(key, new MappingEntry<HashSet<MappingServiceSubscriberRLOC>>(SUBSCRIBERS_SUBKEY, subscribers));
-                    }
+                    sendSmrs(eidRecord, callback);
                 }
-
             }
             if (!failed) {
                 MapNotifyBuilder builder = new MapNotifyBuilder();
@@ -173,12 +163,10 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
     }
 
     public boolean saveRlocs(EidToLocatorRecord eidRecord, boolean checkForChanges) {
-        List<MappingServiceRLOCGroup> oldLocators = null, newLocators = null;
-        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(), eidRecord.getMaskLength());
         Map<String, MappingServiceRLOCGroup> rlocGroups = new HashMap<String, MappingServiceRLOCGroup>();
         if (eidRecord.getLocatorRecord() != null) {
             for (LocatorRecord locatorRecord : eidRecord.getLocatorRecord()) {
-                String subkey = getLocatorKey(locatorRecord);
+                String subkey = getAddressKey(locatorRecord.getLispAddressContainer().getAddress());
                 if (!rlocGroups.containsKey(subkey)) {
                     rlocGroups.put(subkey, new MappingServiceRLOCGroup(eidRecord.getRecordTtl(), eidRecord.getAction(), eidRecord.isAuthoritative()));
                 }
@@ -189,30 +177,60 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
         for (String subkey : rlocGroups.keySet()) {
             entries.add(new MappingEntry<>(subkey, rlocGroups.get(subkey)));
         }
-        if (checkForChanges) {
-            oldLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
-        }
-        dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
-        if (checkForChanges) {
-            newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
-            if (!newLocators.equals(oldLocators)) {
-                return true;
+
+        if (eidRecord.getLispAddressContainer().getAddress() instanceof LcafSourceDest) {
+            Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> oldMapping= null, newMapping = null;
+            LispAFIAddress srcAddr = getSrcForLcafSrcDst(eidRecord.getLispAddressContainer());
+            LispAFIAddress dstAddr = getDstForLcafSrcDst(eidRecord.getLispAddressContainer());
+            short srcMask = getSrcMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
+            short dstMask = getDstMaskForLcafSrcDst(eidRecord.getLispAddressContainer());
+
+            if (checkForChanges) {
+                oldMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
+            }
+            IMappingServiceKey dstKey = MappingServiceKeyUtil.generateMappingServiceKey(dstAddr, dstMask);
+            ILispDAO srcDstDao = (ILispDAO) dao.getSpecific(dstKey, LCAF_SRCDST_SUBKEY);
+            if (srcDstDao == null) {
+                srcDstDao = new HashMapDb();
+                dao.put(dstKey, new MappingEntry<>(LCAF_SRCDST_SUBKEY, srcDstDao));
+            }
+            IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(srcAddr, srcMask);
+            srcDstDao.put(srcKey, entries.toArray(new MappingEntry[entries.size()]));
+            if (checkForChanges) {
+                newMapping = DAOMappingUtil.getMappingExact(srcAddr, dstAddr, srcMask, dstMask, dao);
+                if (!newMapping.getValue().equals(oldMapping.getValue())) {
+                    return true;
+                }
+            }
+        } else {
+            List<MappingServiceRLOCGroup> oldLocators = null, newLocators = null;
+            if (checkForChanges) {
+                oldLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
+            }
+            IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(eidRecord.getLispAddressContainer(),
+                    eidRecord.getMaskLength());
+            dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
+            if (checkForChanges) {
+                newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
+                if (!newLocators.equals(oldLocators)) {
+                    return true;
+                }
             }
         }
         return false;
     }
 
-    private String getLocatorKey(LocatorRecord locatorRecord) {
-        if (locatorRecord.getLispAddressContainer().getAddress() instanceof LcafKeyValue) {
-            LcafKeyValue keyVal = (LcafKeyValue) locatorRecord.getLispAddressContainer().getAddress();
-            if (keyVal.getKey().getPrimitiveAddress() instanceof DistinguishedName) {
-                return ((DistinguishedName) keyVal.getKey().getPrimitiveAddress()).getDistinguishedName();
+    private String getAddressKey(Address address) {
+        if (address instanceof LcafKeyValue) {
+            LcafKeyValue keyVal = (LcafKeyValue) address;
+            if (keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress() instanceof DistinguishedName) {
+                return ((DistinguishedName) keyVal.getLcafKeyValueAddressAddr().getKey().getPrimitiveAddress()).getDistinguishedNameAddress().getDistinguishedName();
             }
         }
         if (shouldOverwrite()) {
             return ADDRESS_SUBKEY;
         } else {
-            return String.valueOf(locatorRecord.getLispAddressContainer().getAddress().hashCode());
+            return String.valueOf(address.hashCode());
         }
     }
 
@@ -221,13 +239,106 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
     }
 
     public void removeAuthenticationKey(LispAddressContainer address, int maskLen) {
-        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
-        dao.removeSpecific(key, PASSWORD_SUBKEY);
+        if (address.getAddress() instanceof LcafSourceDest) {
+            ILispDAO srcDstDao = getSrcDstInnerDao(address, maskLen);
+            if (srcDstDao != null) {
+                IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(getSrcForLcafSrcDst(address),
+                        getSrcMaskForLcafSrcDst(address));
+                srcDstDao.removeSpecific(srcKey, PASSWORD_SUBKEY);
+            }
+        } else {
+            IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
+            dao.removeSpecific(key, PASSWORD_SUBKEY);
+        }
     }
 
     public void addAuthenticationKey(LispAddressContainer address, int maskLen, String key) {
         IMappingServiceKey mappingServiceKey = MappingServiceKeyUtil.generateMappingServiceKey(address, maskLen);
-        dao.put(mappingServiceKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
+        if (address.getAddress() instanceof LcafSourceDest) {
+            IMappingServiceKey srcKey = MappingServiceKeyUtil.generateMappingServiceKey(getSrcForLcafSrcDst(address),
+                    getSrcMaskForLcafSrcDst(address));
+            ILispDAO srcDstDao = getOrInstantiateSrcDstInnerDao(address, maskLen);
+            srcDstDao.put(srcKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
+        } else {
+            dao.put(mappingServiceKey, new MappingEntry<String>(PASSWORD_SUBKEY, key));
+        }
+    }
+
+    public void removeMapping(LispAddressContainer address, int maskLen, boolean smr, IMapNotifyHandler callback) {
+        Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> mapping;
+        ILispDAO db;
+        if (address.getAddress() instanceof LcafSourceDest) {
+            db = getSrcDstInnerDao(address, maskLen);
+            LispAFIAddress srcAddr = getSrcForLcafSrcDst(address);
+            short srcMask = getSrcMaskForLcafSrcDst(address);
+            mapping = DAOMappingUtil.getMappingForEid(srcAddr, srcMask, db);
+        } else {
+            db = dao;
+            mapping = DAOMappingUtil.getMappingForEid(LispAFIConvertor.toAFI(address), maskLen, db);
+        }
+        if (smr) {
+            HashSet<MappingServiceSubscriberRLOC> subscribers = getSubscribers(address, maskLen);
+            // mapping is removed before first SMR is sent to avoid inconsistent replies
+            removeMappingRlocs(mapping, db);
+            handleSmr(new EidToLocatorRecordBuilder().setLispAddressContainer(address).
+                    setMaskLength((short) maskLen).build(), subscribers, callback);
+            db.removeSpecific(mapping.getKey(), SUBSCRIBERS_SUBKEY);
+        } else {
+            removeMappingRlocs(mapping, db);
+            db.removeSpecific(mapping.getKey(), SUBSCRIBERS_SUBKEY);
+        }
+    }
+
+    private void removeMappingRlocs(Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> mapping, ILispDAO db) {
+        for (MappingServiceRLOCGroup group : mapping.getValue()) {
+            for (LocatorRecord record : group.getRecords()) {
+                db.removeSpecific(mapping.getKey(), getAddressKey(record.getLispAddressContainer().getAddress()));
+            }
+        }
+    }
+
+    private void sendSmrs(EidToLocatorRecord record, IMapNotifyHandler callback) {
+        LispAddressContainer eid = record.getLispAddressContainer();
+        HashSet<MappingServiceSubscriberRLOC> subscribers;
+
+        subscribers = getSubscribers(eid, record.getMaskLength());
+        handleSmr(record, subscribers, callback);
+
+        // For SrcDst LCAF also send SMRs to Dst prefix
+        if (eid.getAddress() instanceof LcafSourceDest) {
+            LispAddressContainer dstAddr = LispAFIConvertor.toContainer(getDstForLcafSrcDst(eid));
+            short dstMask = getDstMaskForLcafSrcDst(eid);
+            subscribers = getSubscribers(dstAddr, dstMask);
+            EidToLocatorRecord newRecord = new EidToLocatorRecordBuilder().setAction(record.getAction()).
+                    setAuthoritative(record.isAuthoritative()).setLocatorRecord(record.getLocatorRecord()).
+                    setMapVersion(record.getMapVersion()).setRecordTtl(record.getRecordTtl()).
+                    setLispAddressContainer(dstAddr).setMaskLength(dstMask).build();
+            handleSmr(newRecord, subscribers, callback);
+        }
+    }
+
+    private void handleSmr(EidToLocatorRecord record, HashSet<MappingServiceSubscriberRLOC> subscribers,
+            IMapNotifyHandler callback) {
+        if (subscribers == null) {
+            return;
+        }
+        MapRequest mapRequest = buildSMR(record);
+        LOG.trace("Built SMR packet: " + mapRequest.toString());
+        for (MappingServiceSubscriberRLOC rloc : subscribers) {
+            if (rloc.timedOut()) {
+                LOG.trace("Lazy removing expired subscriber entry " + rloc.toString());
+                subscribers.remove(rloc);
+            } else {
+                try {
+                    callback.handleSMR(mapRequest, rloc.getSrcRloc());
+                } catch (Exception e) {
+                    LOG.error("Errors encountered while handling SMR:" + e.getStackTrace());
+                }
+            }
+        }
+        IMappingServiceKey key = MappingServiceKeyUtil.generateMappingServiceKey(record.getLispAddressContainer(),
+                record.getMaskLength());
+        dao.put(key, new MappingEntry<HashSet<MappingServiceSubscriberRLOC>>(SUBSCRIBERS_SUBKEY, subscribers));
     }
 
     public boolean shouldOverwrite() {