Switch to MD-SAL APIs
[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 package org.opendaylight.openflowplugin.impl.statistics.services.compatibility;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12
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;
66
67 /**
68  * Test for {@link OpendaylightFlowStatisticsServiceDelegateImpl}.
69  * Skipping notification verification. This will be tested in tests of underlying single task oriented services.
70  */
71 public class OpendaylightFlowStatisticsServiceDelegateImplTest extends AbstractSingleStatsServiceTest {
72
73     public static final int NOTIFICATION_WAIT_TIMEOUT_MS = 500;
74     @Captor
75     private ArgumentCaptor<MultipartRequestInput> requestInput;
76
77     private OpendaylightFlowStatisticsServiceDelegateImpl flowStatisticsServiceDelegate;
78     @Mock
79     private MessageTranslator<Object, Object> translator;
80
81     @Override
82     public void setUp() {
83         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
84         flowStatisticsServiceDelegate = new OpendaylightFlowStatisticsServiceDelegateImpl(
85                 rqContextStack, deviceContext, notificationPublishService, new AtomicLong(21), convertorManager);
86
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);
90     }
91
92     @Test(expected = IllegalAccessError.class)
93     public void testGetAggregateFlowStatisticsFromFlowTableForGivenMatch() throws Exception {
94         flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForGivenMatch(null);
95     }
96
97     @Test
98     public void testGetAggregateFlowStatisticsFromFlowTableForAllFlows() throws Exception {
99         GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder input =
100                 new GetAggregateFlowStatisticsFromFlowTableForAllFlowsInputBuilder()
101                 .setNode(createNodeRef("unitProt:123"))
102                 .setTableId(new TableId((short) 1));
103
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))
109                         .build());
110
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))
119                                 .setFlowCount(52L)
120                                 .build())
121                         .build())
122                 .build()))
123                 .build();
124
125         final Future<RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput>> resultFuture
126                 = flowStatisticsServiceDelegate.getAggregateFlowStatisticsFromFlowTableForAllFlows(input.build());
127
128         Assert.assertTrue(resultFuture.isDone());
129         final RpcResult<GetAggregateFlowStatisticsFromFlowTableForAllFlowsOutput> rpcResultCompatible =
130                 resultFuture.get();
131         Assert.assertTrue(rpcResultCompatible.isSuccessful());
132         Assert.assertEquals(MultipartType.OFPMPAGGREGATE, requestInput.getValue().getType());
133
134         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
135                 .offerNotification(any(Notification.class));
136     }
137
138     @Test
139     public void testGetAllFlowStatisticsFromFlowTable() throws Exception {
140         GetAllFlowStatisticsFromFlowTableInputBuilder input = new GetAllFlowStatisticsFromFlowTableInputBuilder()
141                 .setNode(createNodeRef("unitProt:123"))
142                 .setTableId(new TableId((short) 1));
143
144         rpcResult = buildFlowStatsReply();
145
146         final Future<RpcResult<GetAllFlowStatisticsFromFlowTableOutput>> resultFuture
147                 = flowStatisticsServiceDelegate.getAllFlowStatisticsFromFlowTable(input.build());
148
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());
153
154         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
155                 .offerNotification(any(Notification.class));
156     }
157
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)
167                                         .setDurationSec(10L)
168                                         .setDurationNsec(11L)
169                                         .setByteCount(BigInteger.valueOf(12L))
170                                         .setPacketCount(BigInteger.valueOf(13L))
171                                         .setCookie(BigInteger.ZERO)
172                                         .setPriority(14)
173                                         .setMatch(new MatchBuilder()
174                                                 .setMatchEntry(Collections.<MatchEntry>emptyList())
175                                                 .build())
176                                         .setHardTimeout(15)
177                                         .setIdleTimeout(16)
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()
184                                                                                 .setOutputAction(
185                                                                                         new OutputActionBuilder()
186                                                                                         .setMaxLength(17)
187                                                                                         .setPort(new PortNumber(18L))
188                                                                                         .build())
189                                                                                 .build())
190                                                                         .build()))
191                                                                 .build())
192                                                         .build())
193                                                 .build()))
194                                         .build()))
195                                 .build())
196                         .build())
197                 .build()))
198                 .build();
199     }
200
201     @Test
202     public void testGetAllFlowsStatisticsFromAllFlowTables() throws Exception {
203         GetAllFlowsStatisticsFromAllFlowTablesInputBuilder input =
204                 new GetAllFlowsStatisticsFromAllFlowTablesInputBuilder()
205                 .setNode(createNodeRef("unitProt:123"));
206
207         rpcResult = buildFlowStatsReply();
208
209         final Future<RpcResult<GetAllFlowsStatisticsFromAllFlowTablesOutput>> resultFuture
210                 = flowStatisticsServiceDelegate.getAllFlowsStatisticsFromAllFlowTables(input.build());
211
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());
216
217         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
218                 .offerNotification(any(Notification.class));
219     }
220
221     @Test
222     public void testGetFlowStatisticsFromFlowTable() throws Exception {
223         GetFlowStatisticsFromFlowTableInputBuilder input = new GetFlowStatisticsFromFlowTableInputBuilder()
224                 .setNode(createNodeRef("unitProt:123"))
225                 .setTableId((short) 1)
226                 .setPriority(123)
227                 .setOutPort(BigInteger.ONE);
228
229         rpcResult = buildFlowStatsReply();
230
231         final Future<RpcResult<GetFlowStatisticsFromFlowTableOutput>> resultFuture
232                 = flowStatisticsServiceDelegate.getFlowStatisticsFromFlowTable(input.build());
233
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());
238
239         Mockito.verify(notificationPublishService, Mockito.timeout(NOTIFICATION_WAIT_TIMEOUT_MS))
240                 .offerNotification(any(Notification.class));
241     }
242 }