Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / compatibility / OpendaylightFlowStatisticsServiceDelegateImplTest.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.services.compatibility;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.timeout;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.util.concurrent.FutureCallback;
20 import java.util.List;
21 import java.util.concurrent.atomic.AtomicLong;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
27 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
28 import org.opendaylight.openflowplugin.impl.statistics.services.AbstractSingleStatsServiceTest;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
31 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregateBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
57 import org.opendaylight.yangtools.yang.binding.Notification;
58 import org.opendaylight.yangtools.yang.common.RpcResult;
59 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
60 import org.opendaylight.yangtools.yang.common.Uint16;
61 import org.opendaylight.yangtools.yang.common.Uint32;
62 import org.opendaylight.yangtools.yang.common.Uint64;
63 import org.opendaylight.yangtools.yang.common.Uint8;
64
65 /**
66  * Test for {@link GetFlowStatisticsFromFlowTableImpl}.
67  * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
68  */
69 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
70     public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
71
72     @Mock
73     private MessageTranslator<Object, Object> translator;
74     @Captor
75     private ArgumentCaptor<MultipartRequestInput> requestInput;
76
77     private GetFlowStatisticsFromFlowTableImpl getFlowStatisticsFromFlowTable;
78     private GetAllFlowStatisticsFromFlowTableImpl getAllFlowStatisticsFromFlowTable;
79     private GetAllFlowsStatisticsFromAllFlowTablesImpl getAllFlowsStatisticsFromAllFlowTables;
80     private GetAggregateFlowStatisticsFromFlowTableForAllFlowsImpl getAggregateFlowStatisticsFromFlowTableForAllFlows;
81
82     @Override
83     public void setUp() {
84         final var convertorManager = ConvertorManagerFactory.createDefaultManager();
85         final var xid = new AtomicLong(21);
86         getFlowStatisticsFromFlowTable = new GetFlowStatisticsFromFlowTableImpl(
87                 rqContextStack, deviceContext, convertorManager, xid, notificationPublishService);
88         getAllFlowStatisticsFromFlowTable = new GetAllFlowStatisticsFromFlowTableImpl(
89             rqContextStack, deviceContext, convertorManager, xid, notificationPublishService);
90         getAllFlowsStatisticsFromAllFlowTables = new GetAllFlowsStatisticsFromAllFlowTablesImpl(
91             rqContextStack, deviceContext, convertorManager, xid, notificationPublishService);
92         getAggregateFlowStatisticsFromFlowTableForAllFlows = new GetAggregateFlowStatisticsFromFlowTableForAllFlowsImpl(
93             rqContextStack, deviceContext, convertorManager, xid, notificationPublishService);
94
95         doAnswer(answerVoidToCallback).when(outboundQueueProvider)
96                 .commitEntry(eq(Uint32.valueOf(42)), requestInput.capture(), any(FutureCallback.class));
97         when(translatorLibrary.lookupTranslator(any())).thenReturn(translator);
98     }
99
100     @Test
101     public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
102         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
103                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
104                 .setNode(createNodeRef("unitProt:123"))
105                 .setTableId(new TableId(Uint8.ONE));
106
107         when(translator.translate(any(MultipartReply.class), eq(deviceInfo),any()))
108                 .thenReturn(new AggregatedFlowStatisticsBuilder()
109                         .setByteCount(new Counter64(Uint64.valueOf(50)))
110                         .setPacketCount(new Counter64(Uint64.valueOf(51)))
111                         .setFlowCount(new Counter32(Uint32.valueOf(52)))
112                         .build());
113
114         rpcResult = RpcResultBuilder.<Object>success(List.of(new MultipartReplyMessageBuilder()
115                 .setType(MultipartType.OFPMPAGGREGATE)
116                 .setVersion(EncodeConstants.OF_VERSION_1_3)
117                 .setFlags(new MultipartRequestFlags(false))
118                 .setMultipartReplyBody(new MultipartReplyAggregateCaseBuilder()
119                         .setMultipartReplyAggregate(new MultipartReplyAggregateBuilder()
120                                 .setByteCount(Uint64.valueOf(50))
121                                 .setPacketCount(Uint64.valueOf(51))
122                                 .setFlowCount(Uint32.valueOf(52))
123                                 .build())
124                         .build())
125                 .build()))
126                 .build();
127
128         final var resultFuture = getAggregateFlowStatisticsFromFlowTableForAllFlows.invoke(input.build());
129
130         assertTrue(resultFuture.isDone());
131         final var rpcResultCompatible = resultFuture.get();
132         assertTrue(rpcResultCompatible.isSuccessful());
133         assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
134
135         verify(notificationPublishService, timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
136                 .offerNotification(any(Notification.class));
137     }
138
139     @Test
140     public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
141         GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
142                 .setNode(createNodeRef("unitProt:123"))
143                 .setTableId(new TableId(Uint8.ONE));
144
145         rpcResult = buildFlowStatsReply();
146
147         final var resultFuture = getAllFlowStatisticsFromFlowTable.invoke(input.build());
148
149         assertTrue(resultFuture.isDone());
150         final var rpcResultCompatible = resultFuture.get();
151         assertTrue(rpcResultCompatible.isSuccessful());
152         assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
153
154         verify(notificationPublishService, timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
155                 .offerNotification(any(Notification.class));
156     }
157
158     private static RpcResult<Object> buildFlowStatsReply() {
159         return RpcResultBuilder.<Object>success(List.of(new MultipartReplyMessageBuilder()
160                 .setType(MultipartType.OFPMPFLOW)
161                 .setVersion(EncodeConstants.OF_VERSION_1_3)
162                 .setFlags(new MultipartRequestFlags(false))
163                 .setMultipartReplyBody(new MultipartReplyFlowCaseBuilder()
164                     .setMultipartReplyFlow(new MultipartReplyFlowBuilder()
165                         .setFlowStats(List.of(new FlowStatsBuilder()
166                             .setTableId(Uint8.valueOf(123))
167                             .setDurationSec(Uint32.TEN)
168                             .setDurationNsec(Uint32.valueOf(11))
169                             .setByteCount(Uint64.valueOf(12))
170                             .setPacketCount(Uint64.valueOf(13))
171                             .setCookie(Uint64.ZERO)
172                             .setPriority(Uint16.valueOf(14))
173                             .setMatch(new MatchBuilder().setMatchEntry(List.of()).build())
174                             .setHardTimeout(Uint16.valueOf(15))
175                             .setIdleTimeout(Uint16.valueOf(16))
176                             .setFlags(new FlowModFlags(true, false, false, false, false))
177                             .setInstruction(List.of(new InstructionBuilder()
178                                 .setInstructionChoice(new ApplyActionsCaseBuilder()
179                                     .setApplyActions(new ApplyActionsBuilder()
180                                         .setAction(List.of(new ActionBuilder()
181                                             .setActionChoice(new OutputActionCaseBuilder()
182                                                 .setOutputAction(new OutputActionBuilder()
183                                                     .setMaxLength(Uint16.valueOf(17))
184                                                     .setPort(new PortNumber(Uint32.valueOf(18)))
185                                                     .build())
186                                                 .build())
187                                             .build()))
188                                         .build())
189                                     .build())
190                                 .build()))
191                             .build()))
192                         .build())
193                     .build())
194                 .build()))
195             .build();
196     }
197
198     @Test
199     public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
200         GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
201                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
202                 .setNode(createNodeRef("unitProt:123"));
203
204         rpcResult = buildFlowStatsReply();
205
206         final var resultFuture = getAllFlowsStatisticsFromAllFlowTables.invoke(input.build());
207
208         assertTrue(resultFuture.isDone());
209         final var rpcResultCompatible = resultFuture.get();
210         assertTrue(rpcResultCompatible.isSuccessful());
211         assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
212
213         verify(notificationPublishService, timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
214                 .offerNotification(any(Notification.class));
215     }
216
217     @Test
218     public void testGetFlowStatisticsFromFlowTable() throws Exception {
219         GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
220                 .setNode(createNodeRef("unitProt:123"))
221                 .setTableId(Uint8.ONE)
222                 .setPriority(Uint16.valueOf(123))
223                 .setOutPort(Uint64.ONE);
224
225         rpcResult = buildFlowStatsReply();
226
227         final var resultFuture = getFlowStatisticsFromFlowTable.invoke(input.build());
228
229         assertTrue(resultFuture.isDone());
230         final var rpcResultCompatible = resultFuture.get();
231         assertTrue(rpcResultCompatible.isSuccessful());
232         assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
233
234         verify(notificationPublishService, timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
235                 .offerNotification(any(Notification.class));
236     }
237 }