Update MRI upstreams for Phosphorus
[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.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.util.concurrent.Futures;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import java.util.Collections;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
30 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
31 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProviderFactory;
32 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
33 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.NonZeroUint32Type;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.provider.config.rev160510.OpenflowProviderConfig;
38 import org.opendaylight.yangtools.yang.common.RpcResult;
39 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
40 import org.opendaylight.yangtools.yang.common.Uint32;
41
42 @RunWith(MockitoJUnitRunner.class)
43 public class StatisticsContextImplTest extends StatisticsContextImpMockInitiation {
44     private static final Uint32 TEST_XID = Uint32.valueOf(55);
45
46     private StatisticsContextImpl<MultipartReply> statisticsContext;
47     private ConvertorManager convertorManager;
48     @Mock
49     private final OpenflowProviderConfig config =
50             Mockito.mock(OpenflowProviderConfig.class);
51
52     @Before
53     public void setUp() {
54         convertorManager = ConvertorManagerFactory.createDefaultManager();
55         when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
56         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
57         when(config.getIsTableStatisticsPollingOn()).thenReturn(true);
58         when(config.getIsFlowStatisticsPollingOn()).thenReturn(true);
59         when(config.getIsGroupStatisticsPollingOn()).thenReturn(true);
60         when(config.getIsMeterStatisticsPollingOn()).thenReturn(true);
61         when(config.getIsPortStatisticsPollingOn()).thenReturn(true);
62         when(config.getIsQueueStatisticsPollingOn()).thenReturn(true);
63         when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(Uint32.valueOf(3000)));
64         when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(Uint32.valueOf(50000)));
65
66         initStatisticsContext();
67     }
68
69     private void initStatisticsContext() {
70         statisticsContext = new StatisticsContextImpl<>(mockedDeviceContext, convertorManager,
71                 MultipartWriterProviderFactory
72                         .createDefaultProvider(mockedDeviceContext),
73                 MoreExecutors.directExecutor(),
74                 config,
75                 true,
76                 false);
77
78         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
79         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
80     }
81
82     @Test
83     public void testCreateRequestContext() {
84         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
85         assertNotNull(requestContext);
86         assertEquals(TEST_XID, requestContext.getXid().getValue());
87         assertFalse(requestContext.getFuture().isDone());
88     }
89
90     /**
91      * There is nothing to check in close method.
92      */
93     @Test
94     public void testClose() throws InterruptedException, ExecutionException {
95         statisticsContext =
96                 new StatisticsContextImpl<>(mockedDeviceContext,
97                         convertorManager,
98                         MultipartWriterProviderFactory
99                                 .createDefaultProvider(mockedDeviceContext),
100                         MoreExecutors.directExecutor(),
101                         config,
102                         true,
103                         false);
104
105         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
106         statisticsContext.close();
107         assertTrue(requestContext.getFuture().isDone());
108         final RpcResult<?> rpcResult = requestContext.getFuture().get();
109         assertFalse(rpcResult.isSuccessful());
110     }
111
112     @Test
113     public void testGatherDynamicData_none() {
114         statisticsContext.instantiateServiceInstance();
115         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
116     }
117
118     @Test
119     public void testGatherDynamicData_all() {
120         Mockito.reset(mockedDeviceState);
121         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
122         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
123         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
124         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
125         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
126         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
127         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
128         initStatisticsContext();
129
130         when(mockedStatisticsGatheringService
131                      .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
132                 .thenReturn(Futures.immediateFuture(
133                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
134         when(mockedStatisticsOnFlyGatheringService
135                      .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class)))
136                 .thenReturn(Futures.immediateFuture(
137                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
138
139         statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
140         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
141         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
142         statisticsContext.initializeDevice();
143
144         verify(mockedStatisticsGatheringService, times(7))
145                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
146         verify(mockedStatisticsOnFlyGatheringService)
147                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
148         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
149     }
150 }