Merge "replace @SuppressFBWarnings with LoggingFutures"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainHolderImplTest.java
index 8c6b03bef0b2965c77430ae95db15cccac8760d7..9a1c30174d4d68f54d02f297afe98bb2ac1995fa 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -8,7 +8,6 @@
 package org.opendaylight.openflowplugin.impl.lifecycle;
 
 import com.google.common.util.concurrent.Futures;
-import io.netty.util.HashedWheelTimer;
 import java.util.concurrent.ExecutorService;
 import org.junit.Assert;
 import org.junit.Before;
@@ -17,11 +16,11 @@ import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.runners.MockitoJUnitRunner;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+import org.opendaylight.mdsal.eos.binding.api.Entity;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipChange;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipListenerRegistration;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
+import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
@@ -30,10 +29,11 @@ import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.OwnershipChangeListener;
 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkEvent;
 import org.opendaylight.openflowplugin.api.openflow.mastership.ReconciliationFrameworkRegistration;
+import org.opendaylight.openflowplugin.api.openflow.role.RoleContext;
+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.StatisticsContext;
@@ -41,15 +41,13 @@ import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsManager
 import org.opendaylight.openflowplugin.impl.mastership.MastershipChangeServiceManagerImpl;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
-import org.opendaylight.yangtools.yang.common.RpcError;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ContextChainHolderImplTest {
 
     private static final String ENTITY_TEST = "EntityTest";
     private static final String OPENFLOW_TEST = "openflow:test";
-    @Mock
-    private HashedWheelTimer timer;
+    private static final Short AUXILIARY_ID = 0;
     @Mock
     private StatisticsManager statisticsManager;
     @Mock
@@ -57,12 +55,16 @@ public class ContextChainHolderImplTest {
     @Mock
     private DeviceManager deviceManager;
     @Mock
+    private RoleManager roleManager;
+    @Mock
     private StatisticsContext statisticsContext;
     @Mock
     private RpcContext rpcContext;
     @Mock
     private DeviceContext deviceContext;
     @Mock
+    private RoleContext roleContext;
+    @Mock
     private ConnectionContext connectionContext;
     @Mock
     private DeviceInfo deviceInfo;
@@ -77,30 +79,22 @@ public class ContextChainHolderImplTest {
     @Mock
     private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
     @Mock
-    private OwnershipChangeListener ownershipChangeListener;
-    @Mock
     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
     @Mock
     private FeaturesReply featuresReply;
 
     private ContextChainHolderImpl contextChainHolder;
     private ReconciliationFrameworkRegistration registration;
-    private MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
-    private final Short AUXILIARY_ID = 0;
+    private final MastershipChangeServiceManager manager = new MastershipChangeServiceManagerImpl();
 
     @Before
     public void setUp() throws Exception {
-        Mockito.doAnswer(invocation -> {
-            invocation.getArgumentAt(0, Runnable.class).run();
-            return null;
-        }).when(executorService).submit(Mockito.<Runnable>any());
-
-
         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
         Mockito.when(deviceManager.createContext(connectionContext)).thenReturn(deviceContext);
         Mockito.when(rpcManager.createContext(deviceContext)).thenReturn(rpcContext);
-        Mockito.when(statisticsManager.createContext(deviceContext)).thenReturn(statisticsContext);
-        Mockito.when(deviceContext.makeDeviceSlave()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(roleManager.createContext(deviceContext)).thenReturn(roleContext);
+        Mockito.when(statisticsManager.createContext(Mockito.eq(deviceContext), Mockito.anyBoolean()))
+            .thenReturn(statisticsContext);
         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
 
         Mockito.when(singletonServicesProvider.registerClusterSingletonService(Mockito.any()))
@@ -113,15 +107,14 @@ public class ContextChainHolderImplTest {
         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
 
         contextChainHolder = new ContextChainHolderImpl(
-                timer,
                 executorService,
                 singletonServicesProvider,
                 entityOwnershipService,
-                manager
-        );
+                manager);
         contextChainHolder.addManager(statisticsManager);
         contextChainHolder.addManager(rpcManager);
         contextChainHolder.addManager(deviceManager);
+        contextChainHolder.addManager(roleManager);
     }
 
     @Test
@@ -134,37 +127,40 @@ public class ContextChainHolderImplTest {
         contextChainHolder.createContextChain(connectionContext);
         Mockito.verify(deviceManager).createContext(Mockito.any(ConnectionContext.class));
         Mockito.verify(rpcManager).createContext(Mockito.any(DeviceContext.class));
-        Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class));
+        Mockito.verify(roleManager).createContext(Mockito.any(DeviceContext.class));
+        Mockito.verify(statisticsManager).createContext(Mockito.any(DeviceContext.class), Mockito.anyBoolean());
     }
 
 
     @Test
     public void reconciliationFrameworkFailure() throws Exception {
-        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
+        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
+            .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
         contextChainHolder.createContextChain(connectionContext);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
-        Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
+        Mockito.verify(connectionContext).closeConnection(false);
     }
 
     @Test
     public void reconciliationFrameworkDisconnect() throws Exception {
-        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
+        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
+            .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
         contextChainHolder.createContextChain(connectionContext);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
-        Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
+        Mockito.verify(connectionContext).closeConnection(false);
     }
 
     @Test
     public void reconciliationFrameworkSuccess() throws Exception {
         contextChainHolder.createContextChain(connectionContext);
-        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
-        Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(true);
+        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
+            .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
         contextChainHolder.createContextChain(connectionContext);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
@@ -176,14 +172,16 @@ public class ContextChainHolderImplTest {
     @Test
     public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
         contextChainHolder.createContextChain(connectionContext);
-        Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo)).thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
-        Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(false);
+        // TODO when if (future != null) check in MastershipChangeServiceManagerImpl's becomeSlaveOrDisconnect() is rm
+        // Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
+        //    .thenReturn(Futures.immediateFuture(null));
         contextChainHolder.createContextChain(connectionContext);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
-        contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
+        contextChainHolder.onNotAbleToStartMastershipMandatory(deviceInfo, "Test reason");
         Mockito.verify(reconciliationFrameworkEvent).onDeviceDisconnected(deviceInfo);
+        Mockito.verify(connectionContext).closeConnection(false);
     }
 
     @Test
@@ -208,13 +206,13 @@ public class ContextChainHolderImplTest {
         registration.close();
         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
                 == ConnectionStatus.MAY_CONTINUE);
-        Short AUXILIARY_ID_1 = 1;
-        Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID_1);
+        Short auxiliaryId1 = 1;
+        Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(auxiliaryId1);
         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
                 == ConnectionStatus.MAY_CONTINUE);
         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
         Assert.assertTrue(contextChainHolder.deviceConnected(connectionContext)
-                == ConnectionStatus.ALREADY_CONNECTED);
+                == ConnectionStatus.MAY_CONTINUE);
     }
 
     @Test
@@ -231,7 +229,7 @@ public class ContextChainHolderImplTest {
     public void notAbleToSetSlave() throws Exception {
         registration.close();
         contextChainHolder.deviceConnected(connectionContext);
-        contextChainHolder.onSlaveRoleNotAcquired(deviceInfo);
+        contextChainHolder.onSlaveRoleNotAcquired(deviceInfo, "Test reason");
         Mockito.verify(deviceContext).close();
         Mockito.verify(statisticsContext).close();
         Mockito.verify(rpcContext).close();
@@ -268,9 +266,7 @@ public class ContextChainHolderImplTest {
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
-                true,
-                false,
-                false
+                EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NO_OWNER
         );
         contextChainHolder.ownershipChanged(ownershipChange);
         Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
@@ -287,11 +283,9 @@ public class ContextChainHolderImplTest {
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
         EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
                 new Entity(ENTITY_TEST, OPENFLOW_TEST),
-                true,
-                false,
-                true
+                EntityOwnershipChangeState.LOCAL_OWNERSHIP_LOST_NEW_OWNER
         );
         contextChainHolder.ownershipChanged(ownershipChange);
         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
     }
-}
\ No newline at end of file
+}