Merge "OPNFLWPLUG-963 : Updating ports delete reason from OFP:"
[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
9 package org.opendaylight.openflowplugin.impl.statistics;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
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.Matchers;
25 import org.mockito.Mockito;
26 import org.mockito.runners.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.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class StatisticsContextImplTest extends StatisticsContextImpMockInitiation {
41
42     private static final Logger LOG = LoggerFactory.getLogger(StatisticsContextImplTest.class);
43
44     private static final Long TEST_XID = 55L;
45     private StatisticsContextImpl<MultipartReply> statisticsContext;
46     private ConvertorManager convertorManager;
47
48     @Before
49     public void setUp() throws Exception {
50         convertorManager = ConvertorManagerFactory.createDefaultManager();
51         when(mockedDeviceInfo.reserveXidForDeviceMessage()).thenReturn(TEST_XID);
52         Mockito.when(mockedDeviceContext.getDeviceState()).thenReturn(mockedDeviceState);
53         initStatisticsContext();
54     }
55
56     private void initStatisticsContext() {
57         statisticsContext = new StatisticsContextImpl<>(mockedDeviceContext, convertorManager,
58                                                         MultipartWriterProviderFactory
59                                                                 .createDefaultProvider(mockedDeviceContext),
60                                                         MoreExecutors.newDirectExecutorService(), true, false, 3000,
61                                                         50000);
62
63         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
64         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
65     }
66
67     @Test
68     public void testCreateRequestContext() {
69         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
70         assertNotNull(requestContext);
71         assertEquals(TEST_XID, requestContext.getXid().getValue());
72         Assert.assertFalse(requestContext.getFuture().isDone());
73     }
74
75     /**
76      * There is nothing to check in close method.
77      */
78     @Test
79     @SuppressWarnings("checkstyle:IllegalCatch")
80     public void testClose() throws Exception {
81         statisticsContext =
82                 new StatisticsContextImpl<>(mockedDeviceContext,
83                                             convertorManager,
84                                             MultipartWriterProviderFactory
85                                                     .createDefaultProvider(mockedDeviceContext),
86                                             MoreExecutors.newDirectExecutorService(),
87                                             true,
88                                             false,
89                                             3000,50000);
90
91         final RequestContext<Object> requestContext = statisticsContext.createRequestContext();
92         statisticsContext.close();
93         try {
94             Assert.assertTrue(requestContext.getFuture().isDone());
95             final RpcResult<?> rpcResult = requestContext.getFuture().get();
96             Assert.assertFalse(rpcResult.isSuccessful());
97             Assert.assertFalse(rpcResult.isSuccessful());
98         } catch (final Exception e) {
99             LOG.error("request future value should be finished", e);
100             Assert.fail("request context closing failed");
101         }
102     }
103
104     @Test
105     public void testGatherDynamicData_none() throws Exception {
106         statisticsContext.instantiateServiceInstance();
107         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
108     }
109
110     @Test
111     public void testGatherDynamicData_all() throws Exception {
112         Mockito.reset(mockedDeviceState);
113         when(mockedDeviceState.isTableStatisticsAvailable()).thenReturn(Boolean.TRUE);
114         when(mockedDeviceState.isFlowStatisticsAvailable()).thenReturn(Boolean.TRUE);
115         when(mockedDeviceState.isGroupAvailable()).thenReturn(Boolean.TRUE);
116         when(mockedDeviceState.isMetersAvailable()).thenReturn(Boolean.TRUE);
117         when(mockedDeviceState.isPortStatisticsAvailable()).thenReturn(Boolean.TRUE);
118         when(mockedDeviceState.isQueueStatisticsAvailable()).thenReturn(Boolean.TRUE);
119         when(mockedDeviceInfo.getNodeInstanceIdentifier()).thenReturn(DUMMY_NODE_ID);
120         initStatisticsContext();
121
122         when(mockedStatisticsGatheringService
123                      .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class)))
124                 .thenReturn(Futures.immediateFuture(
125                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
126         when(mockedStatisticsOnFlyGatheringService
127                      .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class)))
128                 .thenReturn(Futures.immediateFuture(
129                         RpcResultBuilder.success(Collections.<MultipartReply>emptyList()).build()));
130
131         statisticsContext.registerMastershipWatcher(mockedMastershipWatcher);
132         statisticsContext.setStatisticsGatheringService(mockedStatisticsGatheringService);
133         statisticsContext.setStatisticsGatheringOnTheFlyService(mockedStatisticsOnFlyGatheringService);
134         statisticsContext.instantiateServiceInstance();
135
136         verify(mockedStatisticsGatheringService, times(7))
137                 .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
138         verify(mockedStatisticsOnFlyGatheringService)
139                 .getStatisticsOfType(Matchers.any(EventIdentifier.class), Matchers.any(MultipartType.class));
140         Mockito.verifyNoMoreInteractions(mockedStatisticsGatheringService, mockedStatisticsOnFlyGatheringService);
141     }
142 }