Bug 6536: Allow propagation of SB notifications on slaves
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
index e2014a0f08cd2ea2997bcf767723ef76b86a8a48..f3e57b3a669de7124b940629222ba1fac3df9190 100644 (file)
@@ -8,6 +8,8 @@
 
 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;
@@ -16,14 +18,18 @@ import org.opendaylight.lispflowmapping.implementation.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.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;
@@ -49,14 +55,19 @@ 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.rev130715.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 {
+        IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
+    public static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
+    public static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
+            LISPFLOWMAPPING_ENTITY_NAME);
+
+
     protected 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 +87,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!");
     }
 
@@ -106,6 +118,7 @@ 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);
         LOG.info("LISP (RFC6830) Mapping Service init finished");
     }
 
@@ -164,7 +177,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());
@@ -268,5 +281,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;
     }
 }