Bug 8112: IT: send SMR-invoked Map-Request on SMR
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / lisp / MapResolver.java
index b735cb8a4c3e14eb02c92a238d7afb8129305099..c17c2c79c441177067666676e10df2eb54b57d52 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Contextream, Inc. and others.  All rights reserved.
+ * Copyright (c) 2014, 2017 Contextream, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -9,18 +9,23 @@
 package org.opendaylight.lispflowmapping.implementation.lisp;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
 import com.google.common.collect.Sets;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Set;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
-import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
+import org.opendaylight.lispflowmapping.interfaces.dao.Subscriber;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
+import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
+import org.opendaylight.lispflowmapping.interfaces.lisp.SmrEvent;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.type.MappingData;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressUtil;
+import org.opendaylight.lispflowmapping.lisp.util.MappingRecordUtil;
 import org.opendaylight.lispflowmapping.lisp.util.SourceDestKeyHelper;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4Afi;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.lisp.address.types.rev151105.Ipv4PrefixAfi;
@@ -43,7 +48,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.ei
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecord;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.locatorrecords.LocatorRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecord.Action;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.container.MappingRecordBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapping.record.list.MappingRecordItemBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.mapreplymessage.MapReplyBuilder;
@@ -56,17 +60,16 @@ import org.slf4j.LoggerFactory;
 public class MapResolver implements IMapResolverAsync {
     protected static final Logger LOG = LoggerFactory.getLogger(MapResolver.class);
 
-    private static final int TTL_RLOC_TIMED_OUT = 1;
-    private static final int TTL_NO_RLOC_KNOWN = 15;
-
     private IMappingService mapService;
     private boolean subscriptionService;
     private String elpPolicy;
     private IMapRequestResultHandler requestHandler;
     private boolean authenticate = true;
+    private ISmrNotificationListener smrNotificationListener;
+    private static final int TTL_DELETE_MAPPING = 0;
 
     public MapResolver(IMappingService mapService, boolean smr, String elpPolicy,
-            IMapRequestResultHandler requestHandler) {
+                       IMapRequestResultHandler requestHandler) {
         Preconditions.checkNotNull(mapService);
         this.subscriptionService = smr;
         this.mapService = mapService;
@@ -75,6 +78,7 @@ public class MapResolver implements IMapResolverAsync {
     }
 
     public void handleMapRequest(MapRequest request) {
+        LOG.trace("Map-Request received: {}", request);
         // SMRs and RLOC probes are directed towards xTRs and we're a Map-Resolver here, so ignore them
         if (request.isSmr() != null && request.isSmr()) {
             LOG.debug("Map-Resolver ignoring incoming SMR control message.");
@@ -84,6 +88,17 @@ public class MapResolver implements IMapResolverAsync {
             LOG.debug("Map-Resolver ignoring incoming RLOC probe control message.");
             return;
         }
+        if (request.isSmrInvoked()) {
+            LOG.debug("SMR-invoked request received.");
+            LOG.trace("Map-Request object: {}", request);
+            for (EidItem eidItem : request.getEidItem()) {
+                final SmrEvent event = new SmrEvent(
+                        subscriberListFromItrRlocs(request.getItrRloc(), request.getSourceEid().getEid()),
+                        eidItem.getEid(),
+                        request.getNonce());
+                smrNotificationListener.onSmrInvokedReceived(event);
+            }
+        }
         Eid srcEid = null;
         if (request.getSourceEid() != null) {
             srcEid = request.getSourceEid().getEid();
@@ -98,20 +113,24 @@ public class MapResolver implements IMapResolverAsync {
         final IpAddressBinary sourceRloc = request.getSourceRloc();
 
         for (EidItem eidRecord : request.getEidItem()) {
-            MappingRecord mapping = (MappingRecord) mapService.getMapping(srcEid,
-                    eidRecord.getEid());
-            if (mapping != null) {
-                if (itrRlocs != null && itrRlocs.size() != 0) {
-                    if (subscriptionService) {
-                        final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
-                        updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(), srcEid);
-                    }
-                    mapping = updateLocators(mapping, itrRlocs);
-                }
-                mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
+            MappingData mappingData = mapService.getMapping(srcEid, eidRecord.getEid());
+            MappingRecord mapping;
+            if (mappingData == null) {
+                mapping = mapService.addNegativeMapping(eidRecord.getEid()).getRecord();
             } else {
-                mapping = getNegativeMapping(eidRecord.getEid());
+                mapping = mappingData.getRecord();
+            }
+
+            if (itrRlocs != null && itrRlocs.size() != 0) {
+                if (subscriptionService) {
+                    final Rloc resolvedRloc = resolveRloc(itrRlocs, sourceRloc);
+                    updateSubscribers(resolvedRloc, eidRecord.getEid(), mapping.getEid(),
+                            srcEid, mapping.getRecordTtl());
+                }
+                mapping = updateLocators(mapping, itrRlocs);
             }
+            mapping = fixIfNotSDRequest(mapping, eidRecord.getEid());
+            mapping = fixTtlIfSmrInvoked(request, mapping);
             replyBuilder.getMappingRecordItem().add(new MappingRecordItemBuilder().setMappingRecord(mapping).build());
         }
         requestHandler.handleMapReply(replyBuilder.build());
@@ -167,29 +186,8 @@ public class MapResolver implements IMapResolverAsync {
         }
     }
 
-    private MappingRecord getNegativeMapping(Eid eid) {
-        MappingRecordBuilder recordBuilder = new MappingRecordBuilder();
-        recordBuilder.setAuthoritative(false);
-        recordBuilder.setMapVersion((short) 0);
-        recordBuilder.setEid(eid);
-        if (eid.getAddressType().equals(Ipv4PrefixBinaryAfi.class)
-                || eid.getAddressType().equals(Ipv6PrefixBinaryAfi.class)) {
-            Eid widestNegativePrefix = mapService.getWidestNegativePrefix(eid);
-            if (widestNegativePrefix != null) {
-                recordBuilder.setEid(widestNegativePrefix);
-            }
-        }
-        recordBuilder.setAction(Action.NativelyForward);
-        if (authenticate && mapService.getAuthenticationKey(eid) != null) {
-            recordBuilder.setRecordTtl(TTL_RLOC_TIMED_OUT);
-        } else {
-            recordBuilder.setRecordTtl(TTL_NO_RLOC_KNOWN);
-        }
-        return recordBuilder.build();
-    }
-
-    private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid) {
-        SubscriberRLOC subscriberRloc = new SubscriberRLOC(itrRloc, srcEid);
+    private void updateSubscribers(Rloc itrRloc, Eid reqEid, Eid mapEid, Eid srcEid, Integer recordTtl) {
+        Subscriber subscriber = new Subscriber(itrRloc, srcEid, Subscriber.recordTtlToSubscriberTime(recordTtl));
         Eid subscribedEid = mapEid;
 
         // If the eid in the matched mapping is SourceDest and the requested eid IS NOT then we subscribe itrRloc only
@@ -200,18 +198,18 @@ public class MapResolver implements IMapResolverAsync {
             subscribedEid = SourceDestKeyHelper.getDstBinary(mapEid);
         }
 
-        Set<SubscriberRLOC> subscribers = getSubscribers(subscribedEid);
+        Set<Subscriber> subscribers = getSubscribers(subscribedEid);
         if (subscribers == null) {
             subscribers = Sets.newConcurrentHashSet();
-        } else if (subscribers.contains(subscriberRloc)) {
-            // If there is an entry already for this subscriberRloc, remove it, so that it gets the new
-            // timestamp
-            subscribers.remove(subscriberRloc);
+        } else if (subscribers.contains(subscriber)) {
+            // If there is an entry already for this subscriber, remove it, so that it gets the new timestamp
+            subscribers.remove(subscriber);
         }
         if (LOG.isTraceEnabled()) {
-            LOG.trace("Adding new subscriber: " + LispAddressStringifier.getString(subscriberRloc.getSrcRloc()));
+            LOG.trace("Adding new subscriber {} for EID {}", subscriber.getString(),
+                    LispAddressStringifier.getString(subscribedEid));
         }
-        subscribers.add(subscriberRloc);
+        subscribers.add(subscriber);
         addSubscribers(subscribedEid, subscribers);
     }
 
@@ -225,7 +223,21 @@ public class MapResolver implements IMapResolverAsync {
         return mapping;
     }
 
+    // When an SMR-invoked Map-Request is asking for a mapping that is negative, it is most likely an attempt to delete
+    // that mapping.
+    private MappingRecord fixTtlIfSmrInvoked(MapRequest request, MappingRecord mapping) {
+        if (request.isSmrInvoked() && MappingRecordUtil.isNegativeMapping(mapping)) {
+            return new MappingRecordBuilder(mapping).setRecordTtl(TTL_DELETE_MAPPING).build();
+        }
+        return mapping;
+    }
+
     private boolean locatorsNeedFixing(List<LocatorRecord> locatorRecords) {
+        // no locators - no fixing needed ;)
+        if (locatorRecords == null) {
+            return false;
+        }
+
         for (LocatorRecord record : locatorRecords) {
             if (record.getRloc().getAddress() instanceof ExplicitLocatorPath) {
                 return true;
@@ -328,12 +340,20 @@ public class MapResolver implements IMapResolverAsync {
         return nextHop;
     }
 
+    private static List<Subscriber> subscriberListFromItrRlocs(List<ItrRloc> itrRlocs, Eid srcEid) {
+        List<Subscriber> subscriberList = Lists.newArrayList();
+        for (ItrRloc itrRloc : itrRlocs) {
+            subscriberList.add(new Subscriber(itrRloc.getRloc(), srcEid, Subscriber.DEFAULT_SUBSCRIBER_TIMEOUT));
+        }
+        return subscriberList;
+    }
+
     @SuppressWarnings("unchecked")
-    private Set<SubscriberRLOC> getSubscribers(Eid address) {
-        return (Set<SubscriberRLOC>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
+    private Set<Subscriber> getSubscribers(Eid address) {
+        return (Set<Subscriber>) mapService.getData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS);
     }
 
-    private void addSubscribers(Eid address, Set<SubscriberRLOC> subscribers) {
+    private void addSubscribers(Eid address, Set<Subscriber> subscribers) {
         mapService.addData(MappingOrigin.Southbound, address, SubKeys.SUBSCRIBERS, subscribers);
     }
 
@@ -351,4 +371,9 @@ public class MapResolver implements IMapResolverAsync {
     public void setShouldAuthenticate(boolean shouldAuthenticate) {
         this.authenticate = shouldAuthenticate;
     }
+
+    @Override
+    public void setSmrNotificationListener(ISmrNotificationListener smrNotificationListener) {
+        this.smrNotificationListener = smrNotificationListener;
+    }
 }