Bump upstreams
[lispflowmapping.git] / mappingservice / implementation / src / main / java / org / opendaylight / lispflowmapping / implementation / LispMappingService.java
index 1fcfe00c96a90b3f084016359fd7f5700171403f..aeaa3f4d12beae6b19317168643ef5f2ac8e565a 100644 (file)
@@ -5,15 +5,18 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.lispflowmapping.implementation;
 
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.List;
+import java.util.Set;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
 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.config.ConfigIni;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
@@ -22,14 +25,16 @@ 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.mdsal.binding.api.NotificationService;
+import org.opendaylight.mdsal.binding.api.NotificationService.CompositeListener;
+import org.opendaylight.mdsal.binding.api.RpcService;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.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;
@@ -39,7 +44,6 @@ 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.MapReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.MapRequest;
 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;
@@ -53,54 +57,80 @@ 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.rloc.container.Rloc;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.transport.address.TransportAddressBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapNotify;
 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.SendMapReply;
 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.SendMapRequest;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendMapRequestInputBuilder;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@Singleton
+@Component(service = {IFlowMapping.class, IMapRequestResultHandler.class, IMapNotifyHandler.class},
+        immediate = true, property = "type=default")
 public class LispMappingService implements IFlowMapping, IMapRequestResultHandler,
