Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 17 | package org.opencord.olt.impl; |
| 18 | |
| 19 | import org.junit.After; |
| 20 | import org.junit.Assert; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.mockito.Mockito; |
| 24 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 25 | import org.onosproject.core.CoreServiceAdapter; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.meter.MeterId; |
| 28 | import org.onosproject.net.meter.MeterServiceAdapter; |
| 29 | import org.onosproject.net.meter.MeterState; |
| 30 | import org.onosproject.store.service.StorageServiceAdapter; |
| 31 | import org.onosproject.store.service.TestStorageService; |
| 32 | import org.opencord.sadis.SadisService; |
| 33 | |
| 34 | import java.util.HashMap; |
| 35 | import java.util.Map; |
| 36 | import java.util.concurrent.ExecutorService; |
| 37 | import java.util.concurrent.Executors; |
| 38 | import java.util.concurrent.TimeUnit; |
| 39 | |
| 40 | import static org.mockito.Matchers.any; |
| 41 | import static org.mockito.Mockito.doReturn; |
| 42 | import static org.mockito.Mockito.never; |
| 43 | import static org.mockito.Mockito.times; |
| 44 | import static org.mockito.Mockito.verify; |
| 45 | import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT; |
| 46 | |
| 47 | public 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 | } |