Bug 4005: Fix SMRs
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
index f378412d74b0791ef68db0441f14c747497c9755..b12d2107c80e40ccce57f27bd00c0c513339884e 100644 (file)
@@ -8,8 +8,6 @@
 
 package org.opendaylight.lispflowmapping.implementation.lisp;
 
-import java.io.PrintWriter;
-import java.io.StringWriter;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketException;
@@ -23,6 +21,7 @@ import java.util.Random;
 import java.util.Map.Entry;
 
 import org.apache.commons.lang3.BooleanUtils;
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.opendaylight.lispflowmapping.implementation.authentication.LispAuthenticationUtil;
 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
 import org.opendaylight.lispflowmapping.implementation.dao.MappingServiceKeyUtil;
@@ -30,6 +29,7 @@ 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.implementation.util.MaskUtil;
 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
 import org.opendaylight.lispflowmapping.interfaces.dao.IMappingServiceKey;
 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
@@ -39,7 +39,6 @@ import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
 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;
@@ -105,7 +104,7 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
         return null;
     }
 
-    private static MapRequest buildSMR(EidToLocatorRecord eidRecord) {
+    private static MapRequestBuilder buildSMR(LispAddressContainer srcEid) {
         MapRequestBuilder builder = new MapRequestBuilder();
         builder.setAuthoritative(false);
         builder.setMapDataPresent(false);
@@ -115,19 +114,17 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
         builder.setSmrInvoked(false);
 
         builder.setEidRecord(new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.eidrecords.EidRecord>());
-        LispAddressContainer container = eidRecord.getLispAddressContainer();
-        short mask = (short) eidRecord.getMaskLength();
-        builder.getEidRecord().add(new EidRecordBuilder().setMask(mask).setLispAddressContainer(container).build());
-
+        // The address stored in the SMR's EID record is used as Source EID in the SMR-invoked Map-Request. To
+        // ensure consistent behavior it is set to the value used to originally request a given mapping
+        builder.getEidRecord().add(new EidRecordBuilder()
+                    .setMask((short)MaskUtil.getMaxMask(LispAFIConvertor.toAFI(srcEid)))
+                    .setLispAddressContainer(srcEid).build());
         builder.setItrRloc(new ArrayList<ItrRloc>());
         builder.getItrRloc().add(new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor.toContainer(getLocalAddress())).build());
-
         builder.setMapReply(null);
         builder.setNonce(new Random().nextLong());
 
-        // XXX For now we set source EID to queried EID...
-        builder.setSourceEid(new SourceEidBuilder().setLispAddressContainer(container).build());
-        return builder.build();
+        return builder;
     }
 
     public void handleMapRegister(MapRegister mapRegister, boolean smr, IMapNotifyHandler callback) {
@@ -200,9 +197,8 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
             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;
-                }
+                return (newMapping.getValue() == null) ? oldMapping.getValue() != null :
+                                                        !newMapping.getValue().equals(oldMapping.getValue());
             }
         } else {
             List<MappingServiceRLOCGroup> oldLocators = null, newLocators = null;
@@ -214,9 +210,7 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
             dao.put(key, entries.toArray(new MappingEntry[entries.size()]));
             if (checkForChanges) {
                 newLocators = DAOMappingUtil.getLocatorsByEidToLocatorRecord(eidRecord, dao, shouldIterateMask());
-                if (!newLocators.equals(oldLocators)) {
-                    return true;
-                }
+                return (newLocators == null) ? oldLocators != null : !newLocators.equals(oldLocators);
             }
         }
         return false;
@@ -292,6 +286,9 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
     }
 
     private void removeMappingRlocs(Entry<IMappingServiceKey, List<MappingServiceRLOCGroup>> mapping, ILispDAO db) {
+        if (mapping == null || mapping.getValue() == null) {
+            return;
+        }
         for (MappingServiceRLOCGroup group : mapping.getValue()) {
             for (LocatorRecord record : group.getRecords()) {
                 db.removeSpecific(mapping.getKey(), getAddressKey(record.getLispAddressContainer().getAddress()));
@@ -324,19 +321,19 @@ public class MapServer extends AbstractLispComponent implements IMapServerAsync
         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);
+        MapRequestBuilder mrb = buildSMR(subscribers.iterator().next().getSrcEid());
+        LOG.trace("Built SMR packet: " + mrb.build().toString());
+        for (MappingServiceSubscriberRLOC subscriber : subscribers) {
+            if (subscriber.timedOut()) {
+                LOG.trace("Lazy removing expired subscriber entry " + subscriber.toString());
+                subscribers.remove(subscriber);
             } else {
                 try {
-                    callback.handleSMR(mapRequest, rloc.getSrcRloc());
+                    // The Source EID in a SMR is used as EID record in the SMR-invoked Map-Request.
+                    mrb.setSourceEid(new SourceEidBuilder().setLispAddressContainer(record.getLispAddressContainer()).build());
+                    callback.handleSMR(mrb.build(), subscriber.getSrcRloc());
                 } catch (Exception e) {
-                    StringWriter sw = new StringWriter();
-                    e.printStackTrace(new PrintWriter(sw));
-                    LOG.error("Errors encountered while handling SMR:" + sw.toString());
+                    LOG.error("Errors encountered while handling SMR:" + ExceptionUtils.getStackTrace(e));
                 }
             }
         }