Merge "BUG-4034: Missing barrier after flow remove"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightFlowStatisticsServiceImpl1Test.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.services;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import java.util.concurrent.Future;
13 import org.junit.Assert;
14 import org.junit.Test;
15 import org.mockito.ArgumentCaptor;
16 import org.mockito.Captor;
17 import org.mockito.Matchers;
18 import org.mockito.Mockito;
19 import org.opendaylight.openflowplugin.impl.rpc.AbstractRequestContext;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInputBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32
33 /**
34  * Test for {@link OpendaylightFlowStatisticsServiceImpl}
35  */
36 public class OpendaylightFlowStatisticsServiceImpl1Test extends AbstractStatsServiceTest {
37
38     @Captor
39     private ArgumentCaptor<MultipartRequestInput> requestInput;
40
41     private AbstractRequestContext<Object> rqContext;
42
43     private OpendaylightFlowStatisticsServiceImpl flowStatisticsService;
44
45
46     public void setUp() {
47         flowStatisticsService = new OpendaylightFlowStatisticsServiceImpl(rqContextStack, deviceContext);
48
49         rqContext = new AbstractRequestContext<Object>(42L) {
50             @Override
51             public void close() {
52                 //NOOP
53             }
54         };
55         Mockito.when(rqContextStack.<Object>createRequestContext()).thenReturn(rqContext);
56     }
57
58     @Test
59     public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
60         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
61                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
62
63         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
64                 .setNode(createNodeRef("unitProt:123"))
65                 .setTableId(new TableId((short) 1));
66
67         final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
68                 = flowStatisticsService.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
69
70         Assert.assertTrue(resultFuture.isDone());
71         final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResult = resultFuture.get();
72         Assert.assertTrue(rpcResult.isSuccessful());
73         Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
74     }
75
76     @Test
77     public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
78         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
79                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
80
81         GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
82                 .setNode(createNodeRef("unitProt:123"))
83                 .setTableId(new TableId((short) 1));
84
85         final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
86                 = flowStatisticsService.getAllFlowStatisticsFromFlowTable(input.build());
87
88         Assert.assertTrue(resultFuture.isDone());
89         final RpcResult<GetAllFlowStatisticsFromFlowTableOutput> rpcResult = resultFuture.get();
90         Assert.assertTrue(rpcResult.isSuccessful());
91         Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
92     }
93
94     @Test
95     public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
96         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
97                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
98
99         GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
100                 .setNode(createNodeRef("unitProt:123"));
101
102         final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
103                 = flowStatisticsService.getAllFlowsStatisticsFromAllFlowTables(input.build());
104
105         Assert.assertTrue(resultFuture.isDone());
106         final RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput> rpcResult = resultFuture.get();
107         Assert.assertTrue(rpcResult.isSuccessful());
108         Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
109     }
110
111     @Test
112     public void testGetFlowStatisticsFromFlowTable() throws Exception {
113         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
114                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
115
116         GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
117                 .setNode(createNodeRef("unitProt:123"))
118                 .setPriority(5);
119
120         final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
121                 = flowStatisticsService.getFlowStatisticsFromFlowTable(input.build());
122
123         Assert.assertTrue(resultFuture.isDone());
124         final RpcResult<GetFlowStatisticsFromFlowTableOutput> rpcResult = resultFuture.get();
125         Assert.assertTrue(rpcResult.isSuccessful());
126         Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
127     }
128 }