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