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.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter32;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Counter64;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput;
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.GetAllFlowStatisticsFromFlowTableOutput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesInputBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetAllFlowsStatisticsFromAllFlowTablesOutput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.GetFlowStatisticsFromFlowTableOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.get.aggregate.flow.statistics.from.flow.table._for.given.match.output.AggregatedFlowStatisticsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.output.action._case.OutputActionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ApplyActionsCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.apply.actions._case.ApplyActionsBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartRequestFlags;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyAggregateCaseBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyFlowCaseBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.aggregate._case.MultipartReplyAggregateBuilder;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.MultipartReplyFlowBuilder;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.flow._case.multipart.reply.flow.FlowStatsBuilder;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.TableId;
61 import org.opendaylight.yangtools.yang.binding.Notification;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
66 * Test for {@link OpendaylightFlowStatisticsServiceDelegateImpl}.
67 * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
69 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
71 public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
73 private ArgumentCaptor<MultipartRequestInput> requestInput;
75 private OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsServiceDelegate;
77 private MessageTranslator<Object, Object> translator;
81 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
82 flowStatisticsServiceDelegate = new OpendaylightFlowStatisticsServiceDelegateImpl(
83 rqContextStack, deviceContext, notificationPublishService, new AtomicLong(21), convertorManager);
85 Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
86 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
87 Mockito.when(translatorLibrary.lookupTranslator(Matchers.<TranslatorKey>any())).thenReturn(translator);
90 @Test(expected = IllegalAccessError.class)
91 public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
92 flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForGivenMatch(null);
96 public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
97 GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
98 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
99 .setNode(createNodeRef("unitProt:123"))
100 .setTableId(new TableId((short) 1));
102 Mockito.when(translator.translate(Matchers.any(MultipartReply.class), Matchers.eq(deviceInfo), Matchers.any()))
103 .thenReturn(new AggregatedFlowStatisticsBuilder()
104 .setByteCount(new Counter64(BigInteger.valueOf(50L)))
105 .setPacketCount(new Counter64(BigInteger.valueOf(51L)))
106 .setFlowCount(new Counter32(52L))
109 rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(new MultipartReplyMessageBuilder()
110 .setType(MultipartType.OFPMPAGGREGATE)
111 .setVersion(OFConstants.OFP_VERSION_1_3)
112 .setFlags(new MultipartRequestFlags(false))
113 .setMultipartReplyBody(new MultipartReplyAggregateCaseBuilder()
114 .setMultipartReplyAggregate(new MultipartReplyAggregateBuilder()
115 .setByteCount(BigInteger.valueOf(50L))
116 .setPacketCount(BigInteger.valueOf(51L))
123 final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
124 = flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
126 Assert.assertTrue(resultFuture.isDone());
127 final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResultCompatible =
129 Assert.assertTrue(rpcResultCompatible.isSuccessful());
130 Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
132 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
133 .offerNotification(Matchers.any(Notification.class));
137 public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
138 GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
139 .setNode(createNodeRef("unitProt:123"))
140 .setTableId(new TableId((short) 1));
142 rpcResult = buildFlowStatsReply();
144 final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
145 = flowStatisticsServiceDelegate.getAllFlowStatisticsFromFlowTable(input.build());
147 Assert.assertTrue(resultFuture.isDone());
148 final RpcResult<GetAllFlowStatisticsFromFlowTableOutput> rpcResultCompatible = resultFuture.get();
149 Assert.assertTrue(rpcResultCompatible.isSuccessful());
150 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
152 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
153 .offerNotification(Matchers.any(Notification.class));
156 private static RpcResult<Object> buildFlowStatsReply() {
157 return RpcResultBuilder.<Object>success(Collections.singletonList(new MultipartReplyMessageBuilder()
158 .setType(MultipartType.OFPMPFLOW)
159 .setVersion(OFConstants.OFP_VERSION_1_3)
160 .setFlags(new MultipartRequestFlags(false))
161 .setMultipartReplyBody(new MultipartReplyFlowCaseBuilder()
162 .setMultipartReplyFlow(new MultipartReplyFlowBuilder()
163 .setFlowStats(Collections.singletonList(new FlowStatsBuilder()
164 .setTableId((short) 123)
166 .setDurationNsec(11L)
167 .setByteCount(BigInteger.valueOf(12L))
168 .setPacketCount(BigInteger.valueOf(13L))
169 .setCookie(BigInteger.ZERO)
171 .setMatch(new MatchBuilder()
172 .setMatchEntry(Collections.<MatchEntry>emptyList())
176 .setFlags(new FlowModFlags(true, false, false, false, false))
177 .setInstruction(Collections.singletonList(new InstructionBuilder()
178 .setInstructionChoice(new ApplyActionsCaseBuilder()
179 .setApplyActions(new ApplyActionsBuilder()
180 .setAction(Collections.singletonList(new ActionBuilder()
181 .setActionChoice(new OutputActionCaseBuilder()
183 new OutputActionBuilder()
185 .setPort(new PortNumber(18L))
200 public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
201 GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
202 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
203 .setNode(createNodeRef("unitProt:123"));
205 rpcResult = buildFlowStatsReply();
207 final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
208 = flowStatisticsServiceDelegate.getAllFlowsStatisticsFromAllFlowTables(input.build());
210 Assert.assertTrue(resultFuture.isDone());
211 final RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput> rpcResultCompatible = resultFuture.get();
212 Assert.assertTrue(rpcResultCompatible.isSuccessful());
213 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
215 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
216 .offerNotification(Matchers.any(Notification.class));
220 public void testGetFlowStatisticsFromFlowTable() throws Exception {
221 GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
222 .setNode(createNodeRef("unitProt:123"))
223 .setTableId((short) 1)
225 .setOutPort(BigInteger.ONE);
227 rpcResult = buildFlowStatsReply();
229 final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
230 = flowStatisticsServiceDelegate.getFlowStatisticsFromFlowTable(input.build());
232 Assert.assertTrue(resultFuture.isDone());
233 final RpcResult<GetFlowStatisticsFromFlowTableOutput> rpcResultCompatible = resultFuture.get();
234 Assert.assertTrue(rpcResultCompatible.isSuccessful());
235 Assert.assertEquals(MultipartType.OFPMPFLOW, requestInput.getValue().getType());
237 Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
238 .offerNotification(Matchers.any(Notification.class));