Merge "Conductor RPC registration moved"
authormichal rehak <mirehak@cisco.com>
Tue, 21 Jun 2016 09:00:56 +0000 (09:00 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 21 Jun 2016 09:00:56 +0000 (09:00 +0000)
openflowplugin-api/src/main/java/org/opendaylight/openflowplugin/api/openflow/device/DeviceContext.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/test/java/org/opendaylight/openflowplugin/impl/LifecycleConductorImplTest.java
openflowplugin-impl/src/test/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImplTest.java

index 7a546ac84a69792feb16809912b725778dfea3f8..79b84011b4c720689439c52e73c03cdd65952b36 100644 (file)
@@ -13,7 +13,7 @@ import io.netty.util.Timeout;
 import java.math.BigInteger;
 import java.util.List;
 import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
+
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
 import org.opendaylight.openflowplugin.api.openflow.OFPContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
@@ -114,13 +114,12 @@ public interface DeviceContext extends AutoCloseable,
      * 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 oldRole - old role for quick validation for needed processing
      * @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(@Nullable OfpRole oldRole, @CheckForNull OfpRole role);
+    ListenableFuture<Void> onClusterRoleChange(@CheckForNull OfpRole role);
 
     /**
      * Method has to activate TransactionChainManager and prepare all Contexts from Device Contects suite
@@ -129,13 +128,6 @@ public interface DeviceContext extends AutoCloseable,
      */
     ListenableFuture<Void> onDeviceTakeClusterLeadership();
 
-    /**
-     * Method has to deactivate TransactionChainManager and prepare all Contexts from Device Contects suite
-     * to Lost ClusterLeadership role {@link OfpRole#BECOMESLAVE} (e.g. Stop RPC rounting, stop StatPolling ...)
-     * @return RoleChangeTxChainManager future for deactivation
-     */
-    ListenableFuture<Void> onDeviceLostClusterLeadership();
-
     /**
      * Method has to close TxManager ASAP we are notified about Closed Connection
      * @return sync. future for Slave and MD-SAL completition for Master
index 0213b20aa767d787d7fc7a092b654cf1b0b74897..942f1527fee6e2f37e22bc896cbdafaedb04d510 100644 (file)
@@ -34,7 +34,7 @@ import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListe
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.openflowplugin.impl.util.MdSalRegistrationUtils;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -136,23 +136,34 @@ final class LifecycleConductorImpl implements LifecycleConductor, RoleChangeList
 
             LOG.info("Role change to {} in role context for node {} was successful, starting/stopping services.", newRole, deviceInfo);
 
+            final String logText;
+
             if (OfpRole.BECOMEMASTER.equals(newRole)) {
-                statisticsManager.startScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
+                logText = "Start";
+                statisticsManager.startScheduling(deviceInfo);
+                MdSalRegistrationUtils.registerMasterServices(
+                        rpcManager.gainContext(deviceInfo),
+                        deviceContext,
+                        OfpRole.BECOMEMASTER);
             } else {
-                statisticsManager.stopScheduling(deviceContext.getPrimaryConnectionContext().getDeviceInfo());
+                logText = "Stopp";
+                statisticsManager.stopScheduling(deviceInfo);
+                MdSalRegistrationUtils.registerSlaveServices(
+                        rpcManager.gainContext(deviceInfo),
+                        OfpRole.BECOMESLAVE);
             }
 
-            final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(null, newRole);
+            final ListenableFuture<Void> onClusterRoleChange = deviceContext.onClusterRoleChange(newRole);
             Futures.addCallback(onClusterRoleChange, new FutureCallback<Void>() {
                 @Override
                 public void onSuccess(@Nullable final Void aVoid) {
-                    LOG.info("Starting/Stopping services for node {} was successful", deviceInfo);
+                    LOG.info("{}ing services for node {} was successful", logText, deviceInfo);
                     if (newRole.equals(OfpRole.BECOMESLAVE)) notifyServiceChangeListeners(deviceInfo, true);
                 }
 
                 @Override
                 public void onFailure(final Throwable throwable) {
-                    LOG.warn("Starting/Stopping services for node {} was NOT successful, closing connection", deviceInfo);
+                    LOG.warn("ing services for node {} was NOT successful, closing connection", logText, deviceInfo);
                     closeConnection(deviceInfo);
                 }
             });
index 7b0851c789c62ac32e561f139cb766bfbb6eb1e7..46186ad3c0a76bd99e8fcb565dbe4d99ac37d778 100644 (file)
@@ -241,31 +241,10 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
     }
 
     @Override
-    public ListenableFuture<Void> onClusterRoleChange(final OfpRole oldRole, @CheckForNull final OfpRole role) {
+    public ListenableFuture<Void> onClusterRoleChange(@CheckForNull final OfpRole role) {
         LOG.trace("onClusterRoleChange {} for node:", role, deviceInfo.getNodeId());
-        Preconditions.checkArgument(role != null);
-        if (role.equals(oldRole)) {
-            LOG.debug("Demanded role change for device {} is not changed. OldRole: {}, NewRole {}", deviceInfo.getNodeId(), oldRole, role);
-            return Futures.immediateFuture(null);
-        }
         if (OfpRole.BECOMEMASTER.equals(role)) {
             return onDeviceTakeClusterLeadership();
-        } else if (OfpRole.BECOMESLAVE.equals(role)) {
-            return onDeviceLostClusterLeadership();
-        } else {
-            LOG.warn("Unknown OFCluster Role {} for Node {}", role, deviceInfo.getNodeId());
-            if (null != rpcContext) {
-                MdSalRegistrationUtils.unregisterServices(rpcContext);
-            }
-            return transactionChainManager.deactivateTransactionManager();
-        }
-    }
-
-    @Override
-    public ListenableFuture<Void> onDeviceLostClusterLeadership() {
-        LOG.trace("onDeviceLostClusterLeadership for node: {}", deviceInfo.getNodeId());
-        if (null != rpcContext) {
-            MdSalRegistrationUtils.registerSlaveServices(rpcContext, OfpRole.BECOMESLAVE);
         }
         return transactionChainManager.deactivateTransactionManager();
     }
@@ -284,8 +263,6 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi
             LOG.warn(errMsg);
             return Futures.immediateFailedFuture(new IllegalStateException(errMsg));
         }
-        /* Routed RPC registration */
-        MdSalRegistrationUtils.registerMasterServices(getRpcContext(), DeviceContextImpl.this, OfpRole.BECOMEMASTER);
 
         if (isStatisticsRpcEnabled) {
             MdSalRegistrationUtils.registerStatCompatibilityServices(getRpcContext(), this,
index 9ee458de5f02a5e57d91edb366ad9ad30cda0236..d044c5d5f9a123a71acd64b3cb1551ecb97b3589 100644 (file)
@@ -15,6 +15,8 @@ import static org.mockito.Mockito.when;
 import com.google.common.util.concurrent.ListenableFuture;
 import io.netty.util.HashedWheelTimer;
 import io.netty.util.TimerTask;
+
+import java.math.BigInteger;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.TimeUnit;
 import org.junit.Before;
@@ -29,6 +31,9 @@ import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ServiceChangeListener;
+import org.opendaylight.openflowplugin.api.openflow.role.RoleManager;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
+import org.opendaylight.openflowplugin.api.openflow.rpc.RpcManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
@@ -67,6 +72,10 @@ public class LifecycleConductorImplTest {
     @Mock
     private StatisticsManager statisticsManager;
     @Mock
+    private RpcManager rpcManager;
+    @Mock
+    private RpcContext rpcContext;
+    @Mock
     private DeviceInfo deviceInfo;
 
     private NodeId nodeId = new NodeId("openflow-junit:1");
@@ -81,9 +90,13 @@ public class LifecycleConductorImplTest {
         lifecycleConductor = new LifecycleConductorImpl(messageIntelligenceAgency);
         lifecycleConductor.setSafelyManager(deviceManager);
         lifecycleConductor.setSafelyManager(statisticsManager);
+        lifecycleConductor.setSafelyManager(rpcManager);
 
         when(connectionContext.getFeatures()).thenReturn(featuresReply);
         when(deviceInfo.getNodeId()).thenReturn(nodeId);
+        when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+        when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
+        when(rpcManager.gainContext(Mockito.<DeviceInfo>any())).thenReturn(rpcContext);
     }
 
 
@@ -177,7 +190,7 @@ public class LifecycleConductorImplTest {
     public void roleChangeOnDeviceTest4() {
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
+        when(deviceContext.onClusterRoleChange(OfpRole.BECOMEMASTER)).thenReturn(listenableFuture);
         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMEMASTER,false);
         verify(statisticsManager).startScheduling(Mockito.<DeviceInfo>any());
     }
@@ -189,7 +202,7 @@ public class LifecycleConductorImplTest {
     public void roleChangeOnDeviceTest5() {
         when(deviceContext.getDeviceState()).thenReturn(deviceState);
         when(deviceManager.getDeviceContextFromNodeId(deviceInfo)).thenReturn(deviceContext);
-        when(deviceContext.onClusterRoleChange(null, OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
+        when(deviceContext.onClusterRoleChange(OfpRole.BECOMESLAVE)).thenReturn(listenableFuture);
         lifecycleConductor.roleChangeOnDevice(deviceInfo,true,OfpRole.BECOMESLAVE,false);
         verify(statisticsManager).stopScheduling(Mockito.<DeviceInfo>any());
     }
index 31e8ffe3b93e6a80ecc140989e24784e79162c1b..6ae6fb8d4d7a45239492ccac276760293d02f49c 100644 (file)
@@ -16,6 +16,7 @@ import static org.mockito.Matchers.any;
 import static org.mockito.Matchers.eq;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyZeroInteractions;
 import static org.mockito.Mockito.when;
 
 import com.google.common.base.Optional;
@@ -36,9 +37,8 @@ import org.junit.runner.RunWith;
 import org.mockito.InOrder;
 import org.mockito.Mock;
 import org.mockito.Mockito;
-import org.mockito.invocation.InvocationOnMock;
+import org.mockito.Spy;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
@@ -58,6 +58,7 @@ import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
+import org.opendaylight.openflowplugin.api.openflow.device.TxFacade;
 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
@@ -121,14 +122,14 @@ public class DeviceContextImplTest {
     private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("55");
     Xid xid;
     Xid xidMulti;
-    DeviceContextImpl deviceContext;
+
+    DeviceContext deviceContext;
     @Mock
     TransactionChainManager txChainManager;
     @Mock
     RequestContext<GetAsyncReply> requestContext;
     @Mock
     RequestContext<MultipartReply> requestContextMultiReply;
-
     @Mock
     ConnectionContext connectionContext;
     @Mock
@@ -175,14 +176,14 @@ public class DeviceContextImplTest {
     private DeviceContext deviceContextSpy;
 
     @Before
-    public void setUp() {
+    public void setUp() throws Exception{
         final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
         Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
         Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
         Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class))).thenReturn(txChainFactory);
         Mockito.when(deviceInfo.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);
         Mockito.when(deviceInfo.getNodeId()).thenReturn(nodeId);
-        Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
+        Mockito.when(deviceInfo.getDatapathId()).thenReturn(BigInteger.ONE);
         final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
         final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply = SettableFuture.create();
         Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
@@ -218,11 +219,12 @@ public class DeviceContextImplTest {
         Mockito.when(lifecycleConductor.getMessageIntelligenceAgency()).thenReturn(messageIntelligenceAgency);
 
         deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, lifecycleConductor, outboundQueueProvider, translatorLibrary, false);
-
         deviceContextSpy = Mockito.spy(deviceContext);
 
         xid = new Xid(atomicLong.incrementAndGet());
         xidMulti = new Xid(atomicLong.incrementAndGet());
+
+        Mockito.doNothing().when(deviceContextSpy).writeToTransaction(Mockito.<LogicalDatastoreType>any(), Mockito.<InstanceIdentifier>any(), any());
     }
 
     @Test(expected = NullPointerException.class)
@@ -261,10 +263,10 @@ public class DeviceContextImplTest {
     public void testInitialSubmitTransaction() throws Exception {
         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
         final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
-        deviceContext.getTransactionChainManager().activateTransactionManager() ;
-        deviceContext.getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
         deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
-        deviceContext.initialSubmitTransaction();
+        ((DeviceContextImpl) deviceContext).initialSubmitTransaction();
         verify(wTx).submit();
     }
 
@@ -312,8 +314,8 @@ public class DeviceContextImplTest {
     @Test
     public void testAddDeleteToTxChain() throws Exception{
         final InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
-        deviceContext.getTransactionChainManager().activateTransactionManager() ;
-        deviceContext.getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
         deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
         verify(wTx).delete(eq(LogicalDatastoreType.CONFIGURATION), eq(dummyII));
     }
@@ -323,8 +325,8 @@ public class DeviceContextImplTest {
      */
     @Test
     public void testSubmitTransaction() throws Exception {
-        deviceContext.getTransactionChainManager().activateTransactionManager() ;
-        deviceContext.getTransactionChainManager().enableSubmit();
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().activateTransactionManager() ;
+        ((DeviceContextImpl) deviceContext).getTransactionChainManager().enableSubmit();
         assertTrue(deviceContext.submitTransaction());
     }
 
@@ -478,8 +480,9 @@ public class DeviceContextImplTest {
         when(mockedPortStatusMessage.getPortNo()).thenReturn(42L);
 
         OpenflowPortsUtil.init();
-        deviceContext.processPortStatusMessage(mockedPortStatusMessage);
-//        verify(txChainManager).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class));
+        deviceContextSpy.processPortStatusMessage(mockedPortStatusMessage);
+        verify(deviceContextSpy).writeToTransaction(Mockito.<LogicalDatastoreType>any(), Mockito.<InstanceIdentifier>any(), any());
+        verify(deviceContextSpy).submitTransaction();
     }
 
     @Test
@@ -533,7 +536,7 @@ public class DeviceContextImplTest {
         final NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
 
         deviceContext.setNotificationPublishService(mockedNotificationPublishService);
-        deviceContext.setExtensionConverterProvider(mockedExtensionConverterProvider);
+        ((DeviceContextImpl) deviceContext).setExtensionConverterProvider(mockedExtensionConverterProvider);
         deviceContext.processExperimenterMessage(experimenterMessage);
 
         verify(mockedNotificationPublishService).offerNotification(any(ExperimenterMessageFromDev.class));
@@ -551,25 +554,22 @@ public class DeviceContextImplTest {
 
     @Test
     public void testOnClusterRoleChange() throws Exception {
-        // test role.equals(oldRole)
-        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMEMASTER, OfpRole.BECOMEMASTER).get());
 
         // test call transactionChainManager.deactivateTransactionManager()
-        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE, OfpRole.NOCHANGE).get());
+        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
 
         // test call MdSalRegistrationUtils.unregisterServices(rpcContext)
         final RpcContext rpcContext = mock(RpcContext.class);
         deviceContextSpy.setRpcContext(rpcContext);
-        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE, OfpRole.NOCHANGE).get());
+        Assert.assertNull(deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE).get());
 
         final StatisticsContext statisticsContext = mock(StatisticsContext.class);
         deviceContextSpy.setStatisticsContext(statisticsContext);
 
-        deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE, OfpRole.BECOMEMASTER);
+        deviceContextSpy.onClusterRoleChange(OfpRole.BECOMEMASTER);
         verify(deviceContextSpy).onDeviceTakeClusterLeadership();
 
         Mockito.when(wTx.submit()).thenReturn(Futures.immediateCheckedFuture(null));
-        deviceContextSpy.onClusterRoleChange(OfpRole.NOCHANGE, OfpRole.BECOMESLAVE);
-        verify(deviceContextSpy).onDeviceLostClusterLeadership();
+        deviceContextSpy.onClusterRoleChange(OfpRole.BECOMESLAVE);
     }
 }