-        IMapNotifyHandler, OdlLispProtoListener, AutoCloseable, ClusterSingletonService {
-    private static final String LISPFLOWMAPPING_ENTITY_NAME = "lispflowmapping";
-    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(
-            LISPFLOWMAPPING_ENTITY_NAME);
-
+        IMapNotifyHandler, AutoCloseable, ClusterSingletonService {
+    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER =
+        new ServiceGroupIdentifier("lispflowmapping");
 
     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();
 
-    private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<MapReply>();
-    private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify =
-            new ThreadLocal<Pair<MapNotify, List<TransportAddress>>>();
-    private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest =
-            new ThreadLocal<Pair<MapRequest, TransportAddress>>();
-
-    private final OdlLispSbService lispSB;
+    // These are non-final for testing
+    private ThreadLocal<MapReply> tlsMapReply = new ThreadLocal<>();
+    private ThreadLocal<Pair<MapNotify, List<TransportAddress>>> tlsMapNotify = new ThreadLocal<>();
+    private ThreadLocal<Pair<MapRequest, TransportAddress>> tlsMapRequest = new ThreadLocal<>();
     private IMapResolverAsync mapResolver;
-    private IMapServerAsync mapServer;
+    private MapServer mapServer;
+    private SendMapRequest sendMapRequest;
+    private SendMapReply sendMapReply;
+    private SendMapNotify sendMapNotify;
 
     private final IMappingService mapService;
     private final NotificationService notificationService;
-
-    public LispMappingService(final NotificationService notificationService,
-            final IMappingService mappingService,
-            final OdlLispSbService odlLispService, final ClusterSingletonServiceProvider clusterSingletonService) {
-
-        this.notificationService = notificationService;
+    private final Registration listenerRegistration;
+    private final Registration cssRegistration;
+
+    @Inject
+    @Activate
+    public LispMappingService(@Reference final IMappingService mappingService,
+            @Reference final ClusterSingletonServiceProvider clusterSingletonService,
+            @Reference final RpcService rpcService, @Reference final NotificationService notificationService) {
         this.mapService = mappingService;
-        this.lispSB = odlLispService;
-        this.clusterSingletonService = clusterSingletonService;
-        LOG.debug("LispMappingService Module constructed!");
+        sendMapRequest = rpcService.getRpc(SendMapRequest.class);
+        sendMapReply = rpcService.getRpc(SendMapReply.class);
+        sendMapNotify = rpcService.getRpc(SendMapNotify.class);
+        this.notificationService = notificationService;
+
+        // initialize
+        listenerRegistration = notificationService.registerCompositeListener(new CompositeListener(Set.of(
+                new CompositeListener.Component<>(AddMapping.class, this::onAddMapping),
+                new CompositeListener.Component<>(GotMapNotify.class, this::onGotMapNotify),
+                new CompositeListener.Component<>(RequestMapping.class, this::onRequestMapping),
+                new CompositeListener.Component<>(GotMapReply.class, this::onGotMapReply),
+                new CompositeListener.Component<>(XtrRequestMapping.class, this::onXtrRequestMapping),
+                new CompositeListener.Component<>(XtrReplyMapping.class, this::onXtrReplyMapping),
+                new CompositeListener.Component<>(MappingKeepAlive.class, this::onMappingKeepAlive))));
+        mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
+        mapServer = new MapServer(mapService, smr, this, notificationService);
+        cssRegistration = clusterSingletonService.registerClusterSingletonService(this);
+        mapResolver.setSmrNotificationListener((ISmrNotificationListener) mapServer);
+        LOG.info("LISP (RFC6830) Mapping Service initialized");
     }
 
     public boolean shouldUseSmr() {
         return this.smr;
     }
 
+    @Override
     public void setShouldUseSmr(boolean shouldUseSmr) {
         this.smr = shouldUseSmr;
         if (mapServer != null) {
@@ -116,20 +146,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         return this.notificationService;
     }
 
-    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);
-    }
-
+    @Override
     public MapReply handleMapRequest(MapRequest request) {
         if (LOG.isDebugEnabled()) {
             LOG.debug("LISP: Retrieving mapping for {}",
@@ -144,13 +161,14 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
             SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
             smrib.setMapRequest(new MapRequestBuilder(tlsMapRequest.get().getLeft()).build());
             smrib.setTransportAddress(tlsMapRequest.get().getRight());
-            getLispSB().sendMapRequest(smrib.build());
+            sendMapRequest.invoke(smrib.build());
             return null;
         } else {
             return tlsMapReply.get();
         }
     }
 
+    @Override
     public Pair<MapNotify, List<TransportAddress>> handleMapRegister(MapRegister mapRegister) {
         if (LOG.isDebugEnabled()) {
             LOG.debug("LISP: Adding mapping for {}",
@@ -172,11 +190,11 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         SendMapNotifyInputBuilder smnib = new SendMapNotifyInputBuilder();
         smnib.setMapNotify(new MapNotifyBuilder(mapNotify).build());
         smnib.setTransportAddress(address);
-        getLispSB().sendMapNotify(smnib.build());
+        sendMapNotify.invoke(smnib.build());
     }
 
-    @Override
-    public void onAddMapping(AddMapping mapRegisterNotification) {
+    @VisibleForTesting
+    void onAddMapping(AddMapping mapRegisterNotification) {
         Pair<MapNotify, List<TransportAddress>> result = handleMapRegister(mapRegisterNotification.getMapRegister());
         if (result != null && result.getLeft() != null) {
             MapNotify mapNotify = result.getLeft();
@@ -184,7 +202,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
             if (rlocs == null) {
                 TransportAddressBuilder tab = new TransportAddressBuilder();
                 tab.setIpAddress(mapRegisterNotification.getTransportAddress().getIpAddress());
-                tab.setPort(new PortNumber(LispMessage.PORT_NUM));
+                tab.setPort(new PortNumber(LispMessage.PORT_NUMBER));
                 sendMapNotify(mapNotify, tab.build());
             } else {
                 for (TransportAddress ta : rlocs) {
@@ -196,43 +214,43 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         }
     }
 
-    @Override
-    public void onRequestMapping(RequestMapping mapRequestNotification) {
+    @VisibleForTesting
+    void onRequestMapping(RequestMapping mapRequestNotification) {
         MapReply mapReply = handleMapRequest(mapRequestNotification.getMapRequest());
         if (mapReply != null) {
             SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder();
-            smrib.setMapReply((new MapReplyBuilder(mapReply).build()));
+            smrib.setMapReply(new MapReplyBuilder(mapReply).build());
             smrib.setTransportAddress(mapRequestNotification.getTransportAddress());
-            getLispSB().sendMapReply(smrib.build());
+            sendMapReply.invoke(smrib.build());
         } else {
             LOG.debug("handleMapRequest: Got null MapReply");
         }
     }
 
-    @Override
-    public void onGotMapReply(GotMapReply notification) {
+    @VisibleForTesting
+    void onGotMapReply(GotMapReply notification) {
         LOG.debug("Received GotMapReply notification, ignoring");
     }
 
-    @Override
-    public void onGotMapNotify(GotMapNotify notification) {
+    @VisibleForTesting
+    void onGotMapNotify(GotMapNotify notification) {
         LOG.debug("Received GotMapNotify notification, ignoring");
     }
 
-    @Override
-    public void onXtrRequestMapping(XtrRequestMapping notification) {
+    @VisibleForTesting
+    void onXtrRequestMapping(XtrRequestMapping notification) {
         LOG.debug("Received XtrRequestMapping notification, ignoring");
     }
 
-    @Override
-    public void onXtrReplyMapping(XtrReplyMapping notification) {
+    @VisibleForTesting
+    void onXtrReplyMapping(XtrReplyMapping notification) {
         LOG.debug("Received XtrReplyMapping notification, ignoring");
     }
 
-    @Override
-    public void onMappingKeepAlive(MappingKeepAlive notification) {
+    @VisibleForTesting
+    void onMappingKeepAlive(MappingKeepAlive notification) {
         final MapRegisterCacheMetadata cacheMetadata = notification.getMapRegisterCacheMetadata();
-        for (EidLispAddress eidLispAddress : cacheMetadata.getEidLispAddress()) {
+        for (EidLispAddress eidLispAddress : cacheMetadata.nonnullEidLispAddress().values()) {
             final Eid eid = eidLispAddress.getEid();
             final XtrId xtrId = cacheMetadata.getXtrId();
             final Long timestamp = cacheMetadata.getTimestamp();
@@ -242,10 +260,6 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         }
     }
 
-    private OdlLispSbService getLispSB() {
-        return lispSB;
-    }
-
     @Override
     public void handleMapReply(MapReply reply) {
         tlsMapReply.set(reply);
@@ -253,7 +267,7 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
 
     @Override
     public void handleMapNotify(MapNotify notify, List<TransportAddress> rlocs) {
-        tlsMapNotify.set(new MutablePair<MapNotify, List<TransportAddress>>(notify, rlocs));
+        tlsMapNotify.set(new MutablePair<>(notify, rlocs));
     }
 
     @Override
@@ -267,25 +281,30 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
         SendMapRequestInputBuilder smrib = new SendMapRequestInputBuilder();
         smrib.setMapRequest(new MapRequestBuilder(smrMapRequest).build());
         smrib.setTransportAddress(LispNotificationHelper.getTransportAddressFromRloc(subscriber));
-        getLispSB().sendMapRequest(smrib.build());
-
+        sendMapRequest.invoke(smrib.build());
     }
 
     @Override
     public void handleNonProxyMapRequest(MapRequest mapRequest, TransportAddress transportAddress) {
-        tlsMapRequest.set(new MutablePair<MapRequest, TransportAddress>(mapRequest, transportAddress));
+        tlsMapRequest.set(new MutablePair<>(mapRequest, transportAddress));
     }
 
     private void destroy() {
         LOG.info("LISP (RFC6830) Mapping Service is destroyed!");
         mapResolver = null;
-        mapServer = null;
+        if (mapServer != null) {
+            mapServer.close();
+            mapServer = null;
+        }
     }
 
+    @Deactivate
+    @PreDestroy
     @Override
     public void close() throws Exception {
         destroy();
-        clusterSingletonService.close();
+        cssRegistration.close();
+        listenerRegistration.close();
     }
 
     @Override