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;
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;
58 * Test for {@link OpendaylightMeterStatisticsServiceImpl}
60 public class OpendaylightMeterStatisticsServiceImplTest extends AbstractSingleStatsServiceTest {
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);
64 private ArgumentCaptor<MultipartRequestInput> requestInput;
66 private OpendaylightMeterStatisticsServiceImpl meterStatisticsService;
69 final ConvertorManager convertorManager = ConvertorManagerFactory.createDefaultManager();
70 meterStatisticsService = new OpendaylightMeterStatisticsServiceImpl(rqContextStack, deviceContext,
71 new AtomicLong(), notificationPublishService, convertorManager);
73 Mockito.doAnswer(answerVoidToCallback).when(outboundQueueProvider)
74 .commitEntry(Matchers.eq(42L), requestInput.capture(), Matchers.any(FutureCallback.class));
78 public void tearDown() throws Exception {
79 Mockito.verify(notificationPublishService).offerNotification(Matchers.<Notification>any());
83 public void testGetAllMeterConfigStatistics() throws Exception {
84 GetAllMeterConfigStatisticsInputBuilder input = new GetAllMeterConfigStatisticsInputBuilder()
85 .setNode(createNodeRef("unitProt:123"));
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))
95 .setBands(Collections.singletonList(new BandsBuilder()
96 .setMeterBand(new MeterBandDropCaseBuilder()
97 .setMeterBandDrop(new MeterBandDropBuilder()
100 .setType(MeterBandType.OFPMBTDROP)
110 final Future<RpcResult<GetAllMeterConfigStatisticsOutput>> resultFuture
111 = meterStatisticsService.getAllMeterConfigStatistics(input.build());
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());
120 public void testGetAllMeterStatistics() throws Exception {
121 GetAllMeterStatisticsInputBuilder input = new GetAllMeterStatisticsInputBuilder()
122 .setNode(createNodeRef("unitProt:123"));
124 rpcResult = buildMeterStatisticsReply();
126 final Future<RpcResult<GetAllMeterStatisticsOutput>> resultFuture
127 = meterStatisticsService.getAllMeterStatistics(input.build());
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());
136 public void testGetMeterFeatures() throws Exception {
137 GetMeterFeaturesInputBuilder input = new GetMeterFeaturesInputBuilder()
138 .setNode(createNodeRef("unitProt:123"));
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)
155 final Future<RpcResult<GetMeterFeaturesOutput>> resultFuture
156 = meterStatisticsService.getMeterFeatures(input.build());
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());
165 public void testGetMeterStatistics() throws Exception {
166 GetMeterStatisticsInputBuilder input = new GetMeterStatisticsInputBuilder()
167 .setNode(createNodeRef("unitProt:123"))
168 .setMeterId(new MeterId(21L));
170 rpcResult = buildMeterStatisticsReply();
172 final Future<RpcResult<GetMeterStatisticsOutput>> resultFuture
173 = meterStatisticsService.getMeterStatistics(input.build());
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());
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))
191 .setDurationNsec(83L)
193 .setPacketInCount(BigInteger.valueOf(85L))
194 .setMeterBandStats(Collections.singletonList(new MeterBandStatsBuilder()
195 .setByteBandCount(BigInteger.valueOf(86L))
196 .setPacketBandCount(BigInteger.valueOf(87L))