*/
package org.opendaylight.openflowplugin.impl.statistics;
-import static org.mockito.Mockito.times;
+import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import com.google.common.util.concurrent.Futures;
-import io.netty.util.HashedWheelTimer;
-import io.netty.util.Timeout;
+import com.google.common.util.concurrent.MoreExecutors;
import java.lang.reflect.Field;
import java.math.BigInteger;
-import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Captor;
import org.mockito.Matchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
-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.lifecycle.ReconciliationFrameworkRegistrar;
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;
@Mock
private MultiMsgCollector multiMagCollector;
@Mock
- private ItemLifeCycleRegistry itemLifeCycleRegistry;
- @Captor
- private ArgumentCaptor<ItemLifecycleListener> itemLifeCycleListenerCapt;
- @Mock
private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> serviceControlRegistration;
@Mock
private DeviceInfo deviceInfo;
@Mock
private DataBroker dataBroker;
+ @Mock
+ private ReconciliationFrameworkRegistrar reconciliationFrameworkRegistrar;
private RequestContext<List<MultipartReply>> currentRequestContext;
private StatisticsManagerImpl statisticsManager;
when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
- when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, 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(
+ Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
invocation -> {
currentRequestContext = (RequestContext<List<MultipartReply>>) invocation.getArguments()[0];
return multiMagCollector;
- }
- );
- when(mockedDeviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
+ });
when(rpcProviderRegistry.addRpcImplementation(
Matchers.eq(StatisticsManagerControlService.class),
Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
.setBasicTimerDelay(new NonZeroUint32Type(basicTimerDelay))
.setMaximumTimerDelay(new NonZeroUint32Type(maximumTimerDelay))
.setIsStatisticsPollingOn(false)
- .build(), rpcProviderRegistry, new HashedWheelTimer(),
- convertorManager);
+ .build(), rpcProviderRegistry,
+ convertorManager,
+ MoreExecutors.newDirectExecutorService());
}
private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
throws NoSuchFieldException, IllegalAccessException {
// HACK: contexts map for testing shall be accessed in some more civilized way
final Field contextsField = StatisticsManagerImpl.class.getDeclaredField("contexts");
- Assert.assertNotNull(contextsField);
+ assertNotNull(contextsField);
contextsField.setAccessible(true);
return (Map<DeviceInfo, StatisticsContext>) contextsField.get(statisticsManager);
}
final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
Assert.assertTrue(workMode.isDone());
Assert.assertTrue(workMode.get().isSuccessful());
- Assert.assertNotNull(workMode.get().getResult());
+ assertNotNull(workMode.get().getResult());
Assert.assertEquals(StatisticsWorkMode.COLLECTALL, workMode.get().getResult().getMode());
}
/**
- * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry
+ * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry.
*
- * @throws Exception
*/
@Test
public void testChangeStatisticsWorkMode1() throws Exception {
final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
- when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
- Collections.<ItemLifeCycleSource>emptyList());
-
- when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
- when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
.changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
checkWorkModeChangeOutcome(workMode);
- verify(itemLifeCycleRegistry).getLifeCycleSources();
- verify(statisticContext).stopGatheringData();
+ verify(statisticContext).disableGathering();
}
- private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode) throws InterruptedException, ExecutionException {
+ private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode)
+ throws InterruptedException, ExecutionException {
Assert.assertTrue(workMode.isDone());
Assert.assertTrue(workMode.get().isSuccessful());
}
/**
- * switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry
+ * Switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry.
*
- * @throws Exception
*/
@Test
public void testChangeStatisticsWorkMode2() throws Exception {
- final Timeout pollTimeout = Mockito.mock(Timeout.class);
- final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
- when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
- Collections.singletonList(itemLifecycleSource));
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);
- Future<RpcResult<Void>> workMode = statisticsManager.changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
+ Future<RpcResult<Void>> workMode = statisticsManager
+ .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
checkWorkModeChangeOutcome(workMode);
- verify(itemLifeCycleRegistry).getLifeCycleSources();
- verify(statisticContext).stopGatheringData();
- verify(itemLifecycleSource).setItemLifecycleListener(Matchers.<ItemLifecycleListener>any());
+ verify(statisticContext).disableGathering();
}
/**
- * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
- * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
+ * Tests switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
+ * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout.
*
- * @throws Exception
*/
@Test
public void testChangeStatisticsWorkMode3() throws Exception {
- final Timeout pollTimeout = Mockito.mock(Timeout.class);
- final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
- Mockito.doNothing().when(itemLifecycleSource)
- .setItemLifecycleListener(itemLifeCycleListenerCapt.capture());
-
final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
- when(statisticContext.getItemLifeCycleListener()).thenReturn(
- Mockito.mock(ItemLifecycleListener.class));
- when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
- Collections.singletonList(itemLifecycleSource));
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);
changeStatisticsWorkModeInputBld.build());
checkWorkModeChangeOutcome(workMode);
+ verify(statisticContext).disableGathering();
+
changeStatisticsWorkModeInputBld.setMode(StatisticsWorkMode.COLLECTALL);
workMode = statisticsManager.changeStatisticsWorkMode(
changeStatisticsWorkModeInputBld.build());
checkWorkModeChangeOutcome(workMode);
- verify(itemLifeCycleRegistry, times(2)).getLifeCycleSources();
- verify(statisticContext).stopGatheringData();
-
- final List<ItemLifecycleListener> itemLifeCycleListenerValues = itemLifeCycleListenerCapt.getAllValues();
- Assert.assertEquals(2, itemLifeCycleListenerValues.size());
- Assert.assertNotNull(itemLifeCycleListenerValues.get(0));
- Assert.assertNull(itemLifeCycleListenerValues.get(1));
+ verify(statisticContext).enableGathering();
}
@Test
statisticsManager.close();
verify(serviceControlRegistration).close();
}
-
- @Test
- public void testCalculateTimerDelay() throws Exception {
- final TimeCounter timeCounter = Mockito.mock(TimeCounter.class);
- when(timeCounter.getAverageTimeBetweenMarks()).thenReturn(2000L, (Long)4000L);
- statisticsManager.calculateTimerDelay(timeCounter);
- Assert.assertEquals(3000L, statisticsManager.getCurrentTimerDelay());
- statisticsManager.calculateTimerDelay(timeCounter);
- Assert.assertEquals(6000L, statisticsManager.getCurrentTimerDelay());
- }
-
- @Test
- public void testPollStatistics() throws Exception {
- final StatisticsContext statisticsContext = Mockito.mock(StatisticsContext.class);
- final TimeCounter mockTimerCounter = Mockito.mock(TimeCounter.class);
-
- statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
- verify(mockedDeviceContext).getDeviceState();
-
- statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
-
- statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
-
- when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
- when(statisticsContext.isSchedulingEnabled()).thenReturn(Boolean.TRUE);
- statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
- verify(mockTimerCounter).markStart();
- verify(mockTimerCounter).addTimeMark();
-
- when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFailedFuture(new Throwable("error msg")));
- statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
- verify(mockTimerCounter,times(2)).addTimeMark();
- }
}