DevCtx to DevMng 56/40156/7
authorJozef Bacigal <jbacigal@cisco.com>
Fri, 10 Jun 2016 11:09:01 +0000 (13:09 +0200)
committerJozef Bacigal <jbacigal@cisco.com>
Fri, 17 Jun 2016 11:32:57 +0000 (13:32 +0200)
onClusterRoleChange moved to manager

Change-Id: I520e81adff9382127c29cd9b1902f402eb0bb4ac
Signed-off-by: Jozef Bacigal <jbacigal@cisco.com>
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.java
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceManager.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/LifecycleConductorImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java
openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImpl.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/LifecycleConductorImplTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImplTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceManagerImplTest.java

index 1d3c597f1d6198b2ebb062796427d2bd0396830c..3c3454eeac469869b885194a3893cd29ff5896d9 100644 (file)
@@ -106,20 +106,6 @@ public interface DeviceContext extends AutoCloseable,
 
     DeviceInfo getDeviceInfo();
 
-    /**
-     * Method has to activate (MASTER) or deactivate (SLAVE) TransactionChainManager.
-     * TransactionChainManager represents possibility to write or delete Node subtree data
-     * for actual Controller Cluster Node. We are able to have an active TxManager only if
-     * newRole is {@link OfpRole#BECOMESLAVE}.
-     * Parameters are used as marker to be sure it is change to SLAVE from MASTER or from
-     * MASTER to SLAVE and the last parameter "cleanDataStore" is used for validation only.
-     * @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
-     * @return RoleChangeTxChainManager future for activation/deactivation
-     * @deprecated replaced by method onDeviceTakeClusterLeadership and onDevicLostClusterLeadership
-     */
-    @Deprecated
-    ListenableFuture<Void> onClusterRoleChange(@CheckForNull OfpRole role);
-
     /**
      * Method has to activate TransactionChainManager and prepare all Contexts from Device Contects suite
      * to Taking ClusterLeadership role {@link OfpRole#BECOMEMASTER} (e.g. Routed RPC registration, StatPolling ...)
index 337f06142d2846f46daee588b932098f0dbc0758..449ad81d2bd4698dd698ac6b9983dde6e564d78e 100644 (file)
@@ -8,7 +8,7 @@
 
 package org.opendaylight.openflowplugin.api.openflow.device;
 
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
+import com.google.common.util.concurrent.ListenableFuture;
 import org.opendaylight.openflowplugin.api.openflow.OFPManager;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceConnectedHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceDisconnectedHandler;
@@ -16,6 +16,9 @@ import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitia
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceLifecycleSupervisor;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.translator.TranslatorLibrarian;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
+
+import javax.annotation.CheckForNull;
 
 /**
  * This interface is responsible for instantiating DeviceContext and
@@ -40,5 +43,20 @@ public interface DeviceManager extends DeviceConnectedHandler, DeviceDisconnecte
      */
     DeviceContext getDeviceContextFromNodeId(DeviceInfo deviceInfo);
 
+    /**
+     * Method has to activate (MASTER) or deactivate (SLAVE) TransactionChainManager.
+     * TransactionChainManager represents possibility to write or delete Node subtree data
+     * for actual Controller Cluster Node. We are able to have an active TxManager only if
+     * newRole is {@link OfpRole#BECOMESLAVE}.
+     * Parameters are used as marker to be sure it is change to SLAVE from MASTER or from
+     * MASTER to SLAVE and the last parameter "cleanDataStore" is used for validation only.
+     *
+     * @param deviceInfo
+     * @param role - NewRole expect to be {@link OfpRole#BECOMESLAVE} or {@link OfpRole#BECOMEMASTER}
+     * @return RoleChangeTxChainManager future for activation/deactivation
+     */
+    ListenableFuture<Void> onClusterRoleChange(final DeviceInfo deviceInfo, final OfpRole role);
+
+
 }
 
index 59470c47ab83e89a16748cf0228650b40451ae04..87bc0e0a88e2fa0b5d1b3a66a7dd30c6899e12ca 100644 (file)
@@ -164,7 +164,7 @@ final class LifecycleConductorImpl implements LifecycleConductor, RoleChangeList
                         OfpRole.BECOMESLAVE);
             }
 
