ClusterSingletonService cleaning FRM/FRS 50/44850/2
authorAndrej Leitner <andrej.leitner@pantheon.tech>
Tue, 30 Aug 2016 13:16:51 +0000 (15:16 +0200)
committerAndrej Leitner <andrej.leitner@pantheon.sk>
Tue, 13 Sep 2016 13:07:09 +0000 (13:07 +0000)
Change-Id: I7c9a31ccab5b7b79b318cc7b59742aa5652e41f8
Signed-off-by: Andrej Leitner <andrej.leitner@pantheon.tech>
applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastership.java
applications/forwardingrules-manager/src/main/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManager.java
applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManagerTest.java [new file with mode: 0644]
applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipTest.java [new file with mode: 0644]
applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/clustering/DeviceMastership.java
applications/forwardingrules-sync/src/main/java/org/opendaylight/openflowplugin/applications/frsync/impl/clustering/DeviceMastershipManager.java
applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/clustering/DeviceMastershipManagerTest.java
applications/forwardingrules-sync/src/test/java/org/opendaylight/openflowplugin/applications/frsync/impl/clustering/DeviceMastershipTest.java

index fc4117814e89a798e54a2206d24ed53e23c16080..54360a005dee373773f0d9fc2ff5f98da867413a 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.openflowplugin.applications.frm.impl;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -20,17 +21,18 @@ import org.slf4j.LoggerFactory;
 /**
  * Service (per device) for registration in singleton provider.
  */
-public class DeviceMastership implements ClusterSingletonService {
+public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
     private final NodeId nodeId;
     private final ServiceGroupIdentifier identifier;
-    private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+    private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
     private boolean deviceMastered;
 
-    public DeviceMastership(final NodeId nodeId) {
+    public DeviceMastership(final NodeId nodeId, final ClusterSingletonServiceProvider clusterSingletonService) {
         this.nodeId = nodeId;
         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
         this.deviceMastered = false;
+        clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
     }
 
     @Override
@@ -51,16 +53,19 @@ public class DeviceMastership implements ClusterSingletonService {
         return identifier;
     }
 
-    public boolean isDeviceMastered() {
-        return deviceMastered;
-    }
-
-    public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
-        this.clusterSingletonServiceRegistration = registration;
+    @Override
+    public void close() {
+        if (clusterSingletonServiceRegistration != null) {
+            try {
+                clusterSingletonServiceRegistration.close();
+            } catch (Exception e) {
+                LOG.error("FRM cluster service close fail: {} {}", nodeId.getValue(), e);
+            }
+        }
     }
 
-    public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
-        return clusterSingletonServiceRegistration;
+    public boolean isDeviceMastered() {
+        return deviceMastered;
     }
 
 }
index 1530e46d9145de48ff831d36022faccba505ceba..2627cf6344dc99d9d353e44761387337ee19a10f 100644 (file)
@@ -8,9 +8,9 @@
 
 package org.opendaylight.openflowplugin.applications.frm.impl;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -28,29 +28,26 @@ public class DeviceMastershipManager {
     }
 
     public void onDeviceConnected(final NodeId nodeId) {
-        final DeviceMastership mastership = new DeviceMastership(nodeId);
-        final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
-        mastership.setClusterSingletonServiceRegistration(registration);
+        final DeviceMastership mastership = new DeviceMastership(nodeId, clusterSingletonService);
         deviceMasterships.put(nodeId, mastership);
-        LOG.debug("FRS service registered for: {}", nodeId.getValue());
+        LOG.debug("FRM service registered for: {}", nodeId.getValue());
     }
 
-
     public void onDeviceDisconnected(final NodeId nodeId) {
         final DeviceMastership mastership = deviceMasterships.remove(nodeId);
-        final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
-        if (registration != null) {
-            try {
-                registration.close();
-            } catch (Exception e) {
-                LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
-            }
+        if (mastership != null) {
+            mastership.close();
         }
-        LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
+        LOG.debug("FRM service unregistered for: {}", nodeId.getValue());
     }
 
     public boolean isDeviceMastered(final NodeId nodeId) {
         return deviceMasterships.get(nodeId) != null && deviceMasterships.get(nodeId).isDeviceMastered();
     }
 
+    @VisibleForTesting
+    ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
+        return deviceMasterships;
+    }
+
 }
