2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.statistics.services.compatibility;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.eq;
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;
69 * Test for {@link OpendaylightFlowStatisticsServiceDelegateImpl}.
70 * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
72 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
74 public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
76 private ArgumentCaptor<MultipartRequestInput> requestInput;
78 private OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsServiceDelegate;
80 private MessageTranslator<Object, Object> translator;
84 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
85 flowStatisticsServiceDelegate = new OpendaylightFlowStatisticsServiceDelegateImpl(
86 rqContextStack, deviceContext, notificationPublishService, new AtomicLong(21), convertorManager);
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);
93 @Test(expected = IllegalAccessError.class)
94 public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
95 flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForGivenMatch(null);
99 public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
100 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
101 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
102 .setNode(createNodeRef("unitProt:123"))
103 .setTableId(new TableId((short) 1));
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))
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))
126 final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
127 = flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
129 Assert.assertTrue(resultFuture.isDone());
130 final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResultCompatible =
132 Assert.assertTrue(rpcResultCompatible.isSuccessful());
133 Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
135 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
136 .offerNotification(any(Notification.class));
140 public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
141 GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
142 .setNode(createNodeRef("unitProt:123"))
143 .setTableId(new TableId((short) 1));
145 rpcResult = buildFlowStatsReply();
147 final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
148 = flowStatisticsServiceDelegate.getAllFlowStatisticsFromFlowTable(input.build());
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());
155 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
156 .offerNotification(any(Notification.class));
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)
169 .setDurationNsec(11L)
170 .setByteCount(BigInteger.valueOf(12L))
171 .setPacketCount(BigInteger.valueOf(13L))
172 .setCookie(BigInteger.ZERO)
174 .setMatch(new MatchBuilder()
175 .setMatchEntry(Collections.<MatchEntry>emptyList())
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()
186 new OutputActionBuilder()
188 .setPort(new PortNumber(18L))
203 public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
204 GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
205 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
206 .setNode(createNodeRef("unitProt:123"));
208 rpcResult = buildFlowStatsReply();
210 final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
211 = flowStatisticsServiceDelegate.getAllFlowsStatisticsFromAllFlowTables(input.build());
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());
218 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
219 .offerNotification(any(Notification.class));
223 public void testGetFlowStatisticsFromFlowTable() throws Exception {
224 GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
225 .setNode(createNodeRef("unitProt:123"))
226 .setTableId((short) 1)
228 .setOutPort(BigInteger.ONE);
230 rpcResult = buildFlowStatsReply();
232 final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
233 = flowStatisticsServiceDelegate.getFlowStatisticsFromFlowTable(input.build());
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());
240 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
241 .offerNotification(any(Notification.class));