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 281ab5953ec27eacf4829c4f73e21e0556577968..461c92c5df90fafa27732cc406480f9ccd5dde73 100644 (file)
@@ -51,7 +51,6 @@ 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;
@@ -86,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
@@ -129,8 +126,6 @@ public class StatisticsManagerImplTest {
     @Mock
     private DeviceManager deviceManager;
     @Mock
-    private LifecycleConductor conductor;
-    @Mock
     private GetFeaturesOutput featuresOutput;
     @Mock
     private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
@@ -170,7 +165,7 @@ public class StatisticsManagerImplTest {
         when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
         when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
         when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
-        when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(dataBroker, nodePath));
+        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(
@@ -188,9 +183,11 @@ public class StatisticsManagerImplTest {
                 Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
 
         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
-        statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, false, conductor, convertorManager);
+        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
@@ -207,6 +204,9 @@ 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, lifecycleService);
         verify(mockedDevicePhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
@@ -224,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)
@@ -258,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 =
@@ -295,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);
@@ -331,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);