251eb10fb1b25f2790f32c020a8312ac6f3c4dbc
[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 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.rev130715.Counter32;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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;
62
63 /**
64  * Test for {@link OpendaylightFlowStatisticsServiceDelegateImpl}.
65  * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
66  */
67 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
68
69     public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
70     @Captor
71     private ArgumentCaptor<MultipartRequestInput> requestInput;
72
73     private OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsServiceDelegate;
74     @Mock
75     private MessageTranslator<Object, Object> translator;
76
77     @Override
78     public void setUp() {
79         flowStatisticsServiceDelegate = new OpendaylightFlowStatisticsServiceDelegateImpl(
80                 rqContextStack, deviceContext, notificationPublishService, new AtomicLong(21));
81
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);
85     }
86
87     @Test(expected = IllegalAccessError.class)
88     public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
89         flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForGivenMatch(null);
90     }
91
92     @Test
93     public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
94         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input = new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
95                 .setNode(createNodeRef("unitProt:123"))
96                 .setTableId(new TableId((short) 1));
97
98         Mockito.when(translator.translate(Matchers.any(MultipartReply.class), Matchers.eq(deviceInfo), 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))
103                         .build());
104
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))
113                                 .setFlowCount(52L)
114                                 .build())
115                         .build())
116                 .build()))
117                 .build();
118
119         final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
120                 = flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
121
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());
126
127         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
128     }
129
130     @Test
131     public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
132         GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
133                 .setNode(createNodeRef("unitProt:123"))
134                 .setTableId(new TableId((short) 1));
135
136         rpcResult = buildFlowStatsReply();
137
138         final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
139                 = flowStatisticsServiceDelegate.getAllFlowStatisticsFromFlowTable(input.build());
140
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());
145
146         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
147     }
148
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)
158                                         .setDurationSec(10L)
159                                         .setDurationNsec(11L)
160                                         .setByteCount(BigInteger.valueOf(12L))
161                                         .setPacketCount(BigInteger.valueOf(13L))
162                                         .setCookie(BigInteger.ZERO)
163                                         .setPriority(14)
164                                         .setMatch(new MatchBuilder()
165                                                 .setMatchEntry(Collections.<MatchEntry>emptyList())
166                                                 .build())
167                                         .setHardTimeout(15)
168                                         .setIdleTimeout(16)
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()
176                                                                                         .setMaxLength(17)
177                                                                                         .setPort(new PortNumber(18L))
178                                                                                         .build())
179                                                                                 .build())
180                                                                         .build()))
181                                                                 .build())
182                                                         .build())
183                                                 .build()))
184                                         .build()))
185                                 .build())
186                         .build())
187                 .build()))
188                 .build();
189     }
190
191     @Test
192     public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
193         GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input = new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
194                 .setNode(createNodeRef("unitProt:123"));
195
196         rpcResult = buildFlowStatsReply();
197
198         final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
199                 = flowStatisticsServiceDelegate.getAllFlowsStatisticsFromAllFlowTables(input.build());
200
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());
205
206         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
207     }
208
209     @Test
210     public void testGetFlowStatisticsFromFlowTable() throws Exception {
211         GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
212                 .setNode(createNodeRef("unitProt:123"))
213                 .setTableId((short) 1)
214                 .setPriority(123)
215                 .setOutPort(BigInteger.ONE);
216
217         rpcResult = buildFlowStatsReply();
218
219         final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
220                 = flowStatisticsServiceDelegate.getFlowStatisticsFromFlowTable(input.build());
221
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());
226
227         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS)).offerNotification(Matchers.any(Notification.class));
228     }
229 }