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