Clustering - adding to LispMappingService. 73/40473/18
authorJozef Gloncak <jgloncak@cisco.com>
Fri, 17 Jun 2016 06:11:35 +0000 (08:11 +0200)
committerLorand Jakab <lojakab@cisco.com>
Thu, 21 Jul 2016 18:19:13 +0000 (13:19 -0500)
Added code for cluster aware behaviour to LispMappingService.

Change-Id: I9e6cd6e806dd1e2c3656a3b4ed3b973a85868f68
Signed-off-by: Jozef Gloncak <jgloncak@cisco.com>
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/LispMappingService.java
mappingservice/implementation/src/main/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServer.java
mappingservice/implementation/src/main/resources/org/opendaylight/blueprint/mappingservice.xml
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/LispMappingServiceTest.java
mappingservice/implementation/src/test/java/org/opendaylight/lispflowmapping/implementation/lisp/MapServerTest.java

index e2014a0f08cd2ea2997bcf767723ef76b86a8a48..a704f21f05f5329a33bf46fe02be6f4cd52679b6 100644 (file)
@@ -12,6 +12,7 @@ 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.controller.md.sal.common.api.clustering.EntityOwnershipService;
 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapResolver;
 import org.opendaylight.lispflowmapping.implementation.lisp.MapServer;
@@ -51,12 +52,14 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.SendM
 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.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
 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);
+    private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
 
     private volatile boolean smr = ConfigIni.getInstance().smrIsSet();
     private volatile String elpPolicy = ConfigIni.getInstance().getElpPolicy();
@@ -76,11 +79,12 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
 
     public LispMappingService(final NotificationService notificationService,
             final IMappingService mappingService,
-            final OdlLispSbService odlLispService) {
+            final OdlLispSbService odlLispService, final EntityOwnershipService entityOnwershipService) {
 
         this.notificationService = notificationService;
         this.mapService = mappingService;
         this.lispSB = odlLispService;
+        clusterNodeModulSwitcher = new ClusterNodeModulSwitcherImpl(entityOnwershipService);
         LOG.debug("LispMappingService Module constructed!");
     }
 
@@ -105,13 +109,13 @@ public class LispMappingService implements IFlowMapping, IMapRequestResultHandle
 
     public void initialize() {
         mapResolver = new MapResolver(mapService, smr, elpPolicy, this);
-        mapServer = new MapServer(mapService, smr, this, notificationService);
+        mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
         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);