-            final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(newRole);
+            final ListenableFuture<Void> onClusterRoleChange = deviceManager.onClusterRoleChange(deviceInfo, newRole);
             Futures.addCallback(onClusterRoleChange, new FutureCallback<Void>() {
                 @Override
                 public void onSuccess(@Nullable final Void aVoid) {
index ad3301ed744bb00a24ac6ac636288d248eaca9a0..9fed87908039e521ef024afa0b460bb4832b3322 100644 (file)
@@ -237,15 +237,6 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
         return dataBroker.newReadOnlyTransaction();
     }
 
-    @Override
-    public ListenableFuture<Void> onClusterRoleChange(@CheckForNull final OfpRole role) {
-        LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
-        if (OfpRole.BECOMEMASTER.equals(role)) {
-            return onDeviceTakeClusterLeadership();
-        }
-        return transactionChainManager.deactivateTransactionManager();
-    }
-
     @Override
     public ListenableFuture<Void> onDeviceTakeClusterLeadership() {
         LOG.trace("onDeviceTakeClusterLeadership for node: {}", deviceInfo.getNodeId());
index 57e5096432038d03fd1c7da6968cb38233925a44..51b4b7c9ebf49de75b1ba9e10492527541e6b133 100644 (file)
@@ -25,7 +25,6 @@ import java.util.concurrent.TimeUnit;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
@@ -48,6 +47,7 @@ import org.opendaylight.openflowplugin.impl.device.listener.OpenflowProtocolList
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -257,7 +257,7 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
         LOG.trace("onDeviceDisconnected method call for Node: {}", connectionContext.getNodeId());
         final DeviceInfo deviceInfo = connectionContext.getDeviceInfo();
-        final DeviceContext deviceCtx = this.deviceContexts.get(deviceInfo.getNodeId());
+        final DeviceContext deviceCtx = this.deviceContexts.get(deviceInfo);
 
         if (null == deviceCtx) {
             LOG.info("DeviceContext for Node {} was not found. Connection is terminated without OFP context suite.", deviceInfo.getNodeId());
@@ -304,4 +304,15 @@ public class DeviceManagerImpl implements DeviceManager, ExtensionConverterProvi
     public <T extends OFPContext> T gainContext(final DeviceInfo deviceInfo) {
         return (T) deviceContexts.get(deviceInfo);
     }
+
+    @Override
+    public ListenableFuture<Void> onClusterRoleChange(final DeviceInfo deviceInfo, final OfpRole role) {
+        DeviceContext deviceContext = conductor.getDeviceContext(deviceInfo);
+        LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
+        if (OfpRole.BECOMEMASTER.equals(role)) {
+            return deviceContext.onDeviceTakeClusterLeadership();
+        }
+        return ((DeviceContextImpl)deviceContext).getTransactionChainManager().deactivateTransactionManager();
+    }
+
 }
index e1d0201f08710779121766ac60e658679c636d7b..a6fbfa7b00df07a48c7600984fe0569a50340686 100644 (file)
@@ -193,7 +193,7 @@ public class LifecycleConductorImplTest {
     public void roleChangeOnDeviceTest4() {
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
+        when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMEMASTER,false);
         verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
     }
@@ -205,7 +205,7 @@ public class LifecycleConductorImplTest {
     public void roleChangeOnDeviceTest5() {
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
         when(deviceManager.gainContext(deviceInfo)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
+        when(deviceManager.onClusterRoleChange(deviceInfo, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
         verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
     }
index aa8b1d13ab7cab3178aaab7a8576d4541c4b77eb..011dbc62f5491936fc2c3796fb47ddf505676a9e 100644 (file)
@@ -544,22 +544,4 @@ public class DeviceContextImplTest {
         assertEquals(0, deviceContext.getDeviceMeterRegistry().getAllMeterIds().size());
 
     }
-
-    @Test
-    public void testOnClusterRoleChange() throws Exception {
-
-        // test call transactionChainManager.deactivateTransactionManager()
-        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
-
-        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
-
-        final StatisticsContext statisticsContext = mock(StatisticsContext.class);
-        deviceContextSpy.setStatisticsContext(statisticsContext);
-
-        deviceContextSpy.onClusterRoleChange(OfpRole.BECOMEMASTER);
-        verify(deviceContextSpy).onDeviceTakeClusterLeadership();
-
-        Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
-        deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE);
-    }
 }
index bbb8ae81be9a0574c3a1a56125917f4d6f91d0fa..e15fe5102e803df7680fc65932cadbf5e653efd2 100644 (file)
@@ -110,8 +110,8 @@ public class DeviceManagerImplTest {
         when(mockConnectionContext.getNodeId()).thenReturn(DUMMY_NODE_ID);
         when(mockConnectionContext.getFeatures()).thenReturn(mockFeatures);
         when(mockConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
-        when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
         when(mockConnectionContext.getDeviceInfo()).thenReturn(deviceInfo);
+        when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockConnectionContext);
         when(deviceInfo.getNodeId()).thenReturn(DUMMY_NODE_ID);
 
         final Capabilities capabilitiesV13 = mock(Capabilities.class);
@@ -232,8 +232,8 @@ public class DeviceManagerImplTest {
         when(deviceContext.getPrimaryConnectionContext()).thenReturn(connectionContext);
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
 
-        final ConcurrentHashMap<NodeId, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
-        deviceContexts.put(DUMMY_NODE_ID, deviceContext);
+        final ConcurrentHashMap<DeviceInfo, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
+        deviceContexts.put(deviceInfo, deviceContext);
 
         deviceManager.onDeviceDisconnected(connectionContext);
 
@@ -276,8 +276,8 @@ public class DeviceManagerImplTest {
     public void testClose() throws Exception {
         final DeviceContext deviceContext = mock(DeviceContext.class);
         final DeviceManagerImpl deviceManager = prepareDeviceManager();
-        final ConcurrentHashMap<NodeId, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
-        deviceContexts.put(mockedNodeId, deviceContext);
+        final ConcurrentHashMap<DeviceInfo, DeviceContext> deviceContexts = getContextsCollection(deviceManager);
+        deviceContexts.put(deviceInfo, deviceContext);
         Assert.assertEquals(1, deviceContexts.size());
 
         deviceManager.close();
@@ -286,12 +286,12 @@ public class DeviceManagerImplTest {
         verify(deviceContext, Mockito.never()).close();
     }
 
-    private static ConcurrentHashMap<NodeId, DeviceContext> getContextsCollection(final DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
+    private static ConcurrentHashMap<DeviceInfo, DeviceContext> getContextsCollection(final DeviceManagerImpl deviceManager) throws NoSuchFieldException, IllegalAccessException {
         // HACK: contexts collection for testing shall be accessed in some more civilized way
         final Field contextsField = DeviceManagerImpl.class.getDeclaredField("deviceContexts");
         Assert.assertNotNull(contextsField);
         contextsField.setAccessible(true);
-        return (ConcurrentHashMap<NodeId, DeviceContext>) contextsField.get(deviceManager);
+        return (ConcurrentHashMap<DeviceInfo, DeviceContext>) contextsField.get(deviceManager);
     }
 
 }