2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.statistics;
10 import static org.mockito.Mockito.*;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import java.lang.reflect.Field;
15 import java.math.BigInteger;
16 import java.util.Collections;
17 import java.util.List;
19 import java.util.Optional;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Future;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.mockito.Matchers;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.mockito.stubbing.Answer;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
36 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
37 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
38 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
39 import org.opendaylight.openflowplugin.api.OFConstants;
40 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
41 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
42 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
43 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
44 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
45 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
46 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
47 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
48 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
49 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
50 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
51 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
52 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
53 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
54 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
55 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
56 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
57 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
58 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInputBuilder;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
72 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
73 import org.opendaylight.yangtools.yang.common.RpcResult;
74 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
75 import org.slf4j.Logger;
76 import org.slf4j.LoggerFactory;
77 import io.netty.util.HashedWheelTimer;
78 import io.netty.util.Timeout;
81 @RunWith(MockitoJUnitRunner.class)
82 public class StatisticsManagerImplTest {
84 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImplTest.class);
86 public static final NodeId NODE_ID = new NodeId("ofp-unit-dummy-node-id");
89 RequestContextStack mockedRequestContextStack;
91 ConnectionContext mockedPrimConnectionContext;
93 FeaturesReply mockedFeatures;
95 GetFeaturesOutput mockedFeaturesOutput;
97 ConnectionAdapter mockedConnectionAdapter;
99 MessageSpy mockedMessagSpy;
101 DeviceContext mockedDeviceContext;
103 DeviceState mockedDeviceState;
105 DeviceInfo mockedDeviceInfo;
107 DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
109 DeviceTerminationPhaseHandler mockedTerminationPhaseHandler;
111 private RpcProviderRegistry rpcProviderRegistry;
113 private HashedWheelTimer hashedWheelTimer;
115 private OutboundQueue outboundQueue;
117 private MultiMsgCollector multiMagCollector;
119 private ItemLifeCycleRegistry itemLifeCycleRegistry;
121 private ArgumentCaptor<ItemLifecycleListener> itemLifeCycleListenerCapt;
123 private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> serviceControlRegistration;
125 private DeviceManager deviceManager;
127 private GetFeaturesOutput featuresOutput;
129 private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
131 private DeviceInfo deviceInfo;
133 private DataBroker dataBroker;
135 private LifecycleService lifecycleService;
137 private RequestContext<List<MultipartReply>> currentRequestContext;
138 private StatisticsManagerImpl statisticsManager;
142 public void initialization() {
143 final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
145 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
147 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
148 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
149 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
150 when(mockedPrimConnectionContext.getNodeId()).thenReturn(NODE_ID);
151 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
153 when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
154 when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
155 when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
156 when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
157 when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
158 when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
159 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
160 when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
161 when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
163 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
164 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
165 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
166 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodePath));
167 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
168 when(mockedDeviceContext.getMultiMsgCollector(
169 Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
170 new Answer<MultiMsgCollector>() {
172 public MultiMsgCollector answer(InvocationOnMock invocation) throws Throwable {
173 currentRequestContext = (RequestContext<List<MultipartReply>>) invocation.getArguments()[0];
174 return multiMagCollector;
178 when(mockedDeviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
179 when(rpcProviderRegistry.addRpcImplementation(
180 Matchers.eq(StatisticsManagerControlService.class),
181 Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
183 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
184 final long basicTimerDelay = 3000L;
185 final long maximumTimerDelay = 900000L;
186 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, false, new HashedWheelTimer(),
187 convertorManager, basicTimerDelay, maximumTimerDelay);
188 statisticsManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
192 public void testOnDeviceContextLevelUp() throws Exception {
193 Mockito.doAnswer(new Answer<Void>() {
195 public Void answer(InvocationOnMock invocation) throws Throwable {
196 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
197 LOG.debug("committing entry: {}", ((MultipartRequestInput) invocation.getArguments()[1]).getType());
198 callback.onSuccess(null);
199 currentRequestContext.setResult(RpcResultBuilder.<List<MultipartReply>>success().build());
202 }).when(outboundQueue)
203 .commitEntry(Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
205 Mockito.when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
206 Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
208 statisticsManager.setDeviceInitializationPhaseHandler(mockedDevicePhaseHandler);
209 statisticsManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
210 verify(mockedDevicePhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
214 public void testOnDeviceContextClosed() throws Exception {
215 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
216 final Map<DeviceInfo, StatisticsContext> contextsMap = getContextsMap(statisticsManager);
218 contextsMap.put(deviceInfo, statisticContext);
219 Assert.assertEquals(1, contextsMap.size());
221 statisticsManager.setDeviceTerminationPhaseHandler(mockedTerminationPhaseHandler);
222 statisticsManager.onDeviceContextLevelDown(deviceInfo);
223 verify(statisticContext).close();
224 verify(mockedTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
225 Assert.assertEquals(1, contextsMap.size());
228 private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
229 throws NoSuchFieldException, IllegalAccessException {
230 // HACK: contexts map for testing shall be accessed in some more civilized way
231 final Field contextsField = StatisticsManagerImpl.class.getDeclaredField("contexts");
232 Assert.assertNotNull(contextsField);
233 contextsField.setAccessible(true);
234 return (Map<DeviceInfo, StatisticsContext>) contextsField.get(statisticsManager);
238 public void testGetStatisticsWorkMode() throws Exception {
239 final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
240 Assert.assertTrue(workMode.isDone());
241 Assert.assertTrue(workMode.get().isSuccessful());
242 Assert.assertNotNull(workMode.get().getResult());
243 Assert.assertEquals(StatisticsWorkMode.COLLECTALL, workMode.get().getResult().getMode());
247 * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry
252 public void testChangeStatisticsWorkMode1() throws Exception {
253 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
254 when(statisticContext.getPollTimeout()).thenReturn(
255 Optional.<Timeout>empty());
256 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
257 Collections.<ItemLifeCycleSource>emptyList());
259 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
260 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
261 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
263 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
265 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
266 new ChangeStatisticsWorkModeInputBuilder()
267 .setMode(StatisticsWorkMode.FULLYDISABLED);
269 final Future<RpcResult<Void>> workMode = statisticsManager
270 .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
272 checkWorkModeChangeOutcome(workMode);
273 Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
274 Mockito.verify(statisticContext).getPollTimeout();
277 private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode) throws InterruptedException, ExecutionException {
278 Assert.assertTrue(workMode.isDone());
279 Assert.assertTrue(workMode.get().isSuccessful());
284 * switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry
289 public void testChangeStatisticsWorkMode2() throws Exception {
290 final Timeout pollTimeout = Mockito.mock(Timeout.class);
291 final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
292 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
293 when(statisticContext.getPollTimeout()).thenReturn(
294 Optional.of(pollTimeout));
295 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
296 Collections.singletonList(itemLifecycleSource));
298 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
300 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
301 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
302 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
304 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
305 new ChangeStatisticsWorkModeInputBuilder()
306 .setMode(StatisticsWorkMode.FULLYDISABLED);
308 Future<RpcResult<Void>> workMode = statisticsManager.changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
309 checkWorkModeChangeOutcome(workMode);
311 Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
312 Mockito.verify(statisticContext).getPollTimeout();
313 Mockito.verify(pollTimeout).cancel();
314 Mockito.verify(itemLifecycleSource).setItemLifecycleListener(Matchers.<ItemLifecycleListener>any());
318 * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
319 * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
324 public void testChangeStatisticsWorkMode3() throws Exception {
325 final Timeout pollTimeout = Mockito.mock(Timeout.class);
326 final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
327 Mockito.doNothing().when(itemLifecycleSource)
328 .setItemLifecycleListener(itemLifeCycleListenerCapt.capture());
330 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
331 when(statisticContext.getPollTimeout()).thenReturn(
332 Optional.of(pollTimeout));
333 when(statisticContext.getItemLifeCycleListener()).thenReturn(
334 Mockito.mock(ItemLifecycleListener.class));
335 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
336 Collections.singletonList(itemLifecycleSource));
338 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
340 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
341 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
342 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
344 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
345 new ChangeStatisticsWorkModeInputBuilder()
346 .setMode(StatisticsWorkMode.FULLYDISABLED);
348 Future<RpcResult<Void>> workMode;
349 workMode = statisticsManager.changeStatisticsWorkMode(
350 changeStatisticsWorkModeInputBld.build());
351 checkWorkModeChangeOutcome(workMode);
353 changeStatisticsWorkModeInputBld.setMode(StatisticsWorkMode.COLLECTALL);
354 workMode = statisticsManager.changeStatisticsWorkMode(
355 changeStatisticsWorkModeInputBld.build());
356 checkWorkModeChangeOutcome(workMode);
358 Mockito.verify(itemLifeCycleRegistry, Mockito.times(2)).getLifeCycleSources();
359 Mockito.verify(statisticContext).getPollTimeout();
360 Mockito.verify(pollTimeout).cancel();
362 final List<ItemLifecycleListener> itemLifeCycleListenerValues = itemLifeCycleListenerCapt.getAllValues();
363 Assert.assertEquals(2, itemLifeCycleListenerValues.size());
364 Assert.assertNotNull(itemLifeCycleListenerValues.get(0));
365 Assert.assertNull(itemLifeCycleListenerValues.get(1));
369 public void testClose() throws Exception {
370 statisticsManager.close();
371 Mockito.verify(serviceControlRegistration).close();
375 public void testCalculateTimerDelay() throws Exception {
376 final TimeCounter timeCounter = Mockito.mock(TimeCounter.class);
377 when(timeCounter.getAverageTimeBetweenMarks()).thenReturn(2000L, (Long)4000L);
378 statisticsManager.calculateTimerDelay(timeCounter);
379 Assert.assertEquals(3000L, StatisticsManagerImpl.getCurrentTimerDelay());
380 statisticsManager.calculateTimerDelay(timeCounter);
381 Assert.assertEquals(6000L, StatisticsManagerImpl.getCurrentTimerDelay());
385 public void testPollStatistics() throws Exception {
386 final StatisticsContext statisticsContext = Mockito.mock(StatisticsContext.class);
387 final TimeCounter mockTimerCounter = Mockito.mock(TimeCounter.class);
389 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
390 verify(mockedDeviceContext).getDeviceState();
392 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
394 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
396 when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
397 when(statisticsContext.isSchedulingEnabled()).thenReturn(Boolean.TRUE);
398 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
399 Mockito.verify(mockTimerCounter).markStart();
400 Mockito.verify(mockTimerCounter).addTimeMark();
402 when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFailedFuture(new Throwable("error msg")));
403 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
404 Mockito.verify(mockTimerCounter,times(2)).addTimeMark();