blob: a7d653cf6f221b90db2d5274ba6fb65efb24921b [file] [log] [blame]
Matteo Scandoloaa2adde2021-09-13 12:45:32 -07001/*
2 * Copyright 2021-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
17package org.opencord.olt.impl;
18
19import org.junit.After;
20import org.junit.Assert;
21import org.junit.Before;
22import org.junit.Test;
23import org.mockito.Mockito;
24import org.onosproject.cfg.ComponentConfigAdapter;
25import org.onosproject.core.CoreServiceAdapter;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.meter.MeterId;
28import org.onosproject.net.meter.MeterServiceAdapter;
29import org.onosproject.net.meter.MeterState;
30import org.onosproject.store.service.StorageServiceAdapter;
31import org.onosproject.store.service.TestStorageService;
32import org.opencord.sadis.SadisService;
33
34import java.util.HashMap;
35import java.util.Map;
36import java.util.concurrent.ExecutorService;
37import java.util.concurrent.Executors;
38import java.util.concurrent.TimeUnit;
39
40import static org.mockito.Matchers.any;
41import static org.mockito.Mockito.doReturn;
42import static org.mockito.Mockito.never;
43import static org.mockito.Mockito.times;
44import static org.mockito.Mockito.verify;
45import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT;
46
47public class OltMeterServiceTest extends OltTestHelpers {
48 OltMeterService oltMeterService;
49 OltMeterService component;
50
51 DeviceId deviceId = DeviceId.deviceId("foo");
52
53 @Before
54 public void setUp() {
55 component = new OltMeterService();
56 component.cfgService = new ComponentConfigAdapter();
57 component.coreService = new CoreServiceAdapter();
58 component.storageService = new StorageServiceAdapter();
59 component.sadisService = Mockito.mock(SadisService.class);
60 component.meterService = new MeterServiceAdapter();
61 component.storageService = new TestStorageService();
62 component.activate(null);
63 oltMeterService = Mockito.spy(component);
64 }
65
66 @After
67 public void tearDown() {
68 component.deactivate(null);
69 }
70
71 @Test
72 public void testHasMeter() {
73
74 MeterData meterPending = new MeterData(MeterId.meterId(1),
75 MeterState.PENDING_ADD, "pending");
76 MeterData meterAdded = new MeterData(MeterId.meterId(2),
77 MeterState.ADDED, DEFAULT_BP_ID_DEFAULT);
78
79 Map<String, MeterData> deviceMeters = new HashMap<>();
80 deviceMeters.put("pending", meterPending);
81 deviceMeters.put(DEFAULT_BP_ID_DEFAULT, meterAdded);
82 oltMeterService.programmedMeters.put(deviceId, deviceMeters);
83
84 assert oltMeterService.hasMeterByBandwidthProfile(deviceId, DEFAULT_BP_ID_DEFAULT);
85 assert !oltMeterService.hasMeterByBandwidthProfile(deviceId, "pending");
86 assert !oltMeterService.hasMeterByBandwidthProfile(deviceId, "someBandwidthProfile");
87
88 assert !oltMeterService.hasMeterByBandwidthProfile(DeviceId.deviceId("bar"), DEFAULT_BP_ID_DEFAULT);
89 }
90
91 @Test
92 public void testGetMeterId() {
93
94 MeterData meterAdded = new MeterData(MeterId.meterId(2),
95 MeterState.ADDED, DEFAULT_BP_ID_DEFAULT);
96
97 Map<String, MeterData> deviceMeters = new HashMap<>();
98 deviceMeters.put(DEFAULT_BP_ID_DEFAULT, meterAdded);
99 oltMeterService.programmedMeters.put(deviceId, deviceMeters);
100
101 Assert.assertNull(oltMeterService.getMeterIdForBandwidthProfile(deviceId, "pending"));
102 Assert.assertEquals(MeterId.meterId(2),
103 oltMeterService.getMeterIdForBandwidthProfile(deviceId, DEFAULT_BP_ID_DEFAULT));
104 }
105
106 @Test
107 public void testCreateMeter() {
108
109 DeviceId deviceId = DeviceId.deviceId("foo");
110 String bp = "Default";
111
112 // if we already have a meter do nothing and return true
113 doReturn(true).when(oltMeterService).hasMeterByBandwidthProfile(deviceId, bp);
114 Assert.assertTrue(oltMeterService.createMeter(deviceId, bp));
115 verify(oltMeterService, never()).createMeterForBp(any(), any());
116
117 // if we have a pending meter, do nothing and return false
118 doReturn(false).when(oltMeterService).hasMeterByBandwidthProfile(deviceId, bp);
119 doReturn(true).when(oltMeterService).hasPendingMeterByBandwidthProfile(deviceId, bp);
120 Assert.assertFalse(oltMeterService.createMeter(deviceId, bp));
121 verify(oltMeterService, never()).createMeterForBp(any(), any());
122
123 // if the meter is not present at all, create it and return false
124 doReturn(false).when(oltMeterService).hasMeterByBandwidthProfile(deviceId, bp);
125 doReturn(false).when(oltMeterService).hasPendingMeterByBandwidthProfile(deviceId, bp);
126 Assert.assertFalse(oltMeterService.createMeter(deviceId, bp));
127 verify(oltMeterService, times(1)).createMeterForBp(deviceId, bp);
128 }
129
130 @Test
131 public void testConcurrentMeterCreation() throws InterruptedException {
132
133 ExecutorService executor = Executors.newFixedThreadPool(4);
134
135 DeviceId deviceId = DeviceId.deviceId("foo");
136 String bp = "Default";
137
138 // try to create 4 meters at the same time, only one should be created
139 for (int i = 0; i < 4; i++) {
140
141 executor.execute(() -> {
142 oltMeterService.createMeter(deviceId, bp);
143 });
144 }
145
146 TimeUnit.MILLISECONDS.sleep(600);
147
148 verify(oltMeterService, times(4)).hasMeterByBandwidthProfile(deviceId, bp);
149 verify(oltMeterService, times(1)).createMeterForBp(deviceId, bp);
150 }
151}