Bug 9116: Move notification logic to MappingSystem
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
index a6b7c5e53cfe733d23dd5e84e7b390a8bbefb6ef..1fcfe00c96a90b3f084016359fd7f5700171403f 100644 (file)
@@ -8,22 +8,29 @@
 
 package org.opendaylight.lispflowmapping.implementation;
 
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.ListenableFuture;
 import java.util.List;
 import org.apache.commons.lang3.tuple.MutablePair;
 import org.apache.commons.lang3.tuple.Pair;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
-import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
+import org.opendaylight.lispflowmapping.config.ConfigIni;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
 import org.opendaylight.lispflowmapping.implementation.util.LispNotificationHelper;
-import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapRequestResultHandler;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
+import org.opendaylight.lispflowmapping.interfaces.lisp.ISmrNotificationListener;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
+import org.opendaylight.lispflowmapping.lisp.type.LispMessage;
 import org.opendaylight.lispflowmapping.lisp.util.LispAddressStringifier;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.AddMapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapNotify;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.GotMapReply;
@@ -34,6 +41,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.Ma
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MappingKeepAlive;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.OdlLispProtoListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.RequestMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrReplyMapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.XtrRequestMapping;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.eid.container.Eid;
@@ -49,14 +57,18 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLi
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotifyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapReplyInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
-        IMapNotifyHandler, OdlLispProtoListener, AutoCloseable {
-    protected static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
+        IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
+    private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
+    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
+            LISPFLOWMAPPING_ENTITY_NAME);
+
+
+    private static final Logger LOG = LoggerFactory.getLogger(LispMappingService.class);
+    private final ClusterSingletonServiceProvider clusterSingletonService;
 
     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
@@ -76,11 +88,12 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
 
     public LispMappingService(final NotificationService notificationService,
             final IMappingService mappingService,
-            final OdlLispSbService odlLispService) {
+            final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) {
 
         this.notificationService = notificationService;
         this.mapService = mappingService;
         this.lispSB = odlLispService;
+        this.clusterSingletonService = clusterSingletonService;
         LOG.debug("LispMappingService Module constructed!");
     }
 
@@ -88,15 +101,15 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         return this.smr;
     }
 
-    public void setShouldUseSmr(boolean smr) {
-        this.smr = smr;
+    public void setShouldUseSmr(boolean shouldUseSmr) {
+        this.smr = shouldUseSmr;
         if (mapServer != null) {
-            mapServer.setSubscriptionService(smr);
+            mapServer.setSubscriptionService(shouldUseSmr);
         }
         if (mapResolver != null) {
-            mapResolver.setSubscriptionService(smr);
+            mapResolver.setSubscriptionService(shouldUseSmr);
         }
-        ConfigIni.getInstance().setSmr(smr);
+        ConfigIni.getInstance().setSmr(shouldUseSmr);
     }
 
     public NotificationService getNotificationService() {
@@ -106,17 +119,20 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
     public void initialize() {
         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
         mapServer = new MapServer(mapService, smr, this, notificationService);
+        this.clusterSingletonService.registerClusterSingletonService(this);
+        mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
         LOG.info("LISP (RFC6830) Mapping Service init finished");
     }
 
     public void basicInit() {
         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
         mapServer = new MapServer(mapService, smr, this, notificationService);
+        mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
     }
 
     public MapReply handleMapRequest(MapRequest request) {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("DAO: Retrieving mapping for {}",
+            LOG.debug("LISP: Retrieving mapping for {}",
                     LispAddressStringifier.getString(request.getEidItem().get(0).getEid()));
         }
 
@@ -137,7 +153,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
 
     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("DAO: Adding mapping for {}",
+            LOG.debug("LISP: Adding mapping for {}",
                     LispAddressStringifier.getString(mapRegister.getMappingRecordItem().get(0)
                             .getMappingRecord().getEid()));
         }
@@ -164,7 +180,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
         if (result != null && result.getLeft() != null) {
             MapNotify mapNotify = result.getLeft();
-            List <TransportAddress> rlocs = result.getRight();
+            List<TransportAddress> rlocs = result.getRight();
             if (rlocs == null) {
                 TransportAddressBuilder tab = new TransportAddressBuilder();
                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
@@ -189,7 +205,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
             getLispSB().sendMapReply(smrib.build());
         } else {
-            LOG.warn("got null map reply");
+            LOG.debug("handleMapRequest: Got null MapReply");
         }
     }
 
@@ -218,10 +234,11 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
         for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
             final Eid eid = eidLispAddress.getEid();
+            final XtrId xtrId = cacheMetadata.getXtrId();
             final Long timestamp = cacheMetadata.getTimestamp();
             LOG.debug("Update map registration for eid {} with timestamp {}", LispAddressStringifier.getString(eid),
                     timestamp);
-            mapService.updateMappingRegistration(MappingOrigin.Southbound, eid, timestamp);
+            mapService.refreshMappingRegistration(eid, xtrId, timestamp);
         }
     }
 
@@ -240,15 +257,15 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
     }
 
     @Override
-    public void handleSMR(MapRequest smr, Rloc subscriber) {
+    public void handleSMR(MapRequest smrMapRequest, Rloc subscriber) {
         if (LOG.isDebugEnabled()) {
-            LOG.debug("Sending SMR to {} with Source-EID {} and EID Record {}",
+            LOG.debug("Sending SMR Map-Request to {} with Source-EID {} and EID Record {} (reversed)",
                     LispAddressStringifier.getString(subscriber),
-                    LispAddressStringifier.getString(smr.getSourceEid().getEid()),
-                    LispAddressStringifier.getString(smr.getEidItem().get(0).getEid()));
+                    LispAddressStringifier.getString(smrMapRequest.getSourceEid().getEid()),
+                    LispAddressStringifier.getString(smrMapRequest.getEidItem().get(0).getEid()));
         }
         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
-        smrib.setMapRequest(new MapRequestBuilder(smr).build());
+        smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
         getLispSB().sendMapRequest(smrib.build());
 
@@ -259,7 +276,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
     }
 
-    public void destroy() {
+    private void destroy() {
         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
         mapResolver = null;
         mapServer = null;
@@ -268,5 +285,24 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
     @Override
     public void close() throws Exception {
         destroy();
+        clusterSingletonService.close();
+    }
+
+    @Override
+    public void instantiateServiceInstance() {
+        mapService.setIsMaster(true);
+    }
+
+    @Override
+    public ListenableFuture<Void> closeServiceInstance() {
+        if (mapService != null) {
+            mapService.setIsMaster(false);
+        }
+        return Futures.<Void>immediateFuture(null);
+    }
+
+    @Override
+    public ServiceGroupIdentifier getIdentifier() {
+        return SERVICE_GROUP_IDENTIFIER;
     }
 }