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