diff --git a/applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManagerTest.java b/applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipManagerTest.java
new file mode 100644 (file)
index 0000000..870eb6b
--- /dev/null
@@ -0,0 +1,71 @@
+/**
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.frm.impl;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Matchers;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+/**
+ * Test for {@link DeviceMastershipManager}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceMastershipManagerTest {
+    private static final NodeId NODE_ID = new NodeId("testNode");
+    private DeviceMastershipManager deviceMastershipManager;
+    @Mock
+    private ClusterSingletonServiceRegistration registration;
+    @Mock
+    private ClusterSingletonServiceProvider clusterSingletonService;
+
+    @Before
+    public void setUp() throws Exception {
+        deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService);
+        Mockito.when(clusterSingletonService.registerClusterSingletonService(Matchers.<ClusterSingletonService>any()))
+                .thenReturn(registration);
+    }
+
+    @Test
+    public void testOnDeviceConnectedAndDisconnected() throws Exception {
+        // no context
+        Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+        // create context - register
+        deviceMastershipManager.onDeviceConnected(NODE_ID);
+        DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
+        Assert.assertNotNull(serviceInstance);
+        Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
+        // destroy context - unregister
+        deviceMastershipManager.onDeviceDisconnected(NODE_ID);
+        Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+        Mockito.verify(registration).close();
+    }
+
+    @Test
+    public void testIsDeviceMasteredOrSlaved() {
+        // no context
+        Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
+        deviceMastershipManager.onDeviceConnected(NODE_ID);
+        // is master
+        deviceMastershipManager.getDeviceMasterships().get(NODE_ID).instantiateServiceInstance();
+        Assert.assertTrue(deviceMastershipManager.isDeviceMastered(NODE_ID));
+        // is not master
+        deviceMastershipManager.getDeviceMasterships().get(NODE_ID).closeServiceInstance();
+        Assert.assertFalse(deviceMastershipManager.isDeviceMastered(NODE_ID));
+    }
+
+}
\ No newline at end of file
diff --git a/applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipTest.java b/applications/forwardingrules-manager/src/test/java/org/opendaylight/openflowplugin/applications/frm/impl/DeviceMastershipTest.java
new file mode 100644 (file)
index 0000000..7beee01
--- /dev/null
@@ -0,0 +1,49 @@
+/**
+ * Copyright (c) 2016 Pantheon Technologies s.r.o. 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.frm.impl;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+
+/**
+ * Test for {@link DeviceMastership}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class DeviceMastershipTest {
+    private static final NodeId NODE_ID = new NodeId("testNode");
+    private DeviceMastership deviceMastership;
+
+    @Mock
+    private DeviceMastershipManager deviceMastershipManager;
+
+    @Before
+    public void setUp() throws Exception {
+        deviceMastership = new DeviceMastership(NODE_ID, Mockito.mock(ClusterSingletonServiceProvider.class));
+    }
+
+    @Test
+    public void testInstantiateServiceInstance() {
+        deviceMastership.instantiateServiceInstance();
+        Assert.assertTrue(deviceMastership.isDeviceMastered());
+    }
+
+    @Test
+    public void testCloseServiceInstance() {
+        deviceMastership.closeServiceInstance();
+        Assert.assertFalse(deviceMastership.isDeviceMastered());
+    }
+
+}
\ No newline at end of file
index 211b4f40dc0165ecd93b36b8c01a415784622a56..2d76bf384333e8f1fd2b1356597ffb1850eb4b68 100644 (file)
@@ -11,6 +11,7 @@ package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
@@ -21,19 +22,22 @@ import org.slf4j.LoggerFactory;
 /**
  * {@link ClusterSingletonService} clusterSingletonServiceRegistration per connected device.
  */
