Merge "Bug 6110: Fixed bugs in statistics manager due to race condition." into stable...
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsManagerImplTest.java
index 44c3f3a864e7fd10129ccef928f6a9fa2339dd7c..461c92c5df90fafa27732cc406480f9ccd5dde73 100644 (file)
@@ -1,4 +1,3 @@
-
 /**
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
@@ -52,13 +51,15 @@ import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
-import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor;
+import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
+import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
@@ -84,8 +85,6 @@ public class StatisticsManagerImplTest {
 
     private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImplTest.class);
 
-    private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("444");
-    private static final Short DUMMY_VERSION = OFConstants.OFP_VERSION_1_3;
     public static final NodeId NODE_ID = new NodeId("ofp-unit-dummy-node-id");
 
     @Mock
@@ -127,8 +126,6 @@ public class StatisticsManagerImplTest {
     @Mock
     private DeviceManager deviceManager;
     @Mock
-    private LifecycleConductor conductor;
-    @Mock
     private GetFeaturesOutput featuresOutput;
     @Mock
     private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
@@ -136,6 +133,8 @@ public class StatisticsManagerImplTest {
     private DeviceInfo deviceInfo;
     @Mock
     private DataBroker dataBroker;
+    @Mock
+    private LifecycleService lifecycleService;
 
     private RequestContext<List<MultipartReply>> currentRequestContext;
     private StatisticsManagerImpl statisticsManager;
@@ -161,12 +160,12 @@ public class StatisticsManagerImplTest {
         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
         when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
-        when(mockedDeviceInfo.getNodeId()).thenReturn(new NodeId("ofp-unit-dummy-node-id"));
+        when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
 
         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
-        when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker));
+        when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodePath));
         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
         when(mockedDeviceContext.getMultiMsgCollector(
                 Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
@@ -183,9 +182,12 @@ public class StatisticsManagerImplTest {
                 Matchers.eq(StatisticsManagerControlService.class),
                 Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
 
-        statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, false, conductor);
+        final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
+        final long basicTimerDelay = 3000L;
+        final long maximumTimerDelay = 900000L;
+        statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, false, new HashedWheelTimer(),
+                convertorManager, basicTimerDelay, maximumTimerDelay);
         statisticsManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
-        when(conductor.getDeviceContext(deviceInfo)).thenReturn(mockedDeviceContext);
     }
 
     @Test
@@ -202,9 +204,12 @@ public class StatisticsManagerImplTest {
         }).when(outboundQueue)
                 .commitEntry(Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
 
+        Mockito.when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
+        Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
+
         statisticsManager.setDeviceInitializationPhaseHandler(mockedDevicePhaseHandler);
-        statisticsManager.onDeviceContextLevelUp(deviceInfo);
-        verify(mockedDevicePhaseHandler).onDeviceContextLevelUp(deviceInfo);
+        statisticsManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
+        verify(mockedDevicePhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
     }
 
     @Test
@@ -219,7 +224,7 @@ public class StatisticsManagerImplTest {
         statisticsManager.onDeviceContextLevelDown(deviceInfo);
         verify(statisticContext).close();
         verify(mockedTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
-        Assert.assertEquals(0, contextsMap.size());
+        Assert.assertEquals(1, contextsMap.size());
     }
 
     private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
@@ -253,6 +258,10 @@ public class StatisticsManagerImplTest {
         when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
                 Collections.<ItemLifeCycleSource>emptyList());
 
+        when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
+        when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
+        when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
+
         getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
 
         final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
@@ -290,6 +299,10 @@ public class StatisticsManagerImplTest {
 
         getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
 
+        when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
+        when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
+        when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
+
         final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
                 new ChangeStatisticsWorkModeInputBuilder()
                         .setMode(StatisticsWorkMode.FULLYDISABLED);
@@ -326,6 +339,10 @@ public class StatisticsManagerImplTest {
 
         getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
 
+        when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
+        when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
+        when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
+
         final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
                 new ChangeStatisticsWorkModeInputBuilder()
                         .setMode(StatisticsWorkMode.FULLYDISABLED);
@@ -375,10 +392,8 @@ public class StatisticsManagerImplTest {
         statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
         verify(mockedDeviceContext).getDeviceState();
 
-        when(mockedDeviceContext.getDeviceState().isValid()).thenReturn(true);
         statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
 
-        when(mockedDeviceContext.getDeviceState().isStatisticsPollingEnabled()).thenReturn(true);
         statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
 
         when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
@@ -391,4 +406,4 @@ public class StatisticsManagerImplTest {
         statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
         Mockito.verify(mockTimerCounter,times(2)).addTimeMark();
     }
-}
\ No newline at end of file
+}