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.junit.Assert.assertNotNull;
11 import static org.mockito.Mockito.verify;
12 import static org.mockito.Mockito.when;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import java.lang.reflect.Field;
16 import java.math.BigInteger;
17 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.Future;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Matchers;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.runners.MockitoJUnitRunner;
29 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
30 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
31 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
32 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
33 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
34 import org.opendaylight.openflowplugin.api.OFConstants;
35 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo;
38 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
39 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
41 import org.opendaylight.openflowplugin.api.openflow.lifecycle.ReconciliationFrameworkRegistrar;
42 import org.opendaylight.openflowplugin.api.openflow.statistics.StatisticsContext;
43 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
44 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
45 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
46 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfigBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.ChangeStatisticsWorkModeInputBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.GetStatisticsWorkModeOutput;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsManagerControlService;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.sm.control.rev150812.StatisticsWorkMode;
59 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 @RunWith(MockitoJUnitRunner.class)
66 public class StatisticsManagerImplTest {
68 private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagerImplTest.class);
70 public static final NodeId NODE_ID = new NodeId("ofp-unit-dummy-node-id");
73 private ConnectionContext mockedPrimConnectionContext;
75 private FeaturesReply mockedFeatures;
77 private ConnectionAdapter mockedConnectionAdapter;
79 private MessageSpy mockedMessagSpy;
81 private DeviceContext mockedDeviceContext;
83 private DeviceState mockedDeviceState;
85 private DeviceInfo mockedDeviceInfo;
87 private RpcProviderRegistry rpcProviderRegistry;
89 private OutboundQueue outboundQueue;
91 private MultiMsgCollector multiMagCollector;
93 private BindingAwareBroker.RpcRegistration<StatisticsManagerControlService> serviceControlRegistration;
95 private DeviceInfo deviceInfo;
97 private DataBroker dataBroker;
99 private ReconciliationFrameworkRegistrar reconciliationFrameworkRegistrar;
101 private RequestContext<List<MultipartReply>> currentRequestContext;
102 private StatisticsManagerImpl statisticsManager;
106 public void initialization() {
107 final KeyedInstanceIdentifier<Node, NodeKey> nodePath = KeyedInstanceIdentifier
109 .child(Node.class, new NodeKey(new NodeId("openflow:10")));
111 when(mockedPrimConnectionContext.getFeatures()).thenReturn(mockedFeatures);
112 when(mockedPrimConnectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
113 when(mockedPrimConnectionContext.getConnectionState()).thenReturn(ConnectionContext.CONNECTION_STATE.WORKING);
114 when(mockedPrimConnectionContext.getNodeId()).thenReturn(NODE_ID);
115 when(mockedPrimConnectionContext.getOutboundQueueProvider()).thenReturn(outboundQueue);
117 when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
118 when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
119 when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
120 when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
121 when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
122 when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
123 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(nodePath);
124 when(mockedDeviceInfo.getDatapathId()).thenReturn(BigInteger.TEN);
125 when(mockedDeviceInfo.getNodeId()).thenReturn(NODE_ID);
127 when(mockedDeviceContext.getDeviceInfo()).thenReturn(mockedDeviceInfo);
128 when(mockedDeviceContext.getPrimaryConnectionContext()).thenReturn(mockedPrimConnectionContext);
129 when(mockedDeviceContext.getMessageSpy()).thenReturn(mockedMessagSpy);
130 when(mockedDeviceContext.getDeviceFlowRegistry())
131 .thenReturn(new DeviceFlowRegistryImpl(OFConstants.OFP_VERSION_1_3, dataBroker, nodePath));
132 when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
133 when(mockedDeviceContext.getMultiMsgCollector(
134 Matchers.<RequestContext<List<MultipartReply>>>any())).thenAnswer(
136 currentRequestContext = (RequestContext<List<MultipartReply>>) invocation.getArguments()[0];
137 return multiMagCollector;
140 when(rpcProviderRegistry.addRpcImplementation(
141 Matchers.eq(StatisticsManagerControlService.class),
142 Matchers.<StatisticsManagerControlService>any())).thenReturn(serviceControlRegistration);
144 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
145 final long basicTimerDelay = 3000L;
146 final long maximumTimerDelay = 900000L;
148 statisticsManager = new StatisticsManagerImpl(
149 new OpenflowProviderConfigBuilder()
150 .setBasicTimerDelay(new NonZeroUint32Type(basicTimerDelay))
151 .setMaximumTimerDelay(new NonZeroUint32Type(maximumTimerDelay))
152 .setIsStatisticsPollingOn(false)
153 .build(), rpcProviderRegistry,
155 MoreExecutors.newDirectExecutorService());
158 private static Map<DeviceInfo, StatisticsContext> getContextsMap(final StatisticsManagerImpl statisticsManager)
159 throws NoSuchFieldException, IllegalAccessException {
160 // HACK: contexts map for testing shall be accessed in some more civilized way
161 final Field contextsField = StatisticsManagerImpl.class.getDeclaredField("contexts");
162 assertNotNull(contextsField);
163 contextsField.setAccessible(true);
164 return (Map<DeviceInfo, StatisticsContext>) contextsField.get(statisticsManager);
168 public void testGetStatisticsWorkMode() throws Exception {
169 final Future<RpcResult<GetStatisticsWorkModeOutput>> workMode = statisticsManager.getStatisticsWorkMode();
170 Assert.assertTrue(workMode.isDone());
171 Assert.assertTrue(workMode.get().isSuccessful());
172 assertNotNull(workMode.get().getResult());
173 Assert.assertEquals(StatisticsWorkMode.COLLECTALL, workMode.get().getResult().getMode());
177 * switching to {@link StatisticsWorkMode#FULLYDISABLED}; no pollTimeout and no lifecycleRegistry.
182 public void testChangeStatisticsWorkMode1() throws Exception {
183 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
185 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
187 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
188 new ChangeStatisticsWorkModeInputBuilder()
189 .setMode(StatisticsWorkMode.FULLYDISABLED);
191 final Future<RpcResult<Void>> workMode = statisticsManager
192 .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
194 checkWorkModeChangeOutcome(workMode);
195 verify(statisticContext).disableGathering();
198 private static void checkWorkModeChangeOutcome(Future<RpcResult<Void>> workMode)
199 throws InterruptedException, ExecutionException {
200 Assert.assertTrue(workMode.isDone());
201 Assert.assertTrue(workMode.get().isSuccessful());
206 * Switching to {@link StatisticsWorkMode#FULLYDISABLED}; with pollTimeout and lifecycleRegistry.
211 public void testChangeStatisticsWorkMode2() throws Exception {
212 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
214 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
216 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
217 new ChangeStatisticsWorkModeInputBuilder()
218 .setMode(StatisticsWorkMode.FULLYDISABLED);
220 Future<RpcResult<Void>> workMode = statisticsManager
221 .changeStatisticsWorkMode(changeStatisticsWorkModeInputBld.build());
222 checkWorkModeChangeOutcome(workMode);
224 verify(statisticContext).disableGathering();
228 * switching to {@link StatisticsWorkMode#FULLYDISABLED} and back
229 * to {@link StatisticsWorkMode#COLLECTALL}; with lifecycleRegistry and pollTimeout
234 public void testChangeStatisticsWorkMode3() throws Exception {
235 final StatisticsContext statisticContext = Mockito.mock(StatisticsContext.class);
237 getContextsMap(statisticsManager).put(deviceInfo, statisticContext);
239 final ChangeStatisticsWorkModeInputBuilder changeStatisticsWorkModeInputBld =
240 new ChangeStatisticsWorkModeInputBuilder()
241 .setMode(StatisticsWorkMode.FULLYDISABLED);
243 Future<RpcResult<Void>> workMode;
244 workMode = statisticsManager.changeStatisticsWorkMode(
245 changeStatisticsWorkModeInputBld.build());
246 checkWorkModeChangeOutcome(workMode);
248 verify(statisticContext).disableGathering();
250 changeStatisticsWorkModeInputBld.setMode(StatisticsWorkMode.COLLECTALL);
251 workMode = statisticsManager.changeStatisticsWorkMode(
252 changeStatisticsWorkModeInputBld.build());
253 checkWorkModeChangeOutcome(workMode);
255 verify(statisticContext).enableGathering();
259 public void testClose() throws Exception {
260 statisticsManager.close();
261 verify(serviceControlRegistration).close();