-public class DeviceMastership implements ClusterSingletonService {
+public class DeviceMastership implements ClusterSingletonService, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(DeviceMastership.class);
     private final NodeId nodeId;
     private final ServiceGroupIdentifier identifier;
     private final ReconciliationRegistry reconciliationRegistry;
-    private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+    private final ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
     private boolean deviceMastered;
 
-    public DeviceMastership(final NodeId nodeId, final ReconciliationRegistry reconciliationRegistry) {
+    public DeviceMastership(final NodeId nodeId,
+                            final ReconciliationRegistry reconciliationRegistry,
+                            final ClusterSingletonServiceProvider clusterSingletonService) {
         this.nodeId = nodeId;
         this.identifier = ServiceGroupIdentifier.create(nodeId.getValue());
         this.reconciliationRegistry = reconciliationRegistry;
         this.deviceMastered = false;
+        clusterSingletonServiceRegistration = clusterSingletonService.registerClusterSingletonService(this);
     }
 
     @Override
@@ -56,16 +60,19 @@ public class DeviceMastership implements ClusterSingletonService {
         return identifier;
     }
 
-    public boolean isDeviceMastered() {
-        return deviceMastered;
-    }
-
-    public void setClusterSingletonServiceRegistration(final ClusterSingletonServiceRegistration registration) {
-        this.clusterSingletonServiceRegistration = registration;
+    @Override
+    public void close() {
+        if (clusterSingletonServiceRegistration != null) {
+            try {
+                clusterSingletonServiceRegistration.close();
+            } catch (Exception e) {
+                LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
+            }
+        }
     }
 
-    public ClusterSingletonServiceRegistration getClusterSingletonServiceRegistration() {
-        return clusterSingletonServiceRegistration;
+    public boolean isDeviceMastered() {
+        return deviceMastered;
     }
 
 }
index 06532ad3c2f821268a972c5a5d63b650dd8d41c8..4e7cef661308f39bacb08348ad765581fe074a4f 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -11,7 +11,6 @@ package org.opendaylight.openflowplugin.applications.frsync.impl.clustering;
 import com.google.common.annotations.VisibleForTesting;
 import java.util.concurrent.ConcurrentHashMap;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.slf4j.Logger;
@@ -33,23 +32,15 @@ public class DeviceMastershipManager {
     }
 
     public void onDeviceConnected(final NodeId nodeId) {
-        final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry);
-        final ClusterSingletonServiceRegistration registration = clusterSingletonService.registerClusterSingletonService(mastership);
-        mastership.setClusterSingletonServiceRegistration(registration);
+        final DeviceMastership mastership = new DeviceMastership(nodeId, reconciliationRegistry, clusterSingletonService);
         deviceMasterships.put(nodeId, mastership);
         LOG.debug("FRS service registered for: {}", nodeId.getValue());
     }
 
-
     public void onDeviceDisconnected(final NodeId nodeId) {
         final DeviceMastership mastership = deviceMasterships.remove(nodeId);
-        final ClusterSingletonServiceRegistration registration = mastership.getClusterSingletonServiceRegistration();
-        if (registration != null) {
-            try {
-                registration.close();
-            } catch (Exception e) {
-                LOG.error("FRS cluster service close fail: {} {}", nodeId.getValue(), e);
-            }
+        if (mastership != null) {
+            mastership.close();
         }
         LOG.debug("FRS service unregistered for: {}", nodeId.getValue());
     }
@@ -62,4 +53,5 @@ public class DeviceMastershipManager {
     ConcurrentHashMap<NodeId, DeviceMastership> getDeviceMasterships() {
         return deviceMasterships;
     }
+
 }
index d73df5b2ec4059844df9e6b716f2257078b05ba7..bf0096ebe73c556f419e0f3bc20d7e2842f1a2e9 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -42,17 +42,18 @@ public class DeviceMastershipManagerTest {
     }
 
     @Test
-    public void testOnDeviceConnectedAndDisconnected() {
+    public void testOnDeviceConnectedAndDisconnected() throws Exception {
         // no context
         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
         // create context - register
         deviceMastershipManager.onDeviceConnected(NODE_ID);
-        DeviceMastership registration = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
-        Assert.assertNotNull(registration);
-        Mockito.verify(clusterSingletonService).registerClusterSingletonService(registration);
+        DeviceMastership serviceInstance = deviceMastershipManager.getDeviceMasterships().get(NODE_ID);
+        Assert.assertNotNull(serviceInstance);
+        Mockito.verify(clusterSingletonService).registerClusterSingletonService(serviceInstance);
         // destroy context - unregister
         deviceMastershipManager.onDeviceDisconnected(NODE_ID);
         Assert.assertNull(deviceMastershipManager.getDeviceMasterships().get(NODE_ID));
+        Mockito.verify(registration).close();
     }
 
     @Test
index be45a9d10b140c50eca4b96ca0c7babdad6d1897..53005b8f66aa4248296a9426030352b1c0b49ea1 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -15,6 +15,7 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 
@@ -33,20 +34,21 @@ public class DeviceMastershipTest {
 
     @Before
     public void setUp() throws Exception {
-        deviceMastership = new DeviceMastership(NODE_ID, reconciliationRegistry);
+        deviceMastership = new DeviceMastership(NODE_ID, reconciliationRegistry, Mockito.mock(ClusterSingletonServiceProvider.class));
     }
 
     @Test
-    public void instantiateServiceInstance() {
+    public void testInstantiateServiceInstance() {
         deviceMastership.instantiateServiceInstance();
         Mockito.verify(reconciliationRegistry).register(NODE_ID);
         Assert.assertTrue(deviceMastership.isDeviceMastered());
     }
 
     @Test
-    public void closeServiceInstance() {
+    public void testCloseServiceInstance() {
         deviceMastership.closeServiceInstance();
         Mockito.verify(reconciliationRegistry).unregisterIfRegistered(NODE_ID);
         Assert.assertFalse(deviceMastership.isDeviceMastered());
     }
+
 }
\ No newline at end of file