Redesign statistics context and manager
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / lifecycle / ContextChainImplTest.java
index 13019a48dc56b242b930748a5a1e3432daaa61e2..9cf6628c4df723582bb0516fe6e020919b8fe323 100644 (file)
@@ -8,24 +8,32 @@
 package org.opendaylight.openflowplugin.impl.lifecycle;
 
 import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.MoreExecutors;
+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.mdsal.singleton.common.api.ClusterSingletonServiceRegistration;
+import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
+import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceRemovedHandler;
 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChain;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipState;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.ContextChainMastershipWatcher;
 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 
 @RunWith(MockitoJUnitRunner.class)
 public class ContextChainImplTest {
 
+    private static final String TEST_NODE = "test node";
+    private static final ServiceGroupIdentifier SERVICE_GROUP_IDENTIFIER = ServiceGroupIdentifier.create(TEST_NODE);
+
     @Mock
     private StatisticsContext statisticsContext;
     @Mock
@@ -33,67 +41,86 @@ public class ContextChainImplTest {
     @Mock
     private DeviceContext deviceContext;
     @Mock
-    private LifecycleService lifecycleService;
-    @Mock
     private DeviceInfo deviceInfo;
     @Mock
     private ConnectionContext connectionContext;
+    @Mock
+    private ClusterSingletonServiceProvider clusterSingletonServiceProvider;
+    @Mock
+    private ClusterSingletonServiceRegistration clusterSingletonServiceRegistration;
+    @Mock
+    private ContextChainMastershipWatcher contextChainMastershipWatcher;
+    @Mock
+    private DeviceRemovedHandler deviceRemovedHandler;
 
     private ContextChain contextChain;
 
     @Before
     public void setUp() throws Exception {
-
         Mockito.when(deviceContext.getDeviceInfo()).thenReturn(deviceInfo);
-        Mockito.when(deviceContext.stopClusterServices())
-                .thenReturn(Futures.immediateFuture(null));
-        Mockito.when(rpcContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
-        Mockito.when(statisticsContext.stopClusterServices()).thenReturn(Futures.immediateFuture(null));
-        Mockito.when(statisticsContext.initialGatherDynamicData()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(deviceInfo.getServiceIdentifier()).thenReturn(SERVICE_GROUP_IDENTIFIER);
+        Mockito.when(deviceContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(rpcContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
+        Mockito.when(statisticsContext.closeServiceInstance()).thenReturn(Futures.immediateFuture(null));
         Mockito.when(connectionContext.getDeviceInfo()).thenReturn(deviceInfo);
+        Mockito.when(connectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
+        Mockito.when(clusterSingletonServiceProvider.registerClusterSingletonService(Mockito.any()))
+                .thenReturn(clusterSingletonServiceRegistration);
 
-        contextChain = new ContextChainImpl(connectionContext);
+        contextChain = new ContextChainImpl(contextChainMastershipWatcher, connectionContext,
+                MoreExecutors.newDirectExecutorService());
         contextChain.addContext(statisticsContext);
         contextChain.addContext(rpcContext);
         contextChain.addContext(deviceContext);
-        contextChain.addLifecycleService(lifecycleService);
-
+        contextChain.registerServices(clusterSingletonServiceProvider);
     }
 
     @Test
-    public void stopChain() throws Exception {
-        contextChain.stopChain();
-        Mockito.verify(deviceContext).stopClusterServices();
-        Mockito.verify(rpcContext).stopClusterServices();
-        Mockito.verify(statisticsContext).stopClusterServices();
+    public void closeServiceInstance() throws Exception {
+        contextChain.closeServiceInstance();
+        Mockito.verify(deviceContext).closeServiceInstance();
+        Mockito.verify(rpcContext).closeServiceInstance();
+        Mockito.verify(statisticsContext).closeServiceInstance();
     }
 
     @Test
     public void close() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
         contextChain.close();
         Mockito.verify(statisticsContext).close();
         Mockito.verify(deviceContext).close();
         Mockito.verify(rpcContext).close();
-        Mockito.verify(lifecycleService).close();
+        Mockito.verify(deviceRemovedHandler).onDeviceRemoved(Mockito.any(DeviceInfo.class));
+    }
+
+    @Test
+    public void closeTwoTimes() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
+        contextChain.close();
+        contextChain.close();
+        Mockito.verify(deviceRemovedHandler, Mockito.times(1))
+                .onDeviceRemoved(Mockito.any(DeviceInfo.class));
     }
 
     @Test
-    public void connectionDropped() throws Exception {
-        contextChain.isMastered(ContextChainMastershipState.INITIAL_GATHERING);
-        contextChain.isMastered(ContextChainMastershipState.INITIAL_SUBMIT);
-        contextChain.isMastered(ContextChainMastershipState.MASTER_ON_DEVICE);
-        contextChain.isMastered(ContextChainMastershipState.INITIAL_FLOW_REGISTRY_FILL);
-        contextChain.connectionDropped();
-        Mockito.verify(deviceContext).stopClusterServices();
-        Mockito.verify(rpcContext).stopClusterServices();
-        Mockito.verify(statisticsContext).stopClusterServices();
+    public void closeThreeTimes() throws Exception {
+        contextChain.registerDeviceRemovedHandler(deviceRemovedHandler);
+        contextChain.close();
+        contextChain.close();
+        Mockito.verify(deviceRemovedHandler, Mockito.times(1))
+                .onDeviceRemoved(Mockito.any(DeviceInfo.class));
     }
 
     @Test
-    public void makeDeviceSlave() throws Exception {
-        contextChain.makeDeviceSlave();
-        Mockito.verify(lifecycleService).makeDeviceSlave(Mockito.any(DeviceContext.class));
+    public void getIdentifier() throws Exception {
+        Assert.assertEquals(contextChain.getIdentifier(), SERVICE_GROUP_IDENTIFIER);
     }
 
+    @Test
+    public void instantiateServiceInstanceFail() throws Exception {
+        Mockito.doThrow(new IllegalStateException()).when(deviceContext).instantiateServiceInstance();
+        contextChain.instantiateServiceInstance();
+        Mockito.verify(contextChainMastershipWatcher)
+                .onNotAbleToStartMastershipMandatory(Mockito.any(DeviceInfo.class), Mockito.anyString());
+    }
 }
-