+        mapServer = new MapServer(mapService, smr, this, notificationService, clusterNodeModulSwitcher);
     }
 
     public MapReply handleMapRequest(MapRequest request) {
index 88318bd7e8a3117c08a9e8c0e37b87f832c677dd..b88d6f0ec58eec70c7e02d7e3739e84d83bbe5ff 100644 (file)
@@ -22,6 +22,7 @@ import java.util.Set;
 import org.apache.commons.lang3.BooleanUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
@@ -56,6 +57,7 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev15090
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingChanged;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.MappingOrigin;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.mappingservice.rev150906.OdlMappingserviceListener;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -64,21 +66,25 @@ import com.google.common.base.Preconditions;
 public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
 
     protected static final Logger LOG = LoggerFactory.getLogger(MapServer.class);
+    private final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
     private IMappingService mapService;
     private boolean subscriptionService;
     private IMapNotifyHandler notifyHandler;
     private NotificationService notificationService;
+    private ListenerRegistration<MapServer> mapServerListenerRegistration;
 
     public MapServer(IMappingService mapService, boolean subscriptionService,
-            IMapNotifyHandler notifyHandler, NotificationService notificationService) {
+                     IMapNotifyHandler notifyHandler, NotificationService notificationService,
+                     final ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher) {
         Preconditions.checkNotNull(mapService);
         this.mapService = mapService;
         this.subscriptionService = subscriptionService;
         this.notifyHandler = notifyHandler;
         this.notificationService = notificationService;
         if (notificationService != null) {
-            notificationService.registerNotificationListener(this);
+            mapServerListenerRegistration = notificationService.registerNotificationListener(this);
         }
+        this.clusterNodeModulSwitcher = clusterNodeModulSwitcher;
     }
 
     @Override
@@ -171,7 +177,9 @@ public class MapServer implements IMapServerAsync, OdlMappingserviceListener {
     @Override
     public void onMappingChanged(MappingChanged notification) {
         if (subscriptionService) {
-            sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+            if (clusterNodeModulSwitcher.isMaster()) {
+                sendSmrs(notification.getMappingRecord(), getSubscribers(notification.getMappingRecord().getEid()));
+            }
             if (notification.getChangeType().equals(MappingChange.Removed)) {
                 removeSubscribers(notification.getMappingRecord().getEid());
             }
index 0f6b5a753539ac44737261f54170c612db49c36f..bde613ffff7b5fd1cdd6227d7155a67a8e011ed3 100644 (file)
@@ -12,6 +12,8 @@
     interface="org.opendaylight.controller.md.sal.binding.api.NotificationService" />
   <reference id="lispDAO"
     interface="org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO" />
+  <reference id="entityOwnershipService"
+     interface="org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService" />
 
   <odl:rpc-service id="odlLispSbService"
     interface="org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.sb.rev150904.OdlLispSbService" />
@@ -42,6 +44,7 @@
     <argument ref="notificationService" />
     <argument ref="mappingService" />
     <argument ref="odlLispSbService" />
+    <argument ref="entityOwnershipService" />
   </bean>
   <service ref="lispMappingService"
     interface="org.opendaylight.lispflowmapping.interfaces.lisp.IFlowMapping"
@@ -53,4 +56,4 @@
     interface="org.opendaylight.lispflowmapping.interfaces.lisp.IMapNotifyHandler"
     odl:type="default" />
   <odl:notification-listener ref="lispMappingService" />
-</blueprint>
\ No newline at end of file
+</blueprint>
index 3c53ebbea6f73f6c4e736615a221ed03905b7b64..4bdc6d439567685ada0be83b9f119b4ab4e50be8 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.lispflowmapping.implementation;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNull;
 
+import com.google.common.base.Optional;
 import com.google.common.collect.Lists;
 
 import java.lang.reflect.Field;
@@ -27,6 +28,9 @@ import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapResolverAsync;
 import org.opendaylight.lispflowmapping.interfaces.lisp.IMapServerAsync;
 import org.opendaylight.lispflowmapping.interfaces.mappingservice.IMappingService;
@@ -86,12 +90,13 @@ public class LispMappingServiceTest {
     private final NotificationService notificationService = Mockito.mock(NotificationService.class);
     private final IMappingService mappingService = Mockito.mock(IMappingService.class);
     private final OdlLispSbService odlLispSbService = Mockito.mock(OdlLispSbService.class);
+    private final EntityOwnershipService entityOwnershipSrvice = Mockito.mock(EntityOwnershipService.class);
 
     @InjectMocks
     private LispMappingService lispMappingService = new LispMappingService(
             notificationService,
             mappingService,
-            odlLispSbService);
+            odlLispSbService, entityOwnershipSrvice);
 
     private static final byte[] IPV4_BYTES_1 =       new byte[] {1, 2, 3, 0};
     private static final byte[] IPV4_BYTES_2 =       new byte[] {1, 2, 4, 0};
@@ -193,6 +198,8 @@ public class LispMappingServiceTest {
         Mockito.when(mapRegister.getMappingRecordItem())
                 .thenReturn(Lists.newArrayList(MAPPING_RECORD_ITEM_BUILDER.build()));
         Mockito.when(tlsMapNotifyMock.get()).thenReturn(getDefaultMapNotifyPair());
+        Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+                (new EntityOwnershipState(true, true)));
 
         lispMappingService.onAddMapping(addMapping);
         Mockito.verify(odlLispSbService, Mockito.times(2)).sendMapNotify(Mockito.argThat(new TransportAddressMatch()));
@@ -220,6 +227,8 @@ public class LispMappingServiceTest {
                 .setMapNotify(new org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105
                         .mapnotifymessage.MapNotifyBuilder().setKeyId((short) 1).build())
                 .setTransportAddress(TRANSPORT_ADDRESS);
+        Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+                (new EntityOwnershipState(true, true)));
 
         lispMappingService.onAddMapping(addMapping);
         Mockito.verify(odlLispSbService).sendMapNotify(smnib.build());
@@ -240,6 +249,8 @@ public class LispMappingServiceTest {
         Mockito.when(requestMapping.getTransportAddress()).thenReturn(TRANSPORT_ADDRESS_1);
         Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapReplyMock.get()).thenReturn(mapReply);
+        Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+                (new EntityOwnershipState(true, true)));
 
         // result
         final SendMapReplyInputBuilder smrib = new SendMapReplyInputBuilder()
@@ -264,6 +275,8 @@ public class LispMappingServiceTest {
         Mockito.when(mapRequest.getEidItem()).thenReturn(Lists.newArrayList(EID_ITEM_BUILDER.build()));
         Mockito.when(tlsMapReplyMock.get()).thenReturn(null);
 
+        Mockito.when(entityOwnershipSrvice.getOwnershipState(Mockito.any(Entity.class))).thenReturn(Optional.of
+                (new EntityOwnershipState(true, true)));
         lispMappingService.onRequestMapping(requestMapping);
         Mockito.verifyZeroInteractions(odlLispSbService);
     }
index a20034517cf430c3c63164af259166a1ff8606af..939a0c22b7684ba669cbe7a236ee1ebff00d145c 100644 (file)
@@ -26,6 +26,7 @@ import org.mockito.Mockito;
 import org.mockito.Spy;
 import org.mockito.runners.MockitoJUnitRunner;
 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
+import org.opendaylight.lispflowmapping.clustering.ClusterNodeModulSwitcherImpl;
 import org.opendaylight.lispflowmapping.implementation.config.ConfigIni;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
 import org.opendaylight.lispflowmapping.interfaces.dao.SubscriberRLOC;
@@ -65,6 +66,8 @@ public class MapServerTest {
     @Mock private static IMappingService mapService;
     @Mock private static IMapNotifyHandler notifyHandler;
     @Mock private static NotificationService notificationService;
+    @Mock private static ClusterNodeModulSwitcherImpl clusterNodeModulSwitcher;
+
     @Spy private static Set<SubscriberRLOC> subscriberSetMock_1 = new HashSet<>();
     @Spy private static Set<SubscriberRLOC> subscriberSetMock_2 = new HashSet<>();
     @Spy private static Set<SubscriberRLOC> subscriberSetMock_3 = new HashSet<>();
@@ -139,7 +142,7 @@ public class MapServerTest {
 
     @Before
     public void init() throws NoSuchFieldException, IllegalAccessException  {
-        mapServer = new MapServer(mapService, true, notifyHandler, notificationService);
+        mapServer = new MapServer(mapService, true, notifyHandler, notificationService, clusterNodeModulSwitcher);
         subscriberSetMock_1.add(SUBSCRIBER_RLOC_1);
         subscriberSetMock_1.add(SUBSCRIBER_RLOC_2);
         subscriberSetMock_2.add(SUBSCRIBER_RLOC_3);