blob: 37ff7b4702c8f13c7abbeb2f24e591140d621eb7 [file] [log] [blame]
Pier Luigif094c612017-10-14 12:15:02 +02001/*
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
Thomas Vachuska52f2cd12018-11-08 21:20:04 -080017package org.onosproject.store.meter.impl;
Pier Luigif094c612017-10-14 12:15:02 +020018
Charles Chan593acf92017-11-22 13:55:41 -080019import com.google.common.collect.Lists;
Pier Luigif094c612017-10-14 12:15:02 +020020import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
23import org.onlab.junit.TestUtils;
Charles Chan593acf92017-11-22 13:55:41 -080024import org.onlab.util.KryoNamespace;
25import org.onosproject.TestApplicationId;
Pier Luigif094c612017-10-14 12:15:02 +020026import org.onosproject.net.DeviceId;
27import org.onosproject.net.behaviour.MeterQuery;
28import org.onosproject.net.driver.Behaviour;
29import org.onosproject.net.driver.Driver;
30import org.onosproject.net.driver.DriverData;
31import org.onosproject.net.driver.DriverHandler;
32import org.onosproject.net.driver.DriverServiceAdapter;
33import org.onosproject.net.meter.Band;
34import org.onosproject.net.meter.DefaultBand;
35import org.onosproject.net.meter.DefaultMeter;
36import org.onosproject.net.meter.DefaultMeterFeatures;
37import org.onosproject.net.meter.Meter;
pierventre3b39bd82021-08-18 09:40:14 +020038import org.onosproject.net.meter.MeterCellId;
Pier Luigif094c612017-10-14 12:15:02 +020039import org.onosproject.net.meter.MeterFeatures;
40import org.onosproject.net.meter.MeterFeaturesKey;
41import org.onosproject.net.meter.MeterId;
42import org.onosproject.net.meter.MeterKey;
pierventre3b39bd82021-08-18 09:40:14 +020043import org.onosproject.net.meter.MeterScope;
Pier Luigif094c612017-10-14 12:15:02 +020044import org.onosproject.net.meter.MeterState;
pierventre3b39bd82021-08-18 09:40:14 +020045import org.onosproject.net.meter.MeterTableKey;
46import org.onosproject.net.pi.model.PiMeterId;
47import org.onosproject.net.pi.runtime.PiMeterCellId;
Charles Chan593acf92017-11-22 13:55:41 -080048import org.onosproject.store.service.Serializer;
Pier Luigif094c612017-10-14 12:15:02 +020049import org.onosproject.store.service.TestStorageService;
50
pierventre44220052020-09-22 12:51:06 +020051import java.util.Collection;
Pier Luigif094c612017-10-14 12:15:02 +020052import java.util.Collections;
53import java.util.HashSet;
Pier Luigif094c612017-10-14 12:15:02 +020054import java.util.concurrent.CompletableFuture;
55import java.util.concurrent.ExecutionException;
56
57import static org.hamcrest.Matchers.is;
58import static org.junit.Assert.*;
59import static org.onosproject.net.NetTestTools.APP_ID;
Daniele Moro43ac2892021-07-15 17:02:59 +020060import static org.onosproject.net.NetTestTools.APP_ID_2;
Pier Luigif094c612017-10-14 12:15:02 +020061import static org.onosproject.net.NetTestTools.did;
62
63/**
64 * Meter store tests.
65 */
66public class DistributedMeterStoreTest {
Pier Luigif094c612017-10-14 12:15:02 +020067 // Store under testing
68 private DistributedMeterStore meterStore;
69
70 // Device ids used during the tests
71 private DeviceId did1 = did("1");
72 private DeviceId did2 = did("2");
73 private DeviceId did3 = did("3");
74 private DeviceId did4 = did("4");
75
76 // Meter ids used during the tests
77 private MeterId mid1 = MeterId.meterId(1);
78 private MeterId mid2 = MeterId.meterId(2);
Daniele Moro43ac2892021-07-15 17:02:59 +020079 private MeterId mid3 = MeterId.meterId(3);
Pier Luigibdcd9672017-10-13 13:54:48 +020080 private MeterId mid10 = MeterId.meterId(10);
pierventre3b39bd82021-08-18 09:40:14 +020081 private MeterCellId cid4 = PiMeterCellId.ofIndirect(
82 PiMeterId.of("foo"), 4);
83 private MeterCellId invalidCid = PiMeterCellId.ofIndirect(
84 PiMeterId.of("foo"), 11);
Pier Luigif094c612017-10-14 12:15:02 +020085
86 // Bands used during the tests
87 private Band b1 = DefaultBand.builder()
88 .ofType(Band.Type.DROP)
89 .withRate(500)
90 .build();
91
92 // Meters used during the tests
93 private Meter m1 = DefaultMeter.builder()
94 .forDevice(did1)
95 .fromApp(APP_ID)
96 .withId(mid1)
97 .withUnit(Meter.Unit.KB_PER_SEC)
98 .withBands(Collections.singletonList(b1))
99 .build();
100
Daniele Moro43ac2892021-07-15 17:02:59 +0200101 private Meter m2 = DefaultMeter.builder()
102 .forDevice(did1)
103 .fromApp(APP_ID_2)
104 .withCellId(mid2)
105 .withUnit(Meter.Unit.KB_PER_SEC)
106 .withBands(Collections.singletonList(b1))
107 .build();
108
109 private Meter m3 = DefaultMeter.builder()
110 .forDevice(did2)
111 .fromApp(APP_ID_2)
112 .withCellId(mid3)
113 .withUnit(Meter.Unit.KB_PER_SEC)
114 .withBands(Collections.singletonList(b1))
115 .build();
116
pierventre3b39bd82021-08-18 09:40:14 +0200117 private Meter m4 = DefaultMeter.builder()
118 .forDevice(did3)
119 .fromApp(APP_ID)
120 .withCellId(cid4)
121 .withUnit(Meter.Unit.KB_PER_SEC)
122 .withBands(Collections.singletonList(b1))
123 .build();
124
Pier Luigif094c612017-10-14 12:15:02 +0200125 // Meter features used during the tests
126 private MeterFeatures mef1 = DefaultMeterFeatures.builder().forDevice(did1)
127 .withMaxMeters(3L)
128 .withBandTypes(new HashSet<>())
129 .withUnits(new HashSet<>())
130 .hasStats(false)
131 .hasBurst(false)
132 .withMaxBands((byte) 0)
133 .withMaxColors((byte) 0)
134 .build();
135 private MeterFeatures mef2 = DefaultMeterFeatures.builder().forDevice(did2)
136 .withMaxMeters(10L)
137 .withBandTypes(new HashSet<>())
138 .withUnits(new HashSet<>())
139 .hasStats(false)
140 .hasBurst(false)
141 .withMaxBands((byte) 0)
142 .withMaxColors((byte) 0)
143 .build();
pierventre3b39bd82021-08-18 09:40:14 +0200144 private MeterFeatures mef3 = DefaultMeterFeatures.builder().forDevice(did3)
145 .withStartIndex(0L)
146 .withEndIndex(10L)
147 .withScope(MeterScope.of("foo"))
148 .withBandTypes(new HashSet<>())
149 .withUnits(new HashSet<>())
150 .hasStats(false)
151 .hasBurst(false)
152 .withMaxBands((byte) 0)
153 .withMaxColors((byte) 0)
154 .build();
Pier Luigif094c612017-10-14 12:15:02 +0200155
156 @Before
157 public void setup() {
158 // Init step
159 meterStore = new DistributedMeterStore();
160 // Let's initialize some internal services
161 TestUtils.setField(meterStore, "storageService", new TestStorageService());
Pier Luigif094c612017-10-14 12:15:02 +0200162 TestUtils.setField(meterStore, "driverService", new TestDriverService());
Charles Chan593acf92017-11-22 13:55:41 -0800163
164 // Inject TestApplicationId into the DistributedMeterStore serializer
165 KryoNamespace.Builder testKryoBuilder = TestUtils.getField(meterStore, "APP_KRYO_BUILDER");
166 testKryoBuilder.register(TestApplicationId.class);
167 Serializer testSerializer = Serializer.using(Lists.newArrayList(testKryoBuilder.build()));
168 TestUtils.setField(meterStore, "serializer", testSerializer);
169
Pier Luigif094c612017-10-14 12:15:02 +0200170 // Activate the store
171 meterStore.activate();
172 }
173
174 @After
175 public void tearDown() {
176 // Deactivate the store
177 meterStore.deactivate();
178 }
179
pierventre3b39bd82021-08-18 09:40:14 +0200180 private void initMeterStore(boolean enableUserDefinedIndex) {
181 meterStore.userDefinedIndexMode(enableUserDefinedIndex);
Pier Luigif094c612017-10-14 12:15:02 +0200182 // Let's store feature for device 1
183 meterStore.storeMeterFeatures(mef1);
184 // Let's store feature for device 2
185 meterStore.storeMeterFeatures(mef2);
pierventre3b39bd82021-08-18 09:40:14 +0200186 meterStore.storeMeterFeatures(mef3);
Pier Luigif094c612017-10-14 12:15:02 +0200187 }
188
189 /**
190 * Test proper store of meter features.
191 */
192 @Test
193 public void testStoreMeterFeatures() {
194 // Let's store feature for device 1
195 meterStore.storeMeterFeatures(mef1);
196 // Verify store meter features
197 assertThat(meterStore.getMaxMeters(MeterFeaturesKey.key(did1)), is(3L));
198 // Let's store feature for device 1
199 meterStore.storeMeterFeatures(mef2);
200 // Verify store meter features
201 assertThat(meterStore.getMaxMeters(MeterFeaturesKey.key(did2)), is(10L));
202 }
203
204 /**
205 * Test proper delete of meter features.
206 */
207 @Test
208 public void testDeleteMeterFeatures() {
209 // Let's store feature for device 1
210 meterStore.storeMeterFeatures(mef1);
211 // Verify store meter features
212 assertThat(meterStore.getMaxMeters(MeterFeaturesKey.key(did1)), is(3L));
213 // Let's delete the features
214 meterStore.deleteMeterFeatures(did1);
215 // Verify delete meter features
216 assertThat(meterStore.getMaxMeters(MeterFeaturesKey.key(did1)), is(0L));
217 }
218
219 /**
220 * Test proper allocation of meter ids.
221 */
222 @Test
223 public void testAllocateId() {
224 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200225 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200226 // Allocate a meter id and verify is equal to mid1
227 assertThat(mid1, is(meterStore.allocateMeterId(did1)));
228 // Allocate a meter id and verify is equal to mid2
229 assertThat(mid2, is(meterStore.allocateMeterId(did1)));
230 }
231
232 /**
233 * Test proper free of meter ids.
234 */
235 @Test
236 public void testFreeId() {
237 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200238 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200239 // Allocate a meter id and verify is equal to mid1
240 assertThat(mid1, is(meterStore.allocateMeterId(did1)));
241 // Free the above id
242 meterStore.freeMeterId(did1, mid1);
243 // Allocate a meter id and verify is equal to mid1
244 assertThat(mid1, is(meterStore.allocateMeterId(did1)));
Pier Luigibdcd9672017-10-13 13:54:48 +0200245 // Free an id not allocated
246 meterStore.freeMeterId(did1, mid10);
247 // Allocate a meter id and verify is equal to mid2
248 assertThat(mid2, is(meterStore.allocateMeterId(did1)));
Pier Luigif094c612017-10-14 12:15:02 +0200249 }
250
251 /**
252 * Test proper reuse of meter ids.
253 */
254 @Test
255 public void testReuseId() {
256 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200257 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200258 // Reserve id 1
259 MeterId meterIdOne = meterStore.allocateMeterId(did2);
260 // Free the above id
261 meterStore.freeMeterId(did2, meterIdOne);
262 // Start an async reservation
263 CompletableFuture<MeterId> future = CompletableFuture.supplyAsync(
264 () -> meterStore.allocateMeterId(did2)
265 );
266 // Start another reservation
267 MeterId meterIdTwo = meterStore.allocateMeterId(did2);
268 try {
269 meterIdOne = future.get();
270 } catch (InterruptedException | ExecutionException e) {
271 e.printStackTrace();
272 }
273 // Ids should be different, otherwise we had clash in the store
274 assertNotEquals("Ids should be different", meterIdOne, meterIdTwo);
275
276 // Free the above id
277 meterStore.freeMeterId(did1, meterIdOne);
278 // Free the above id
279 meterStore.freeMeterId(did1, meterIdTwo);
280 // Reserve id 1
281 meterIdOne = meterStore.allocateMeterId(did2);
282 // Reserve id 2
283 meterStore.allocateMeterId(did2);
284 // Reserve id 3
285 MeterId meterIdThree = meterStore.allocateMeterId(did2);
286 // Reserve id 4
287 MeterId meterIdFour = meterStore.allocateMeterId(did2);
288 // Free the above id
289 meterStore.freeMeterId(did1, meterIdOne);
290 // Free the above id
291 meterStore.freeMeterId(did1, meterIdThree);
292 // Free the above id
293 meterStore.freeMeterId(did1, meterIdFour);
294 // Start an async reservation
295 future = CompletableFuture.supplyAsync(
296 () -> meterStore.allocateMeterId(did2)
297 );
298 // Start another reservation
299 MeterId meterAnotherId = meterStore.allocateMeterId(did2);
300 try {
301 meterAnotherId = future.get();
302 } catch (InterruptedException | ExecutionException e) {
303 e.printStackTrace();
304 }
305 // Ids should be different, otherwise we had clash in the store
306 assertNotEquals("Ids should be different", meterAnotherId, meterIdOne);
307 }
308
309 /**
310 * Test query meters mechanism.
311 */
312 @Test
313 public void testQueryMeters() {
314 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200315 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200316 // Let's test queryMeters
317 assertThat(mid1, is(meterStore.allocateMeterId(did3)));
318 // Let's test queryMeters error
319 assertNull(meterStore.allocateMeterId(did4));
320 }
321
322 /**
323 * Test max meter error.
324 */
325 @Test
326 public void testMaxMeterError() {
327 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200328 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200329 // Reserve id 1
330 assertThat(mid1, is(meterStore.allocateMeterId(did1)));
331 // Reserve id 2
332 assertThat(mid2, is(meterStore.allocateMeterId(did1)));
333 // Max meter error
334 assertNull(meterStore.allocateMeterId(did1));
335 }
336
337 /**
338 * Test store meter.
339 */
340 @Test
341 public void testStoreMeter() {
342 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200343 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200344 // Simulate the allocation of an id
345 MeterId idOne = meterStore.allocateMeterId(did1);
346 // Verify the allocation
347 assertThat(mid1, is(idOne));
348 // Let's create a meter
349 Meter meterOne = DefaultMeter.builder()
350 .forDevice(did1)
351 .fromApp(APP_ID)
352 .withId(mid1)
353 .withUnit(Meter.Unit.KB_PER_SEC)
354 .withBands(Collections.singletonList(b1))
355 .build();
356 // Set the state
357 ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
358 // Store the meter
359 meterStore.storeMeter(meterOne);
360 // Let's create meter key
361 MeterKey meterKey = MeterKey.key(did1, mid1);
362 // Verify the store
363 assertThat(1, is(meterStore.getAllMeters().size()));
364 assertThat(1, is(meterStore.getAllMeters(did1).size()));
365 assertThat(m1, is(meterStore.getMeter(meterKey)));
366 }
367
368 /**
369 * Test delete meter.
370 */
371 @Test
372 public void testDeleteMeter() {
373 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200374 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200375 // Simulate the allocation of an id
376 MeterId idOne = meterStore.allocateMeterId(did1);
377 // Verify the allocation
378 assertThat(mid1, is(idOne));
379 // Let's create a meter
380 Meter meterOne = DefaultMeter.builder()
381 .forDevice(did1)
382 .fromApp(APP_ID)
383 .withId(mid1)
384 .withUnit(Meter.Unit.KB_PER_SEC)
385 .withBands(Collections.singletonList(b1))
386 .build();
387 // Set the state
388 ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
389 // Store the meter
390 meterStore.storeMeter(meterOne);
391 // Set the state
392 ((DefaultMeter) meterOne).setState(MeterState.PENDING_REMOVE);
393 // Let's create meter key
394 MeterKey meterKey = MeterKey.key(did1, mid1);
395 // Delete meter
396 meterStore.deleteMeter(meterOne);
397 // Start an async delete, simulating the operation of the provider
398 CompletableFuture<Void> future = CompletableFuture.runAsync(
399 () -> meterStore.deleteMeterNow(meterOne)
400 );
401 // Let's wait
402 try {
403 future.get();
404 } catch (InterruptedException | ExecutionException e) {
405 e.printStackTrace();
406 }
407 // Verify delete
408 assertThat(0, is(meterStore.getAllMeters().size()));
409 assertThat(0, is(meterStore.getAllMeters(did1).size()));
410 assertNull(meterStore.getMeter(meterKey));
411 assertThat(mid1, is(meterStore.allocateMeterId(did1)));
412 }
413
414 /**
415 * Test no delete meter.
416 */
417 @Test
418 public void testNoDeleteMeter() {
419 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200420 initMeterStore(false);
Pier Luigif094c612017-10-14 12:15:02 +0200421 // Simulate the allocation of an id
422 MeterId idOne = meterStore.allocateMeterId(did1);
423 // Create the key
424 MeterKey keyOne = MeterKey.key(did1, idOne);
425 // Let's create a meter
426 Meter meterOne = DefaultMeter.builder()
427 .forDevice(did1)
428 .fromApp(APP_ID)
429 .withId(mid1)
430 .withUnit(Meter.Unit.KB_PER_SEC)
431 .withBands(Collections.singletonList(b1))
432 .build();
433 // Set the state
434 ((DefaultMeter) meterOne).setState(MeterState.PENDING_REMOVE);
435 // Delete meter
436 meterStore.deleteMeter(meterOne);
437 // Verify No delete
438 assertThat(0, is(meterStore.getAllMeters().size()));
439 assertThat(0, is(meterStore.getAllMeters(did1).size()));
440 assertNull(meterStore.getMeter(keyOne));
441 }
442
pierventre1b8afbc2020-07-13 14:07:05 +0200443 /**
444 * Test purge meter.
445 */
446 @Test
447 public void testPurgeMeter() {
448 // add the meter
449 testStoreMeter();
450 meterStore.purgeMeter(did1);
451 // Verify delete
452 MeterKey keyOne = MeterKey.key(did1, mid1);
453 assertThat(0, is(meterStore.getAllMeters().size()));
454 assertThat(0, is(meterStore.getAllMeters(did1).size()));
455 assertNull(meterStore.getMeter(keyOne));
Pier Luigif094c612017-10-14 12:15:02 +0200456 }
457
pierventre44220052020-09-22 12:51:06 +0200458 /**
Daniele Moro43ac2892021-07-15 17:02:59 +0200459 * Test purge meter given device and application.
460 */
461 @Test
462 public void testPurgeMeterDeviceAndApp() {
463 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200464 initMeterStore(false);
Daniele Moro43ac2892021-07-15 17:02:59 +0200465 // add the meters
466 ((DefaultMeter) m1).setState(MeterState.PENDING_ADD);
467 ((DefaultMeter) m2).setState(MeterState.PENDING_ADD);
468 ((DefaultMeter) m3).setState(MeterState.PENDING_ADD);
469 meterStore.storeMeter(m1);
470 meterStore.storeMeter(m2);
471 meterStore.storeMeter(m3);
472 assertThat(3, is(meterStore.getAllMeters().size()));
473
474 meterStore.purgeMeters(did1, APP_ID_2);
475 // Verify delete
476 MeterKey keyTwo = MeterKey.key(did1, mid2);
477 assertThat(2, is(meterStore.getAllMeters().size()));
478 assertThat(1, is(meterStore.getAllMeters(did1).size()));
479 assertThat(1, is(meterStore.getAllMeters(did2).size()));
480 assertNull(meterStore.getMeter(keyTwo));
481 }
482
483 /**
pierventre44220052020-09-22 12:51:06 +0200484 * Test getMeters API immutability.
485 */
486 @Test
487 public void testGetMetersImmutability() {
488 // Init the store
pierventre3b39bd82021-08-18 09:40:14 +0200489 initMeterStore(false);
pierventre44220052020-09-22 12:51:06 +0200490
491 // Simulate the allocation of an id
492 MeterId idOne = meterStore.allocateMeterId(did1);
493 // Verify the allocation
494 assertThat(mid1, is(idOne));
495 // Let's create a meter
496 Meter meterOne = DefaultMeter.builder()
497 .forDevice(did1)
498 .fromApp(APP_ID)
499 .withId(mid1)
500 .withUnit(Meter.Unit.KB_PER_SEC)
501 .withBands(Collections.singletonList(b1))
502 .build();
503 // Set the state
504 ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
505 // Store the meter
506 meterStore.storeMeter(meterOne);
507
508 // Verify the immutability
509 Collection<Meter> meters = meterStore.getAllMeters();
510 Collection<Meter> metersDevice = meterStore.getAllMeters(did1);
511 assertThat(1, is(meters.size()));
512 assertThat(1, is(metersDevice.size()));
513
514 MeterId idTwo = meterStore.allocateMeterId(did1);
515 // Verify the allocation
516 assertThat(mid2, is(idTwo));
517 // Let's create a meter
518 Meter meterTwo = DefaultMeter.builder()
519 .forDevice(did1)
520 .fromApp(APP_ID)
521 .withId(mid2)
522 .withUnit(Meter.Unit.KB_PER_SEC)
523 .withBands(Collections.singletonList(b1))
524 .build();
525 // Set the state
526 ((DefaultMeter) meterTwo).setState(MeterState.PENDING_ADD);
527 // Store the meter
528 meterStore.storeMeter(meterTwo);
529
530 assertThat(1, is(meters.size()));
531 assertThat(1, is(metersDevice.size()));
532
533 meters = meterStore.getAllMeters();
534 metersDevice = meterStore.getAllMeters(did1);
535 assertThat(2, is(meters.size()));
536 assertThat(2, is(metersDevice.size()));
pierventre3b39bd82021-08-18 09:40:14 +0200537 }
pierventre44220052020-09-22 12:51:06 +0200538
pierventre3b39bd82021-08-18 09:40:14 +0200539 /**
540 * Test invalid allocation of a cell id.
541 */
542 @Test(expected = IllegalArgumentException.class)
543 public void testInvalidCellId() {
544 initMeterStore(true);
545 // MF defines an end index equals to 10
546 Meter meterBad = DefaultMeter.builder()
547 .forDevice(did3)
548 .fromApp(APP_ID)
549 .withCellId(invalidCid)
550 .withUnit(Meter.Unit.KB_PER_SEC)
551 .withBands(Collections.singletonList(b1))
552 .build();
553 ((DefaultMeter) meterBad).setState(MeterState.PENDING_ADD);
554 meterStore.storeMeter(meterBad);
555 }
pierventre44220052020-09-22 12:51:06 +0200556
pierventre3b39bd82021-08-18 09:40:14 +0200557 /**
558 * Test enabling user defined index mode.
559 */
560 @Test
561 public void testEnableUserDefinedIndex() {
562 initMeterStore(false);
563 assertTrue(meterStore.userDefinedIndexMode(true));
564 }
565
566 /**
567 * Test invalid enabling user defined index mode.
568 */
569 @Test
570 public void testInvalidEnableUserDefinedIndex() {
571 testStoreMeter();
572 assertFalse(meterStore.userDefinedIndexMode(true));
573 }
574
575 /**
576 * Test disabling user defined index mode.
577 */
578 @Test
579 public void testDisableUserDefinedIndex() {
580 initMeterStore(true);
581 assertFalse(meterStore.userDefinedIndexMode(false));
582 }
583
584 /**
585 * Test store meter in user defined index mode.
586 */
587 @Test
588 public void testStoreMeterInUserDefinedIndexMode() {
589 initMeterStore(true);
590 // Let's create a meter
591 Meter meterOne = DefaultMeter.builder()
592 .forDevice(did3)
593 .fromApp(APP_ID)
594 .withCellId(cid4)
595 .withUnit(Meter.Unit.KB_PER_SEC)
596 .withBands(Collections.singletonList(b1))
597 .build();
598 // Set the state
599 ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
600 // Store the meter
601 meterStore.storeMeter(meterOne);
602 // Let's create meter key
603 MeterKey meterKey = MeterKey.key(did3, cid4);
604 // Verify the store
605 assertThat(1, is(meterStore.getAllMeters().size()));
606 assertThat(1, is(meterStore.getAllMeters(did3).size()));
607 assertThat(m4, is(meterStore.getMeter(meterKey)));
608 }
609
610 /**
611 * Test invalid disabling user defined index mode.
612 */
613 @Test
614 public void testInvalidDisableUserDefinedIndex() {
615 testStoreMeterInUserDefinedIndexMode();
616 assertTrue(meterStore.userDefinedIndexMode(false));
617 }
618
619 /**
620 * Test allocation of meter ids in user defined index mode.
621 */
622 @Test
623 public void testAllocateIdInUserDefinedIndexMode() {
624 initMeterStore(true);
625 assertNull(meterStore.allocateMeterId(did1));
626 }
627
628 /**
629 * Test free of meter ids in user defined index mode.
630 */
631 @Test
632 public void testFreeIdInUserMode() {
633 initMeterStore(true);
634 // Free the id and expect not being available
635 meterStore.freeMeterId(did1, mid1);
636 MeterTableKey globalKey = MeterTableKey.key(did1, MeterScope.globalScope());
637 assertNotNull(meterStore.availableMeterIds.get(globalKey));
638 assertTrue(meterStore.availableMeterIds.get(globalKey).isEmpty());
639 }
640
641 /**
642 * Test delete meter in user defined index mode.
643 */
644 @Test
645 public void testDeleteMeterInUserDefinedIndexMode() {
646 initMeterStore(true);
647 Meter meterOne = DefaultMeter.builder()
648 .forDevice(did3)
649 .fromApp(APP_ID)
650 .withCellId(cid4)
651 .withUnit(Meter.Unit.KB_PER_SEC)
652 .withBands(Collections.singletonList(b1))
653 .build();
654 ((DefaultMeter) meterOne).setState(MeterState.PENDING_ADD);
655 meterStore.storeMeter(meterOne);
656
657 ((DefaultMeter) meterOne).setState(MeterState.PENDING_REMOVE);
658 MeterKey meterKey = MeterKey.key(did3, cid4);
659 meterStore.deleteMeter(meterOne);
660 CompletableFuture<Void> future = CompletableFuture.runAsync(
661 () -> meterStore.purgeMeter(meterOne)
662 );
663
664 try {
665 future.get();
666 } catch (InterruptedException | ExecutionException e) {
667 e.printStackTrace();
668 }
669 assertThat(0, is(meterStore.getAllMeters().size()));
670 assertThat(0, is(meterStore.getAllMeters(did3).size()));
671 assertNull(meterStore.getMeter(meterKey));
672 MeterTableKey globalKey = MeterTableKey.key(did1, MeterScope.globalScope());
673 assertNotNull(meterStore.availableMeterIds.get(globalKey));
674 assertTrue(meterStore.availableMeterIds.get(globalKey).isEmpty());
pierventre44220052020-09-22 12:51:06 +0200675 }
676
Pier Luigif094c612017-10-14 12:15:02 +0200677 // Test class for driver service.
678 private class TestDriverService extends DriverServiceAdapter {
679 @Override
680 public DriverHandler createHandler(DeviceId deviceId, String... credentials) {
681 return deviceId.equals(did3) ? new TestDriverHandler() : null;
682 }
683 }
684
685 // Test class for driver handler.
686 private class TestDriverHandler implements DriverHandler {
687
688 @Override
689 public Driver driver() {
690 return null;
691 }
692
693 @Override
694 public DriverData data() {
695 return null;
696 }
697
698 @Override
699 @SuppressWarnings("unchecked")
700 public <T extends Behaviour> T behaviour(Class<T> behaviourClass) {
701 return (T) new TestMeterQuery();
702 }
703
704 @Override
705 public <T> T get(Class<T> serviceClass) {
706 return null;
707 }
708
709 @Override
710 public boolean hasBehaviour(Class<? extends Behaviour> behaviourClass) {
711 return true;
712 }
713 }
714
715 // Test meter query
716 private class TestMeterQuery implements MeterQuery {
717
718 @Override
719 public DriverData data() {
720 return null;
721 }
722
723 @Override
724 public void setData(DriverData data) {
725
726 }
727 @Override
728 public DriverHandler handler() {
729 return null;
730 }
731
732 @Override
733 public void setHandler(DriverHandler handler) {
734
735 }
736
737 @Override
738 public long getMaxMeters() {
739 return 100;
740 }
741 }
742
743}