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 com.google.common.util.concurrent.FutureCallback;
12 import java.math.BigInteger;
13 import java.util.Collections;
14 import java.util.concurrent.Future;
15 import java.util.concurrent.atomic.AtomicLong;
16 import org.junit.Assert;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.Captor;
20 import org.mockito.Matchers;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
25 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
26 import org.opendaylight.openflowplugin.impl.statistics.services.AbstractSingleStatsServiceTest;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter32;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.Counter64;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowStatisticsFromFlowTableOutput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
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.GetFlowStatisticsFromFlowTableOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCaseBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregateBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
59 import org.opendaylight.yangtools.yang.binding.Notification;
60 import org.opendaylight.yangtools.yang.common.RpcResult;
61 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
64 * Test for {@link OpendaylightFlowStatisticsServiceDelegateImpl}.
65 * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
67 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
69 public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
71 private ArgumentCaptor<MultipartRequestInput> requestInput;
73 private OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsServiceDelegate;
75 private MessageTranslator<Object, Object> translator;
79 flowStatisticsServiceDelegate = new OpendaylightFlowStatisticsServiceDelegateImpl(
80 rqContextStack, deviceContext, notificationPublishService, new AtomicLong(21));
82 Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
83 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
84 Mockito.when(translatorLibrary.lookupTranslator(Matchers.<TranslatorKey>any())).thenReturn(translator);
87 @Test(expected = IllegalAccessError.class)
88 public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
89 flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForGivenMatch(null);
93 public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
94 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
95 .setNode(createNodeRef("unitProt:123"))
96 .setTableId(new TableId((short) 1));
98 Mockito.when(translator.translate(Matchers.any(MultipartReply.class), Matchers.eq(deviceState), Matchers.any()))
99 .thenReturn(new AggregatedFlowStatisticsBuilder()
100 .setByteCount(new Counter64(BigInteger.valueOf(50L)))
101 .setPacketCount(new Counter64(BigInteger.valueOf(51L)))
102 .setFlowCount(new Counter32(52L))
105 rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(new MultipartReplyMessageBuilder()
106 .setType(MultipartType.OFPMPAGGREGATE)
107 .setVersion(OFConstants.OFP_VERSION_1_3)
108 .setFlags(new MultipartRequestFlags(false))
109 .setMultipartReplyBody(new MultipartReplyAggregateCaseBuilder()
110 .setMultipartReplyAggregate(new MultipartReplyAggregateBuilder()
111 .setByteCount(BigInteger.valueOf(50L))
112 .setPacketCount(BigInteger.valueOf(51L))
119 final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
120 = flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
122 Assert.assertTrue(resultFuture.isDone());
123 final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResultCompatible = resultFuture.get();
124 Assert.assertTrue(rpcResultCompatible.isSuccessful());
125 Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
127 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
131 public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
132 GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
133 .setNode(createNodeRef("unitProt:123"))
134 .setTableId(new TableId((short) 1));
136 rpcResult = buildFlowStatsReply();
138 final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
139 = flowStatisticsServiceDelegate.getAllFlowStatisticsFromFlowTable(input.build());
141 Assert.assertTrue(resultFuture.isDone());
142 final RpcResult<GetAllFlowStatisticsFromFlowTableOutput> rpcResultCompatible = resultFuture.get();
143 Assert.assertTrue(rpcResultCompatible.isSuccessful());
144 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
146 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
149 private static RpcResult<Object> buildFlowStatsReply() {
150 return RpcResultBuilder.<Object>success(Collections.singletonList(new MultipartReplyMessageBuilder()
151 .setType(MultipartType.OFPMPFLOW)
152 .setVersion(OFConstants.OFP_VERSION_1_3)
153 .setFlags(new MultipartRequestFlags(false))
154 .setMultipartReplyBody(new MultipartReplyFlowCaseBuilder()
155 .setMultipartReplyFlow(new MultipartReplyFlowBuilder()
156 .setFlowStats(Collections.singletonList(new FlowStatsBuilder()
157 .setTableId((short) 123)
159 .setDurationNsec(11L)
160 .setByteCount(BigInteger.valueOf(12L))
161 .setPacketCount(BigInteger.valueOf(13L))
162 .setCookie(BigInteger.ZERO)
164 .setMatch(new MatchBuilder()
165 .setMatchEntry(Collections.<MatchEntry>emptyList())
169 .setFlags(new FlowModFlags(true, false, false, false, false))
170 .setInstruction(Collections.singletonList(new InstructionBuilder()
171 .setInstructionChoice(new ApplyActionsCaseBuilder()
172 .setApplyActions(new ApplyActionsBuilder()
173 .setAction(Collections.singletonList(new ActionBuilder()
174 .setActionChoice(new OutputActionCaseBuilder()
175 .setOutputAction(new OutputActionBuilder()
177 .setPort(new PortNumber(18L))
192 public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
193 GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
194 .setNode(createNodeRef("unitProt:123"));
196 rpcResult = buildFlowStatsReply();
198 final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
199 = flowStatisticsServiceDelegate.getAllFlowsStatisticsFromAllFlowTables(input.build());
201 Assert.assertTrue(resultFuture.isDone());
202 final RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput> rpcResultCompatible = resultFuture.get();
203 Assert.assertTrue(rpcResultCompatible.isSuccessful());
204 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
206 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
210 public void testGetFlowStatisticsFromFlowTable() throws Exception {
211 GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
212 .setNode(createNodeRef("unitProt:123"))
213 .setTableId((short) 1)
215 .setOutPort(BigInteger.ONE);
217 rpcResult = buildFlowStatsReply();
219 final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
220 = flowStatisticsServiceDelegate.getFlowStatisticsFromFlowTable(input.build());
222 Assert.assertTrue(resultFuture.isDone());
223 final RpcResult<GetFlowStatisticsFromFlowTableOutput> rpcResultCompatible = resultFuture.get();
224 Assert.assertTrue(rpcResultCompatible.isSuccessful());
225 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
227 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));