Switch to MD-SAL APIs
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImplTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.statistics;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.Collections;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
28 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
29 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
31 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
36 import org.opendaylight.yangtools.yang.common.RpcResult;
37 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class StatisticsContextImplTest extends StatisticsContextImpMockInitiation {
43
44     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImplTest.class);
45
46     private static final Long TEST_XID = 55L;
47     private StatisticsContextImpl<MultipartReply> statisticsContext;
48     private ConvertorManager convertorManager;
49     @Mock
50     private final OpenflowProviderConfig config =
51             Mockito.mock(OpenflowProviderConfig.class);
52
53     @Before
54     public void setUp() throws Exception {
55         convertorManager = ConvertorManagerFactory.createDefaultManager();
56         when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
57         Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
58         Mockito.when(config.isIsTableStatisticsPollingOn()).thenReturn(true);
59         Mockito.when(config.isIsFlowStatisticsPollingOn()).thenReturn(true);
60         Mockito.when(config.isIsGroupStatisticsPollingOn()).thenReturn(true);
61         Mockito.when(config.isIsMeterStatisticsPollingOn()).thenReturn(true);
62         Mockito.when(config.isIsPortStatisticsPollingOn()).thenReturn(true);
63         Mockito.when(config.isIsQueueStatisticsPollingOn()).thenReturn(true);
64         Mockito.when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(3000L));
65         Mockito.when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(50000L));
66
67         initStatisticsContext();
68     }
69
70     private void initStatisticsContext() {
71         statisticsContext = new StatisticsContextImpl<>(mockedDeviceContext, convertorManager,
72                 MultipartWriterProviderFactory
73                         .createDefaultProvider(mockedDeviceContext),
74                 MoreExecutors.newDirectExecutorService(),
75                 config,
76                 true,
77                 false);
78
79         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
80         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
81     }
82
83     @Test
84     public void testCreateRequestContext() {
85         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
86         assertNotNull(requestContext);
87         assertEquals(TEST_XID, requestContext.getXid().getValue());
88         Assert.assertFalse(requestContext.getFuture().isDone());
89     }
90
91     /**
92      * There is nothing to check in close method.
93      */
94     @Test
95     @SuppressWarnings("checkstyle:IllegalCatch")
96     public void testClose() throws Exception {
97         statisticsContext =
98                 new StatisticsContextImpl<>(mockedDeviceContext,
99                         convertorManager,
100                         MultipartWriterProviderFactory
101                                 .createDefaultProvider(mockedDeviceContext),
102                         MoreExecutors.newDirectExecutorService(),
103                         config,
104                         true,
105                         false);
106
107         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
108         statisticsContext.close();
109         try {
110             Assert.assertTrue(requestContext.getFuture().isDone());
111             final RpcResult<?> rpcResult = requestContext.getFuture().get();
112             Assert.assertFalse(rpcResult.isSuccessful());
113             Assert.assertFalse(rpcResult.isSuccessful());
114         } catch (final Exception e) {
115             LOG.error("request future value should be finished", e);
116             Assert.fail("request context closing failed");
117         }
118     }
119
120     @Test
121     public void testGatherDynamicData_none() throws Exception {
122         statisticsContext.instantiateServiceInstance();
123         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
124     }
125
126     @Test
127     public void testGatherDynamicData_all() throws Exception {
128         Mockito.reset(mockedDeviceState);
129         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
130         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
131         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
132         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
133         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
134         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
135         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
136         initStatisticsContext();
137
138         when(mockedStatisticsGatheringService
139                      .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
140                 .thenReturn(Futures.immediateFuture(
141                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
142         when(mockedStatisticsOnFlyGatheringService
143                      .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
144                 .thenReturn(Futures.immediateFuture(
145                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
146
147         statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
148         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
149         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
150         statisticsContext.instantiateServiceInstance();
151
152         verify(mockedStatisticsGatheringService, times(7))
153                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
154         verify(mockedStatisticsOnFlyGatheringService)
155                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
156         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
157     }
158 }