Make some improvements
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapServer.java
index f281bd896ce118a819520ab2f1dcdb6c823170eb..924c1c4347f2a974fbd6371783fc654166fc0bb4 100644 (file)
@@ -49,13 +49,20 @@ import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MapNotifyBuilderHelper;
 import org.opendaylight.lispflowmapping.lisp.util.MapRequestUtil;
 import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
+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.inet.types.rev130715.PortNumber;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.lisp.address.Address;
 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.inet.binary.types.rev160303.IpAddressBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv4PrefixBinaryBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinary;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.binary.address.types.rev160504.augmented.lisp.address.address.Ipv6PrefixBinaryBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRegister;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.SiteId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.EidBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItem;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.list.EidItemBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapnotifymessage.MapNotifyBuilder;
@@ -75,7 +82,7 @@ import org.slf4j.LoggerFactory;
 
 public class MapServer implements IMapServerAsync, OdlMappingserviceListener, ISmrNotificationListener {
 
-    protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
+    private static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
     private static final byte[] ALL_ZEROES_XTR_ID = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,0};
     private IMappingService mapService;
     private boolean subscriptionService;
@@ -135,7 +142,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
                     // subscribers of the NEW mapping below (since the EIDs are different, the result of
                     // mappingChanged() will be true, and then send an SMR to all subscribers with the EID of the NEW
                     // mapping only.
-                    Set<Subscriber> oldMappingSubscribers = getSubscribers(oldMapping.getEid());
+                    Set<Subscriber> oldMappingSubscribers = mapService.getSubscribers(oldMapping.getEid());
                     if (oldMappingSubscribers != null) {
                         subscribers.addAll(oldMappingSubscribers);
                         LoggingUtil.logSubscribers(LOG, oldMapping.getEid(), subscribers);
@@ -153,7 +160,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
                         LOG.debug("Mapping update occured for {} SMRs will be sent for its subscribers.",
                                 LispAddressStringifier.getString(eid));
                     }
-                    Set<Subscriber> newMappingSubscribers = getSubscribers(eid);
+                    Set<Subscriber> newMappingSubscribers = mapService.getSubscribers(eid);
                     if (oldMapping != null && !oldMapping.getEid().equals(eid)) {
                         newMappingSubscribers = addParentSubscribers(eid, newMappingSubscribers);
                     }
@@ -275,7 +282,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
         // For SrcDst LCAF also send SMRs to Dst prefix
         if (eid.getAddress() instanceof SourceDestKey) {
             Eid dstAddr = SourceDestKeyHelper.getDstBinary(eid);
-            Set<Subscriber> dstSubs = getSubscribers(dstAddr);
+            Set<Subscriber> dstSubs = mapService.getSubscribers(dstAddr);
             sendSmrs(dstAddr, dstSubs);
         }
     }
@@ -290,18 +297,13 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
         scheduler.scheduleSmrs(mrb, subscribers.iterator());
     }
 
