Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onosproject.incubator.protobuf.services.nb; |
| 18 | |
| 19 | import io.grpc.BindableService; |
| 20 | import io.grpc.ManagedChannel; |
| 21 | import io.grpc.inprocess.InProcessChannelBuilder; |
| 22 | import org.junit.AfterClass; |
| 23 | import org.junit.BeforeClass; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
| 27 | import org.onosproject.grpc.core.models.ApplicationIdProtoOuterClass; |
| 28 | import org.onosproject.grpc.nb.net.meter.MeterServiceGrpc; |
| 29 | import org.onosproject.grpc.nb.net.meter.MeterServiceNbProto; |
| 30 | import org.onosproject.grpc.net.meter.models.BandProtoOuterClass; |
| 31 | import org.onosproject.grpc.net.meter.models.MeterEnumsProto; |
| 32 | import org.onosproject.grpc.net.meter.models.MeterProtoOuterClass; |
| 33 | import org.onosproject.grpc.net.meter.models.MeterRequestProtoOuterClass; |
| 34 | import org.onosproject.net.DeviceId; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 35 | import org.onosproject.net.meter.Band; |
| 36 | import org.onosproject.net.meter.DefaultBand; |
| 37 | import org.onosproject.net.meter.Meter; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 38 | import org.onosproject.net.meter.MeterCellId; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 39 | import org.onosproject.net.meter.MeterId; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 40 | import org.onosproject.net.meter.MeterListener; |
| 41 | import org.onosproject.net.meter.MeterRequest; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 42 | import org.onosproject.net.meter.MeterService; |
| 43 | import org.onosproject.net.meter.MeterState; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 44 | |
| 45 | import java.io.IOException; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 46 | import java.util.ArrayList; |
| 47 | import java.util.Collection; |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 48 | import java.util.HashSet; |
| 49 | import java.util.List; |
| 50 | import java.util.Set; |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 51 | |
| 52 | import static org.junit.Assert.assertTrue; |
| 53 | |
| 54 | |
| 55 | public class GrpcNbMeterServiceTest { |
| 56 | |
| 57 | private static InProcessServer<BindableService> inProcessServer; |
| 58 | private static ManagedChannel channel; |
| 59 | private static MeterServiceGrpc.MeterServiceBlockingStub blockingStub; |
| 60 | |
| 61 | private static final MeterService MOCK_METER = new MockMeterService(); |
| 62 | |
| 63 | private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("d1"); |
| 64 | private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("d2"); |
| 65 | private static final DeviceId DEVICE_ID_3 = DeviceId.deviceId("d3"); |
| 66 | |
| 67 | private static final long METER_ID_1 = 1L; |
| 68 | private static final long METER_ID_2 = 2L; |
| 69 | private static final long METER_ID_3 = 3L; |
| 70 | private static final long METER_ID_4 = 4L; |
| 71 | |
| 72 | private static final Meter METER_1 = new MockMeter(DEVICE_ID_1, 1, METER_ID_1, 1); |
| 73 | private static final Meter METER_2 = new MockMeter(DEVICE_ID_2, 1, METER_ID_2, 2); |
| 74 | private static final Meter METER_3 = new MockMeter(DEVICE_ID_3, 1, METER_ID_3, 3); |
| 75 | private static final Meter METER_4 = new MockMeter(DEVICE_ID_3, 1, METER_ID_4, 4); |
| 76 | |
| 77 | private static Set<Meter> allMeters = new HashSet<>(); |
| 78 | |
| 79 | /** |
| 80 | * Create inProcessServer and bind grpcNbMeterService. |
| 81 | * |
| 82 | * @throws IllegalAccessException |
| 83 | * @throws IOException |
| 84 | * @throws InstantiationException |
| 85 | */ |
| 86 | @BeforeClass |
| 87 | public static void setup() throws IllegalAccessException, IOException, InstantiationException { |
| 88 | |
| 89 | GrpcNbMeterService grpcNbMeterService = new GrpcNbMeterService(); |
| 90 | grpcNbMeterService.meterService = MOCK_METER; |
| 91 | inProcessServer = new InProcessServer(GrpcNbMeterService.class); |
| 92 | inProcessServer.addServiceToBind(grpcNbMeterService); |
| 93 | |
| 94 | inProcessServer.start(); |
| 95 | channel = InProcessChannelBuilder.forName("test").directExecutor() |
| 96 | // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid |
| 97 | // needing certificates. |
| 98 | .usePlaintext(true).build(); |
| 99 | blockingStub = MeterServiceGrpc.newBlockingStub(channel); |
| 100 | |
| 101 | allMeters.add(METER_2); |
| 102 | allMeters.add(METER_3); |
| 103 | allMeters.add(METER_4); |
| 104 | } |
| 105 | |
| 106 | @AfterClass |
| 107 | public static void down() { |
| 108 | channel.shutdown(); |
| 109 | |
| 110 | inProcessServer.stop(); |
| 111 | } |
| 112 | |
| 113 | @Test |
| 114 | public void testSubmit() { |
| 115 | MeterServiceNbProto.submitRequest request = MeterServiceNbProto.submitRequest.newBuilder() |
| 116 | .setMeter(MeterRequestProtoOuterClass.MeterRequestProto.newBuilder() |
| 117 | .setDeviceId(DEVICE_ID_1.toString()) |
| 118 | .setApplicationId(ApplicationIdProtoOuterClass.ApplicationIdProto.newBuilder() |
| 119 | .setId(METER_1.appId().id()) |
| 120 | .build()) |
| 121 | .setUnit(MeterEnumsProto.MeterUnitProto.KB_PER_SEC) |
| 122 | .setIsBurst(false) |
| 123 | .addBands(BandProtoOuterClass.BandProto.getDefaultInstance()) |
| 124 | .setType(MeterEnumsProto.MeterRequestTypeProto.ADD) |
| 125 | .build()) |
| 126 | .build(); |
| 127 | MeterServiceNbProto.submitReply reply; |
| 128 | |
| 129 | int size = allMeters.size(); |
| 130 | reply = blockingStub.submit(request); |
| 131 | MeterProtoOuterClass.MeterProto meter = reply.getSubmitMeter(); |
| 132 | assertTrue(allMeters.size() == (size + 1) |
| 133 | && meter.getDeviceId().equals(METER_1.deviceId().toString()) |
| 134 | && meter.getApplicationId().getId() == METER_1.appId().id()); |
| 135 | |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void testWithdraw() { |
| 140 | MeterServiceNbProto.withdrawRequest request = MeterServiceNbProto.withdrawRequest.newBuilder() |
| 141 | .setMeter(MeterRequestProtoOuterClass.MeterRequestProto.newBuilder() |
| 142 | .setDeviceId(DEVICE_ID_2.toString()) |
| 143 | .setApplicationId(ApplicationIdProtoOuterClass.ApplicationIdProto.newBuilder() |
| 144 | .setId(1) |
| 145 | .build()) |
| 146 | .setUnit(MeterEnumsProto.MeterUnitProto.KB_PER_SEC) |
| 147 | .setIsBurst(false) |
| 148 | .addBands(BandProtoOuterClass.BandProto.getDefaultInstance()) |
| 149 | .setType(MeterEnumsProto.MeterRequestTypeProto.REMOVE) |
| 150 | .build()) |
| 151 | .setMeterId(METER_ID_2) |
| 152 | .build(); |
| 153 | MeterServiceNbProto.withdrawReply reply; |
| 154 | |
| 155 | int size = allMeters.size(); |
| 156 | reply = blockingStub.withdraw(request); |
| 157 | assertTrue(allMeters.size() == (size - 1)); |
| 158 | } |
| 159 | |
| 160 | @Test |
| 161 | public void testGetMeter() { |
| 162 | MeterServiceNbProto.getMeterRequest request = MeterServiceNbProto.getMeterRequest.newBuilder() |
| 163 | .setDeviceId(DEVICE_ID_3.toString()) |
| 164 | .setMeterId(METER_ID_3) |
| 165 | .build(); |
| 166 | MeterServiceNbProto.getMeterReply reply; |
| 167 | |
| 168 | reply = blockingStub.getMeter(request); |
| 169 | MeterProtoOuterClass.MeterProto meter = reply.getMeter(); |
| 170 | assertTrue(meter.getApplicationId().getId() == METER_3.appId().id() |
| 171 | && meter.getDeviceId().equals(DEVICE_ID_3.toString())); |
| 172 | } |
| 173 | |
| 174 | @Test |
| 175 | public void testGetAllMeters() { |
| 176 | MeterServiceNbProto.getAllMetersRequest request = MeterServiceNbProto.getAllMetersRequest.getDefaultInstance(); |
| 177 | MeterServiceNbProto.getAllMetersReply reply; |
| 178 | |
| 179 | reply = blockingStub.getAllMeters(request); |
| 180 | assertTrue(reply.getMetersCount() == allMeters.size()); |
| 181 | |
| 182 | } |
| 183 | |
| 184 | @Test |
| 185 | public void testGetMeters() { |
| 186 | MeterServiceNbProto.getMetersRequest request = MeterServiceNbProto.getMetersRequest.newBuilder() |
| 187 | .setDeviceId(DEVICE_ID_3.toString()) |
| 188 | .build(); |
| 189 | MeterServiceNbProto.getMetersReply reply; |
| 190 | |
| 191 | reply = blockingStub.getMeters(request); |
| 192 | assertTrue(reply.getMetersCount() == 2); |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * A mock class of meter. |
| 198 | */ |
| 199 | private static class MockMeter implements Meter { |
| 200 | |
| 201 | final DeviceId deviceId; |
| 202 | final ApplicationId appId; |
| 203 | final MeterId meterId; |
| 204 | final long baseValue; |
| 205 | final List<Band> bandList; |
| 206 | |
| 207 | public MockMeter(DeviceId deviceId, int appId, long meterId, int id) { |
| 208 | this.deviceId = deviceId; |
| 209 | this.appId = new DefaultApplicationId(appId, String.valueOf(appId)); |
| 210 | this.baseValue = id * 200L; |
| 211 | this.meterId = MeterId.meterId(meterId); |
| 212 | |
| 213 | Band band = DefaultBand.builder() |
| 214 | .ofType(Band.Type.REMARK) |
| 215 | .withRate(10) |
| 216 | .dropPrecedence((short) 20) |
| 217 | .burstSize(30).build(); |
| 218 | |
| 219 | this.bandList = new ArrayList<>(); |
| 220 | this.bandList.add(band); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public DeviceId deviceId() { |
| 225 | return this.deviceId; |
| 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public MeterId id() { |
| 230 | return this.meterId; |
| 231 | } |
| 232 | |
| 233 | @Override |
Frank Wang | d7e3b4b | 2017-09-24 13:37:54 +0900 | [diff] [blame] | 234 | public MeterCellId meterCellId() { |
| 235 | return this.id(); |
| 236 | } |
| 237 | |
| 238 | @Override |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 239 | public ApplicationId appId() { |
| 240 | return this.appId; |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public Unit unit() { |
| 245 | return Unit.KB_PER_SEC; |
| 246 | } |
| 247 | |
| 248 | @Override |
| 249 | public boolean isBurst() { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | @Override |
| 254 | public Collection<Band> bands() { |
| 255 | return this.bandList; |
| 256 | } |
| 257 | |
| 258 | @Override |
| 259 | public MeterState state() { |
| 260 | return MeterState.ADDED; |
| 261 | } |
| 262 | |
| 263 | @Override |
| 264 | public long life() { |
| 265 | return baseValue + 11; |
| 266 | } |
| 267 | |
| 268 | @Override |
| 269 | public long referenceCount() { |
| 270 | return baseValue + 22; |
| 271 | } |
| 272 | |
| 273 | @Override |
| 274 | public long packetsSeen() { |
| 275 | return baseValue + 33; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public long bytesSeen() { |
| 280 | return baseValue + 44; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * A mock class of MeterService. |
| 286 | */ |
| 287 | public static class MockMeterService implements MeterService { |
| 288 | @Override |
| 289 | public void addListener(MeterListener listener) { |
| 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void removeListener(MeterListener listener) { |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public Meter submit(MeterRequest meter) { |
| 298 | Meter m = new MockMeter(meter.deviceId(), meter.appId().id(), METER_ID_1, 1); |
| 299 | allMeters.add(m); |
| 300 | return m; |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void withdraw(MeterRequest meter, MeterId meterId) { |
| 305 | Meter toRemove = null; |
| 306 | for (Meter m: allMeters) { |
| 307 | if (meter.appId().id() == m.appId().id() && meter.deviceId().equals(m.deviceId()) |
| 308 | && m.id().equals(meterId)) { |
| 309 | toRemove = m; |
| 310 | break; |
| 311 | } |
| 312 | } |
| 313 | if (null != toRemove) { |
| 314 | allMeters.remove(toRemove); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public Meter getMeter(DeviceId deviceId, MeterId id) { |
| 320 | for (Meter m: allMeters) { |
| 321 | if (deviceId.equals(m.deviceId()) && m.id().equals(id)) { |
| 322 | return m; |
| 323 | } |
| 324 | } |
| 325 | return null; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public Collection<Meter> getAllMeters() { |
| 330 | return allMeters; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public Collection<Meter> getMeters(DeviceId deviceId) { |
| 335 | List<Meter> meters = new ArrayList<>(); |
| 336 | for (Meter m: allMeters) { |
| 337 | if (deviceId.equals(m.deviceId())) { |
| 338 | meters.add(m); |
| 339 | } |
| 340 | } |
| 341 | return meters; |
| 342 | } |
Pier Luigi | bdcd967 | 2017-10-13 13:54:48 +0200 | [diff] [blame] | 343 | |
| 344 | @Override |
| 345 | public MeterId allocateMeterId(DeviceId deviceId) { |
| 346 | return null; |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public void freeMeterId(DeviceId deviceId, MeterId meterId) { |
| 351 | |
| 352 | } |
Tian Jian | 5e4be91 | 2017-09-19 12:47:55 +0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | } |