17672c9efeb845bb20ad91e5603842d803047081
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / StatisticsContextImplParamTest.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
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import static com.google.common.util.concurrent.Futures.immediateFuture;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
17
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import java.util.Arrays;
21 import java.util.Collections;
22 import java.util.List;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.junit.runners.Parameterized;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
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
40 @RunWith(Parameterized.class)
41 public class StatisticsContextImplParamTest extends StatisticsContextImpMockInitiation {
42
43     @Mock
44     private OpenflowProviderConfig config = mock(OpenflowProviderConfig.class);
45
46     public StatisticsContextImplParamTest(final boolean isTable, final boolean isFlow,
47                                           final boolean isGroup, final boolean isMeter,
48                                           final boolean isPort, final boolean isQueue) {
49         super();
50         this.isTable = isTable;
51         this.isFlow = isFlow;
52         this.isGroup = isGroup;
53         this.isMeter = isMeter;
54         this.isPort = isPort;
55         this.isQueue = isQueue;
56     }
57
58     @Parameterized.Parameters(name = "{index}")
59     public static Iterable<Object[]> data1() {
60         return Arrays.asList(new Object[][]{
61                 {false, true, false, false, false, false},
62                 {true, false, false, false, false, false},
63                 {false, false, true, false, false, false},
64                 {false, false, false, true, false, false},
65                 {false, false, false, false, true, false},
66                 {false, false, false, false, false, true},
67         });
68     }
69
70     @Before
71     public void setUp() {
72         Mockito.when(config.isIsTableStatisticsPollingOn()).thenReturn(true);
73         Mockito.when(config.isIsFlowStatisticsPollingOn()).thenReturn(true);
74         Mockito.when(config.isIsGroupStatisticsPollingOn()).thenReturn(true);
75         Mockito.when(config.isIsMeterStatisticsPollingOn()).thenReturn(true);
76         Mockito.when(config.isIsPortStatisticsPollingOn()).thenReturn(true);
77         Mockito.when(config.isIsQueueStatisticsPollingOn()).thenReturn(true);
78         Mockito.when(config.getBasicTimerDelay()).thenReturn(new NonZeroUint32Type(3000L));
79         Mockito.when(config.getMaximumTimerDelay()).thenReturn(new NonZeroUint32Type(50000L));
80     }
81
82     @Test
83     public void gatherDynamicDataTest() throws InterruptedException {
84
85         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
86         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
87         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
88         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
89         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
90         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
91         when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
92
93         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
94         final StatisticsContextImpl<MultipartReply> statisticsContext = new StatisticsContextImpl<>(
95                 mockedDeviceContext, convertorManager,
96                 MultipartWriterProviderFactory.createDefaultProvider(mockedDeviceContext),
97                 MoreExecutors.newDirectExecutorService(),
98                 config,
99                 true,
100                 false);
101
102         final ListenableFuture<RpcResult<List<MultipartReply>>> rpcResult = immediateFuture(RpcResultBuilder
103                 .success(Collections.<MultipartReply>emptyList()).build());
104         when(mockedStatisticsGatheringService.getStatisticsOfType(any(EventIdentifier.class), any(MultipartType
105                 .class))).thenReturn(rpcResult);
106         when(mockedStatisticsOnFlyGatheringService.getStatisticsOfType(any(EventIdentifier.class), any(MultipartType
107                 .class))).thenReturn(rpcResult);
108
109         statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
110         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
111         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
112         statisticsContext.instantiateServiceInstance();
113
114         verify(mockedStatisticsGatheringService, times(7))
115                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
116         verify(mockedStatisticsOnFlyGatheringService)
117                 .getStatisticsOfType(any(EventIdentifier.class), any(MultipartType.class));
118     }
119
120 }