-    @SuppressWarnings("unchecked")
-    private Set<Subscriber> getSubscribers(Eid address) {
-        return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
-    }
-
     private Set<Subscriber> addParentSubscribers(Eid eid, Set<Subscriber> subscribers) {
         Eid parentPrefix = mapService.getParentPrefix(eid);
         if (parentPrefix == null) {
             return subscribers;
         }
 
-        Set<Subscriber> parentSubscribers = getSubscribers(parentPrefix);
+        Set<Subscriber> parentSubscribers = mapService.getSubscribers(parentPrefix);
         if (parentSubscribers != null) {
             if (subscribers != null) {
                 subscribers.addAll(parentSubscribers);
@@ -312,10 +314,6 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
         return subscribers;
     }
 
-    private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
-        mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
-    }
-
     private static InetAddress getLocalAddress() {
         try {
             Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
@@ -360,7 +358,7 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
         private final Map<Eid, Map<Subscriber, ScheduledFuture<?>>> eidFutureMap = Maps.newConcurrentMap();
 
         void scheduleSmrs(MapRequestBuilder mrb, Iterator<Subscriber> subscribers) {
-            final Eid srcEid = mrb.getSourceEid().getEid();
+            final Eid srcEid = fixSrcEidMask(mrb.getSourceEid().getEid());
             cancelExistingFuturesForEid(srcEid);
 
             final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = Maps.newConcurrentMap();
@@ -388,21 +386,39 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
         void smrReceived(SmrEvent event) {
             final List<Subscriber> subscriberList = event.getSubscriberList();
             for (Subscriber subscriber : subscriberList) {
-                LOG.trace("SMR-invoked event, EID {}, subscriber {}", LispAddressStringifier.getString(event.getEid()),
-                        subscriber.getString());
+                if (LOG.isTraceEnabled()) {
+                    LOG.trace("SMR-invoked event, EID {}, subscriber {}",
+                            LispAddressStringifier.getString(event.getEid()),
+                            subscriber.getString());
+                    LOG.trace("eidFutureMap: {}", eidFutureMap);
+                }
                 final Map<Subscriber, ScheduledFuture<?>> subscriberFutureMap = eidFutureMap.get(event.getEid());
                 if (subscriberFutureMap != null) {
                     final ScheduledFuture<?> future = subscriberFutureMap.get(subscriber);
                     if (future != null && !future.isCancelled()) {
                         future.cancel(true);
-                        LOG.debug("SMR-invoked MapRequest received, scheduled task for subscriber {}, EID {} with"
-                                + " nonce {} has been cancelled", subscriber.getString(),
-                                LispAddressStringifier.getString(event.getEid()), event.getNonce());
+                        if (LOG.isDebugEnabled()) {
+                            LOG.debug("SMR-invoked MapRequest received, scheduled task for subscriber {}, EID {} with"
+                                    + " nonce {} has been cancelled", subscriber.getString(),
+                                    LispAddressStringifier.getString(event.getEid()), event.getNonce());
+                        }
                         subscriberFutureMap.remove(subscriber);
+                    } else {
+                        if (future == null) {
+                            LOG.trace("No outstanding SMR tasks for EID {}, subscriber {}",
+                                    LispAddressStringifier.getString(event.getEid()), subscriber.getString());
+                        } else {
+                            LOG.trace("Future {} is cancelled", future);
+                        }
                     }
                     if (subscriberFutureMap.isEmpty()) {
                         eidFutureMap.remove(event.getEid());
                     }
+                } else {
+                    if (LOG.isTraceEnabled()) {
+                        LOG.trace("No outstanding SMR tasks for EID {}",
+                                LispAddressStringifier.getString(event.getEid()));
+                    }
                 }
             }
         }
@@ -422,6 +438,26 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener, IS
             }
         }
 
+        /*
+         * See https://bugs.opendaylight.org/show_bug.cgi?id=8469#c1 why this is necessary.
+         *
+         * TL;DR  The sourceEid field in the MapRequestBuilder object will be serialized to a packet on the wire, and
+         * a Map-Request can't store the prefix length in the source EID.
+         *
+         * Since we store all prefixes as binary internally, we only care about and fix those address types.
+         */
+        private Eid fixSrcEidMask(Eid eid) {
+            Address address = eid.getAddress();
+            if (address instanceof Ipv4PrefixBinary) {
+                return new EidBuilder(eid).setAddress(new Ipv4PrefixBinaryBuilder((Ipv4PrefixBinary) address)
+                        .setIpv4MaskLength(MaskUtil.IPV4_MAX_MASK).build()).build();
+            } else if (address instanceof Ipv6PrefixBinary) {
+                return new EidBuilder(eid).setAddress(new Ipv6PrefixBinaryBuilder((Ipv6PrefixBinary) address)
+                        .setIpv6MaskLength(MaskUtil.IPV6_MAX_MASK).build()).build();
+            }
+            return eid;
+        }
+
         private final class CancellableRunnable implements Runnable {
             private MapRequestBuilder mrb;
             private Subscriber subscriber;