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
9 package org.opendaylight.openflowplugin.impl.statistics;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Collections;
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.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
29 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
30 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
37 import org.opendaylight.yangtools.yang.common.RpcResult;
38 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 @RunWith(MockitoJUnitRunner.class)
43 public class StatisticsContextImplTest extends StatisticsContextImpMockInitiation {
45 private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImplTest.class);
47 private static final Long TEST_XID = 55L;
48 private StatisticsContextImpl<MultipartReply> statisticsContext;
49 private ConvertorManager convertorManager;
51 private OpenflowProviderConfig config =
52 Mockito.mock(OpenflowProviderConfig.class);
55 public void setUp() throws Exception {
56 convertorManager = ConvertorManagerFactory.createDefaultManager();
57 when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
58 Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
59 Mockito.when(config.isIsTableStatisticsPollingOn()).thenReturn(true);
60 Mockito.when(config.isIsFlowStatisticsPollingOn()).thenReturn(true);
61 Mockito.when(config.isIsGroupStatisticsPollingOn()).thenReturn(true);
62 Mockito.when(config.isIsMeterStatisticsPollingOn()).thenReturn(true);
63 Mockito.when(config.isIsPortStatisticsPollingOn()).thenReturn(true);
64 Mockito.when(config.isIsQueueStatisticsPollingOn()).thenReturn(true);
65 Mockito.when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(3000L));
66 Mockito.when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(50000L));
68 initStatisticsContext();
71 private void initStatisticsContext() {
72 statisticsContext = new StatisticsContextImpl<>(mockedDeviceContext, convertorManager,
73 MultipartWriterProviderFactory
74 .createDefaultProvider(mockedDeviceContext),
75 MoreExecutors.newDirectExecutorService(),
80 statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
81 statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
85 public void testCreateRequestContext() {
86 final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
87 assertNotNull(requestContext);
88 assertEquals(TEST_XID, requestContext.getXid().getValue());
89 Assert.assertFalse(requestContext.getFuture().isDone());
93 * There is nothing to check in close method.
96 @SuppressWarnings("checkstyle:IllegalCatch")
97 public void testClose() throws Exception {
99 new StatisticsContextImpl<>(mockedDeviceContext,
101 MultipartWriterProviderFactory
102 .createDefaultProvider(mockedDeviceContext),
103 MoreExecutors.newDirectExecutorService(),
108 final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
109 statisticsContext.close();
111 Assert.assertTrue(requestContext.getFuture().isDone());
112 final RpcResult<?> rpcResult = requestContext.getFuture().get();
113 Assert.assertFalse(rpcResult.isSuccessful());
114 Assert.assertFalse(rpcResult.isSuccessful());
115 } catch (final Exception e) {
116 LOG.error("request future value should be finished", e);
117 Assert.fail("request context closing failed");
122 public void testGatherDynamicData_none() throws Exception {
123 statisticsContext.instantiateServiceInstance();
124 Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
128 public void testGatherDynamicData_all() throws Exception {
129 Mockito.reset(mockedDeviceState);
130 when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
131 when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
132 when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
133 when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
134 when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
135 when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
136 when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
137 initStatisticsContext();
139 when(mockedStatisticsGatheringService
140 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
141 .thenReturn(Futures.immediateFuture(
142 RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
143 when(mockedStatisticsOnFlyGatheringService
144 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
145 .thenReturn(Futures.immediateFuture(
146 RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
148 statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
149 statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
150 statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
151 statisticsContext.instantiateServiceInstance();
153 verify(mockedStatisticsGatheringService, times(7))
154 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
155 verify(mockedStatisticsOnFlyGatheringService)
156 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
157 Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);