Merge changes from topic 'ofj-models-to-ofp-models'
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / statistics / services / OpendaylightMeterStatisticsServiceImplTest.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;
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.After;
17 import org.junit.Assert;
18 import org.junit.Test;
19 import org.mockito.ArgumentCaptor;
20 import org.mockito.Captor;
21 import org.mockito.Matchers;
22 import org.mockito.Mockito;
23 import org.opendaylight.openflowplugin.api.OFConstants;
24 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManager;
25 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorManagerFactory;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsInputBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterConfigStatisticsOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetAllMeterStatisticsOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesInputBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterFeaturesOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.statistics.rev131111.GetMeterStatisticsOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandType;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterBandTypeBitmap;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterFlags;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessageBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.MeterBandDropCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.meter.band.header.meter.band.meter.band.drop._case.MeterBandDropBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterCaseBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterConfigCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyMeterFeaturesCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.MultipartReplyMeterBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.MeterStatsBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter._case.multipart.reply.meter.meter.stats.MeterBandStatsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.MultipartReplyMeterConfigBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.MeterConfigBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.config._case.multipart.reply.meter.config.meter.config.BandsBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.meter.features._case.MultipartReplyMeterFeaturesBuilder;
53 import org.opendaylight.yangtools.yang.binding.Notification;
54 import org.opendaylight.yangtools.yang.common.RpcResult;
55 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
56
57 /**
58  * Test for {@link OpendaylightMeterStatisticsServiceImpl}
59  */
60 public class OpendaylightMeterStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
61
62     private static final org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId METER_ID = new org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MeterId(123L);
63     @Captor
64     private ArgumentCaptor<MultipartRequestInput> requestInput;
65
66     private OpendaylightMeterStatisticsServiceImpl meterStatisticsService;
67
68     public void setUp() {
69         final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
70         meterStatisticsService = new OpendaylightMeterStatisticsServiceImpl(rqContextStack, deviceContext,
71                 new AtomicLong(), notificationPublishService, convertorManager);
72
73         Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
74                 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
75     }
76
77     @After
78     public void tearDown() throws Exception {
79         Mockito.verify(notificationPublishService).offerNotification(Matchers.<Notification>any());
80     }
81
82     @Test
83     public void testGetAllMeterConfigStatistics() throws Exception {
84         GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder()
85                 .setNode(createNodeRef("unitProt:123"));
86
87         rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(
88                 new MultipartReplyMessageBuilder()
89                         .setVersion(OFConstants.OFP_VERSION_1_3)
90                         .setMultipartReplyBody(new MultipartReplyMeterConfigCaseBuilder()
91                                 .setMultipartReplyMeterConfig(new MultipartReplyMeterConfigBuilder()
92                                         .setMeterConfig(Collections.singletonList(new MeterConfigBuilder()
93                                                 .setFlags(new MeterFlags(true, false, false, false))
94                                                 .setMeterId(METER_ID)
95                                                 .setBands(Collections.singletonList(new BandsBuilder()
96                                                         .setMeterBand(new MeterBandDropCaseBuilder()
97                                                                 .setMeterBandDrop(new MeterBandDropBuilder()
98                                                                         .setBurstSize(61L)
99                                                                         .setRate(62L)
100                                                                         .setType(MeterBandType.OFPMBTDROP)
101                                                                         .build())
102                                                                 .build())
103                                                         .build()))
104                                                 .build()))
105                                         .build())
106                                 .build())
107                         .build()
108         )).build();
109
110         final Future<RpcResult<GetAllMeterConfigStatisticsOutput>> resultFuture
111                 = meterStatisticsService.getAllMeterConfigStatistics(input.build());
112
113         Assert.assertTrue(resultFuture.isDone());
114         final RpcResult<GetAllMeterConfigStatisticsOutput> rpcResult = resultFuture.get();
115         Assert.assertTrue(rpcResult.isSuccessful());
116         Assert.assertEquals(MultipartType.OFPMPMETERCONFIG, requestInput.getValue().getType());
117     }
118
119     @Test
120     public void testGetAllMeterStatistics() throws Exception {
121         GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder()
122                 .setNode(createNodeRef("unitProt:123"));
123
124         rpcResult = buildMeterStatisticsReply();
125
126         final Future<RpcResult<GetAllMeterStatisticsOutput>> resultFuture
127                 = meterStatisticsService.getAllMeterStatistics(input.build());
128
129         Assert.assertTrue(resultFuture.isDone());
130         final RpcResult<GetAllMeterStatisticsOutput> rpcResult = resultFuture.get();
131         Assert.assertTrue(rpcResult.isSuccessful());
132         Assert.assertEquals(MultipartType.OFPMPMETER, requestInput.getValue().getType());
133     }
134
135     @Test
136     public void testGetMeterFeatures() throws Exception {
137         GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder()
138                 .setNode(createNodeRef("unitProt:123"));
139
140         rpcResult = RpcResultBuilder.<Object>success(Collections.singletonList(
141                 new MultipartReplyMessageBuilder()
142                         .setVersion(OFConstants.OFP_VERSION_1_3)
143                         .setMultipartReplyBody(new MultipartReplyMeterFeaturesCaseBuilder()
144                                 .setMultipartReplyMeterFeatures(new MultipartReplyMeterFeaturesBuilder()
145                                         .setBandTypes(new MeterBandTypeBitmap(true, false))
146                                         .setCapabilities(new MeterFlags(true, false, false, false))
147                                         .setMaxBands((short) 71)
148                                         .setMaxColor((short) 72)
149                                         .setMaxMeter(73L)
150                                         .build())
151                                 .build())
152                         .build()
153         )).build();
154
155         final Future<RpcResult<GetMeterFeaturesOutput>> resultFuture
156                 = meterStatisticsService.getMeterFeatures(input.build());
157
158         Assert.assertTrue(resultFuture.isDone());
159         final RpcResult<GetMeterFeaturesOutput> rpcResult = resultFuture.get();
160         Assert.assertTrue(rpcResult.isSuccessful());
161         Assert.assertEquals(MultipartType.OFPMPMETERFEATURES, requestInput.getValue().getType());
162     }
163
164     @Test
165     public void testGetMeterStatistics() throws Exception {
166         GetMeterStatisticsInputBuilder input = new GetMeterStatisticsInputBuilder()
167                 .setNode(createNodeRef("unitProt:123"))
168                 .setMeterId(new MeterId(21L));
169
170         rpcResult = buildMeterStatisticsReply();
171
172         final Future<RpcResult<GetMeterStatisticsOutput>> resultFuture
173                 = meterStatisticsService.getMeterStatistics(input.build());
174
175         Assert.assertTrue(resultFuture.isDone());
176         final RpcResult<GetMeterStatisticsOutput> rpcResult = resultFuture.get();
177         Assert.assertTrue(rpcResult.isSuccessful());
178         Assert.assertEquals(MultipartType.OFPMPMETER, requestInput.getValue().getType());
179     }
180
181     protected RpcResult<Object> buildMeterStatisticsReply() {
182         return RpcResultBuilder.<Object>success(Collections.singletonList(
183                 new MultipartReplyMessageBuilder()
184                         .setVersion(OFConstants.OFP_VERSION_1_3)
185                         .setMultipartReplyBody(new MultipartReplyMeterCaseBuilder()
186                                 .setMultipartReplyMeter(new MultipartReplyMeterBuilder()
187                                         .setMeterStats(Collections.singletonList(new MeterStatsBuilder()
188                                                 .setMeterId(METER_ID)
189                                                 .setByteInCount(BigInteger.valueOf(81L))
190                                                 .setDurationSec(82L)
191                                                 .setDurationNsec(83L)
192                                                 .setFlowCount(84L)
193                                                 .setPacketInCount(BigInteger.valueOf(85L))
194                                                 .setMeterBandStats(Collections.singletonList(new MeterBandStatsBuilder()
195                                                         .setByteBandCount(BigInteger.valueOf(86L))
196                                                         .setPacketBandCount(BigInteger.valueOf(87L))
197                                                         .build()))
198                                                 .build()))
199                                         .build())
200                                 .build())
201                         .build()
202         )).build();
203     }
204 }