Bump upstreams
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainHolderImplTest.java
index d6b2dd29457a6fdffbab3eaa4f11f046f049eac7..a4b80f653e71f5fbcde8bc3bd32d1ff5fd62b322 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
@@ -15,13 +15,11 @@ 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.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.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
-import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.opendaylight.mdsal.eos.binding.api.Entity;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipService;
+import org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange;
+import org.opendaylight.mdsal.singleton.api.ClusterSingletonServiceProvider;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionStatus;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
@@ -39,13 +37,20 @@ import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext
 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.openflow.provider.config.rev160510.NonZeroUint32Type;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
+import org.opendaylight.yangtools.concepts.Registration;
+import org.opendaylight.yangtools.yang.common.Uint32;
+import org.opendaylight.yangtools.yang.common.Uint8;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ContextChainHolderImplTest {
 
     private static final String ENTITY_TEST = "EntityTest";
     private static final String OPENFLOW_TEST = "openflow:test";
+    private static final Uint8 AUXILIARY_ID = Uint8.ZERO;
+    private static final Uint32 DEVICE_DATASTORE_REMOVAL_DELAY = Uint32.valueOf(500);
     @Mock
     private StatisticsManager statisticsManager;
     @Mock
@@ -71,34 +76,30 @@ public class ContextChainHolderImplTest {
     @Mock
     private ExecutorService executorService;
     @Mock
-    private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+    private Registration clusterSingletonServiceRegistration;
     @Mock
     private EntityOwnershipService entityOwnershipService;
     @Mock
-    private EntityOwnershipListenerRegistration entityOwnershipListenerRegistration;
+    private Registration entityOwnershipListenerRegistration;
     @Mock
     private ReconciliationFrameworkEvent reconciliationFrameworkEvent;
     @Mock
     private FeaturesReply featuresReply;
+    @Mock
+    private OpenflowProviderConfig config;
 
     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(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()))
@@ -107,6 +108,8 @@ public class ContextChainHolderImplTest {
                 .thenReturn(entityOwnershipListenerRegistration);
         Mockito.when(connectionContext.getFeatures()).thenReturn(featuresReply);
         Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(AUXILIARY_ID);
+        Mockito.when(config.getDeviceDatastoreRemovalDelay())
+                .thenReturn(new NonZeroUint32Type(DEVICE_DATASTORE_REMOVAL_DELAY));
 
         registration = manager.reconciliationFrameworkRegistration(reconciliationFrameworkEvent);
 
@@ -114,7 +117,8 @@ public class ContextChainHolderImplTest {
                 executorService,
                 singletonServicesProvider,
                 entityOwnershipService,
-                manager);
+                manager,
+                config);
         contextChainHolder.addManager(statisticsManager);
         contextChainHolder.addManager(rpcManager);
         contextChainHolder.addManager(deviceManager);
@@ -122,80 +126,71 @@ public class ContextChainHolderImplTest {
     }
 
     @Test
-    public void addManager() throws Exception {
+    public void addManager() {
         Assert.assertTrue(contextChainHolder.checkAllManagers());
     }
 
     @Test
-    public void createContextChain() throws Exception {
+    public void createContextChain() {
         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 {
+    public void reconciliationFrameworkFailure() {
         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
-                .thenReturn(Futures.immediateFailedFuture(new Throwable("test")));
+            .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 {
+    public void reconciliationFrameworkDisconnect() {
         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
-                .thenReturn(Futures.immediateFuture(ResultState.DISCONNECT));
+            .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 {
+    public void reconciliationFrameworkSuccess() {
         contextChainHolder.createContextChain(connectionContext);
         Mockito.when(reconciliationFrameworkEvent.onDevicePrepared(deviceInfo))
-                .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
-        Mockito.when(statisticsContext.initialSubmitAfterReconciliation()).thenReturn(true);
+            .thenReturn(Futures.immediateFuture(ResultState.DONOTHING));
         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).onDevicePrepared(deviceInfo);
     }
 
     @Test
-    public void reconciliationFrameworkSuccessButNotSubmit() throws Exception {
+    public void reconciliationFrameworkSuccessButNotSubmit() {
         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
     public void deviceMastered() throws Exception {
         registration.close();
         contextChainHolder.createContextChain(connectionContext);
-        contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
-        Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
-        contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_GATHERING);
-        Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.RPC_REGISTRATION);
         Assert.assertFalse(contextChainHolder.isAnyDeviceMastered());
         contextChainHolder.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.MASTER_ON_DEVICE);
@@ -210,13 +205,12 @@ 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);
+        Mockito.when(featuresReply.getAuxiliaryId()).thenReturn(Uint8.ONE);
         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
@@ -263,37 +257,23 @@ public class ContextChainHolderImplTest {
     public void ownershipChanged() throws Exception {
         registration.close();
         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.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
-        EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
-                new Entity(ENTITY_TEST, OPENFLOW_TEST),
-                true,
-                false,
-                false
-        );
-        contextChainHolder.ownershipChanged(ownershipChange);
-        Mockito.verify(deviceManager).removeDeviceFromOperationalDS(Mockito.any());
+        contextChainHolder.ownershipChanged(new Entity(ENTITY_TEST, OPENFLOW_TEST),
+            EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NO_OWNER, false);
+        Mockito.verify(deviceManager, Mockito.timeout(1000)).removeDeviceFromOperationalDS(Mockito.any());
     }
 
     @Test
     public void ownershipChangedButHasOwner() throws Exception {
         registration.close();
         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.onMasterRoleAcquired(deviceInfo, ContextChainMastershipState.INITIAL_SUBMIT);
-        EntityOwnershipChange ownershipChange = new EntityOwnershipChange(
-                new Entity(ENTITY_TEST, OPENFLOW_TEST),
-                true,
-                false,
-                true
-        );
-        contextChainHolder.ownershipChanged(ownershipChange);
+        contextChainHolder.ownershipChanged(new Entity(ENTITY_TEST, OPENFLOW_TEST),
+            EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NEW_OWNER, false);
         Mockito.verify(deviceManager,Mockito.never()).removeDeviceFromOperationalDS(Mockito.any());
     }
 }