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