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