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.times;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
14 import com.google.common.util.concurrent.FutureCallback;
15 import com.google.common.util.concurrent.Futures;
16 import io.netty.util.HashedWheelTimer;
17 import io.netty.util.Timeout;
18 import java.lang.reflect.Field;
19 import java.math.BigInteger;
20 import java.util.Collections;
21 import java.util.List;
23 import java.util.Optional;
24 import java.util.concurrent.ExecutionException;
25 import java.util.concurrent.Future;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Captor;
32 import org.mockito.Matchers;
33 import org.mockito.Mock;
34 import org.mockito.Mockito;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.mockito.stubbing.Answer;
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
40 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
41 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
42 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
43 import org.opendaylight.openflowplugin.api.OFConstants;
44 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
45 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
46 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
47 import org.opendaylight.openflowplugin.api.openflow.device.DeviceManager;
48 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
49 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
50 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
51 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceInitializationPhaseHandler;
52 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceTerminationPhaseHandler;
53 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
54 import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleService;
55 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
56 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
57 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
58 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
59 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
60 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
61 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
62 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInputBuilder;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
76 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
77 import org.opendaylight.yangtools.yang.common.RpcResult;
78 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
83 @RunWith(MockitoJUnitRunner.class)
84 public class StatisticsManagerImplTest {
86 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImplTest.class);
88 public static final NodeId NODE_ID = new NodeId("ofp-unit-dummy-node-id");
91 RequestContextStack mockedRequestContextStack;
93 ConnectionContext mockedPrimConnectionContext;
95 FeaturesReply mockedFeatures;
97 GetFeaturesOutput mockedFeaturesOutput;
99 ConnectionAdapter mockedConnectionAdapter;
101 MessageSpy mockedMessagSpy;
103 DeviceContext mockedDeviceContext;
105 DeviceState mockedDeviceState;
107 DeviceInfo mockedDeviceInfo;
109 DeviceInitializationPhaseHandler mockedDevicePhaseHandler;
111 DeviceTerminationPhaseHandler mockedTerminationPhaseHandler;
113 private RpcProviderRegistry rpcProviderRegistry;
115 private HashedWheelTimer hashedWheelTimer;
117 private OutboundQueue outboundQueue;
119 private MultiMsgCollector multiMagCollector;
121 private ItemLifeCycleRegistry itemLifeCycleRegistry;
123 private ArgumentCaptor<ItemLifecycleListener> itemLifeCycleListenerCapt;
125 private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> serviceControlRegistration;
127 private DeviceManager deviceManager;
129 private GetFeaturesOutput featuresOutput;
131 private DeviceInitializationPhaseHandler deviceInitializationPhaseHandler;
133 private DeviceInfo deviceInfo;
135 private DataBroker dataBroker;
137 private LifecycleService lifecycleService;
139 private RequestContext<List<MultipartReply>> currentRequestContext;
140 private StatisticsManagerImpl statisticsManager;
144 public void initialization() {
145 final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
147 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
149 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
150 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
151 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
152 when(mockedPrimConnectionContext.getNodeId()).thenReturn(NODE_ID);
153 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
155 when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
156 when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
157 when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
158 when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
159 when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
160 when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
161 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
162 when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
163 when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
165 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
166 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
167 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
168 when(mockedDeviceContext.getDeviceFlowRegistry()).thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodePath));
169 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
170 when(mockedDeviceContext.getMultiMsgCollector(
171 Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
172 new Answer<MultiMsgCollector>() {
174 public MultiMsgCollector answer(InvocationOnMock invocation) throws Throwable {
175 currentRequestContext = (RequestContext<List<MultipartReply>>) invocation.getArguments()[0];
176 return multiMagCollector;
180 when(mockedDeviceContext.getItemLifeCycleSourceRegistry()).thenReturn(itemLifeCycleRegistry);
181 when(rpcProviderRegistry.addRpcImplementation(
182 Matchers.eq(StatisticsManagerControlService.class),
183 Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
185 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
186 final long basicTimerDelay = 3000L;
187 final long maximumTimerDelay = 900000L;
188 statisticsManager = new StatisticsManagerImpl(rpcProviderRegistry, false, new HashedWheelTimer(),
189 convertorManager, basicTimerDelay, maximumTimerDelay);
190 statisticsManager.setDeviceInitializationPhaseHandler(deviceInitializationPhaseHandler);
194 public void testOnDeviceContextLevelUp() throws Exception {
195 Mockito.doAnswer(new Answer<Void>() {
197 public Void answer(InvocationOnMock invocation) throws Throwable {
198 final FutureCallback<OfHeader> callback = (FutureCallback<OfHeader>) invocation.getArguments()[2];
199 LOG.debug("committing entry: {}", ((MultipartRequestInput) invocation.getArguments()[1]).getType());
200 callback.onSuccess(null);
201 currentRequestContext.setResult(RpcResultBuilder.<List<MultipartReply>>success().build());
204 }).when(outboundQueue)
205 .commitEntry(Matchers.anyLong(), Matchers.<OfHeader>any(), Matchers.<FutureCallback<OfHeader>>any());
207 Mockito.when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
208 Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
210 statisticsManager.setDeviceInitializationPhaseHandler(mockedDevicePhaseHandler);
211 statisticsManager.onDeviceContextLevelUp(deviceInfo, lifecycleService);
212 verify(mockedDevicePhaseHandler).onDeviceContextLevelUp(deviceInfo, lifecycleService);
216 public void testOnDeviceContextClosed() throws Exception {
217 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
218 final Map<DeviceInfo, StatisticsContext> contextsMap = getContextsMap(statisticsManager);
220 contextsMap.put(deviceInfo, statisticContext);
221 Assert.assertEquals(1, contextsMap.size());
223 statisticsManager.setDeviceTerminationPhaseHandler(mockedTerminationPhaseHandler);
224 statisticsManager.onDeviceContextLevelDown(deviceInfo);
225 verify(statisticContext).close();
226 verify(mockedTerminationPhaseHandler).onDeviceContextLevelDown(deviceInfo);
227 Assert.assertEquals(1, contextsMap.size());
230 private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
231 throws NoSuchFieldException, IllegalAccessException {
232 // HACK: contexts map for testing shall be accessed in some more civilized way
233 final Field contextsField = StatisticsManagerImpl.class.getDeclaredField("contexts");
234 Assert.assertNotNull(contextsField);
235 contextsField.setAccessible(true);
236 return (Map<DeviceInfo, StatisticsContext>) contextsField.get(statisticsManager);
240 public void testGetStatisticsWorkMode() throws Exception {
241 final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
242 Assert.assertTrue(workMode.isDone());
243 Assert.assertTrue(workMode.get().isSuccessful());
244 Assert.assertNotNull(workMode.get().getResult());
245 Assert.assertEquals(StatisticsWorkMode.COLLECTALL, workMode.get().getResult().getMode());
249 * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry
254 public void testChangeStatisticsWorkMode1() throws Exception {
255 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
256 when(statisticContext.getPollTimeout()).thenReturn(
257 Optional.<Timeout>empty());
258 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
259 Collections.<ItemLifeCycleSource>emptyList());
261 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
262 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
263 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
265 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
267 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
268 new ChangeStatisticsWorkModeInputBuilder()
269 .setMode(StatisticsWorkMode.FULLYDISABLED);
271 final Future<RpcResult<Void>> workMode = statisticsManager
272 .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
274 checkWorkModeChangeOutcome(workMode);
275 Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
276 Mockito.verify(statisticContext).getPollTimeout();
279 private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode) throws InterruptedException, ExecutionException {
280 Assert.assertTrue(workMode.isDone());
281 Assert.assertTrue(workMode.get().isSuccessful());
286 * switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry
291 public void testChangeStatisticsWorkMode2() throws Exception {
292 final Timeout pollTimeout = Mockito.mock(Timeout.class);
293 final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
294 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
295 when(statisticContext.getPollTimeout()).thenReturn(
296 Optional.of(pollTimeout));
297 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
298 Collections.singletonList(itemLifecycleSource));
300 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
302 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
303 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
304 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
306 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
307 new ChangeStatisticsWorkModeInputBuilder()
308 .setMode(StatisticsWorkMode.FULLYDISABLED);
310 Future<RpcResult<Void>> workMode = statisticsManager.changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
311 checkWorkModeChangeOutcome(workMode);
313 Mockito.verify(itemLifeCycleRegistry).getLifeCycleSources();
314 Mockito.verify(statisticContext).getPollTimeout();
315 Mockito.verify(pollTimeout).cancel();
316 Mockito.verify(itemLifecycleSource).setItemLifecycleListener(Matchers.<ItemLifecycleListener>any());
320 * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
321 * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
326 public void testChangeStatisticsWorkMode3() throws Exception {
327 final Timeout pollTimeout = Mockito.mock(Timeout.class);
328 final ItemLifeCycleSource itemLifecycleSource = Mockito.mock(ItemLifeCycleSource.class);
329 Mockito.doNothing().when(itemLifecycleSource)
330 .setItemLifecycleListener(itemLifeCycleListenerCapt.capture());
332 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
333 when(statisticContext.getPollTimeout()).thenReturn(
334 Optional.of(pollTimeout));
335 when(statisticContext.getItemLifeCycleListener()).thenReturn(
336 Mockito.mock(ItemLifecycleListener.class));
337 when(itemLifeCycleRegistry.getLifeCycleSources()).thenReturn(
338 Collections.singletonList(itemLifecycleSource));
340 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
342 when(statisticContext.gainDeviceContext()).thenReturn(mockedDeviceContext);
343 when(statisticContext.gainDeviceState()).thenReturn(mockedDeviceState);
344 when(lifecycleService.getDeviceContext()).thenReturn(mockedDeviceContext);
346 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
347 new ChangeStatisticsWorkModeInputBuilder()
348 .setMode(StatisticsWorkMode.FULLYDISABLED);
350 Future<RpcResult<Void>> workMode;
351 workMode = statisticsManager.changeStatisticsWorkMode(
352 changeStatisticsWorkModeInputBld.build());
353 checkWorkModeChangeOutcome(workMode);
355 changeStatisticsWorkModeInputBld.setMode(StatisticsWorkMode.COLLECTALL);
356 workMode = statisticsManager.changeStatisticsWorkMode(
357 changeStatisticsWorkModeInputBld.build());
358 checkWorkModeChangeOutcome(workMode);
360 Mockito.verify(itemLifeCycleRegistry, Mockito.times(2)).getLifeCycleSources();
361 Mockito.verify(statisticContext).getPollTimeout();
362 Mockito.verify(pollTimeout).cancel();
364 final List<ItemLifecycleListener> itemLifeCycleListenerValues = itemLifeCycleListenerCapt.getAllValues();
365 Assert.assertEquals(2, itemLifeCycleListenerValues.size());
366 Assert.assertNotNull(itemLifeCycleListenerValues.get(0));
367 Assert.assertNull(itemLifeCycleListenerValues.get(1));
371 public void testClose() throws Exception {
372 statisticsManager.close();
373 Mockito.verify(serviceControlRegistration).close();
377 public void testCalculateTimerDelay() throws Exception {
378 final TimeCounter timeCounter = Mockito.mock(TimeCounter.class);
379 when(timeCounter.getAverageTimeBetweenMarks()).thenReturn(2000L, (Long)4000L);
381 statisticsManager.calculateTimerDelay(timeCounter);
382 Assert.assertEquals(3000L, StatisticsManagerImpl.getCurrentTimerDelay());
383 statisticsManager.calculateTimerDelay(timeCounter);
384 Assert.assertEquals(6000L, StatisticsManagerImpl.getCurrentTimerDelay());
388 public void testPollStatistics() throws Exception {
389 final StatisticsContext statisticsContext = Mockito.mock(StatisticsContext.class);
390 final TimeCounter mockTimerCounter = Mockito.mock(TimeCounter.class);
392 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
393 verify(mockedDeviceContext).getDeviceState();
395 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
397 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
399 when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateCheckedFuture(Boolean.TRUE));
400 when(statisticsContext.isSchedulingEnabled()).thenReturn(Boolean.TRUE);
401 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
402 Mockito.verify(mockTimerCounter).markStart();
403 Mockito.verify(mockTimerCounter).addTimeMark();
405 when(statisticsContext.gatherDynamicData()).thenReturn(Futures.immediateFailedFuture(new Throwable("error msg")));
406 statisticsManager.pollStatistics(mockedDeviceContext.getDeviceState(), statisticsContext, mockTimerCounter, mockedDeviceInfo);
407 Mockito.verify(mockTimerCounter,times(2)).addTimeMark();