Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 7f6d6d2 | 2023-01-09 17:09:50 -0500 | [diff] [blame^] | 2 | * Copyright 2021-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 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 | */ |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 16 | |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 17 | package org.opencord.olt.impl; |
| 18 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 19 | import org.junit.After; |
| 20 | import org.junit.Assert; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 23 | import org.junit.runner.RunWith; |
| 24 | import org.mockito.Mockito; |
| 25 | import org.mockito.runners.MockitoJUnitRunner; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 26 | import org.onlab.packet.ChassisId; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 31 | import org.onosproject.net.AnnotationKeys; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.DefaultDevice; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.PortNumber; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 39 | import org.onosproject.net.device.DeviceService; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 40 | import org.onosproject.net.provider.ProviderId; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 41 | import org.onosproject.store.service.TestStorageService; |
Gustavo Silva | 29fb20e | 2022-05-26 09:59:54 -0300 | [diff] [blame] | 42 | import org.opencord.olt.DiscoveredSubscriber; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 43 | import org.opencord.sadis.SadisService; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 44 | import org.opencord.sadis.SubscriberAndDeviceInformation; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 45 | import org.opencord.sadis.UniTagInformation; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | import org.slf4j.LoggerFactory; |
| 48 | |
Andrea Campanella | 61650a1 | 2022-01-24 18:09:44 -0800 | [diff] [blame] | 49 | import java.util.HashMap; |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 50 | import java.util.LinkedList; |
| 51 | import java.util.List; |
| 52 | import java.util.concurrent.Executors; |
| 53 | import java.util.concurrent.LinkedBlockingQueue; |
| 54 | import java.util.concurrent.TimeUnit; |
| 55 | |
| 56 | import static org.mockito.Matchers.any; |
| 57 | import static org.mockito.Matchers.eq; |
| 58 | import static org.mockito.Mockito.atLeastOnce; |
| 59 | import static org.mockito.Mockito.doReturn; |
| 60 | import static org.mockito.Mockito.never; |
| 61 | import static org.mockito.Mockito.reset; |
| 62 | import static org.mockito.Mockito.spy; |
| 63 | import static org.mockito.Mockito.times; |
| 64 | import static org.mockito.Mockito.verify; |
| 65 | import static org.onlab.util.Tools.groupedThreads; |
| 66 | import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_BP_ID_DEFAULT; |
| 67 | |
| 68 | /** |
| 69 | * Set of tests of the ONOS application component. |
| 70 | */ |
| 71 | |
| 72 | @RunWith(MockitoJUnitRunner.class) |
| 73 | public class OltTest extends OltTestHelpers { |
| 74 | |
| 75 | private Olt component; |
| 76 | private final ApplicationId testAppId = new DefaultApplicationId(1, "org.opencord.olt.test"); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 77 | private final Logger log = LoggerFactory.getLogger(getClass()); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 78 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 79 | private DeviceId deviceId = DeviceId.deviceId("test-device"); |
| 80 | private Device testDevice = new DefaultDevice(ProviderId.NONE, deviceId, Device.Type.OLT, |
| 81 | "testManufacturer", "1.0", "1.0", "SN", new ChassisId(1)); |
| 82 | private Port uniUpdateEnabled = new OltPort(testDevice, true, PortNumber.portNumber(16), |
| 83 | DefaultAnnotations.builder().set(AnnotationKeys.PORT_NAME, "uni-1") |
| 84 | .build()); |
| 85 | private ConnectPoint cp = new ConnectPoint(deviceId, uniUpdateEnabled.number()); |
| 86 | private DiscoveredSubscriber sub; |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 87 | |
| 88 | @Before |
| 89 | public void setUp() { |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 90 | component = new Olt(); |
| 91 | component.requeueDelay = 0; // avoid delays in the queue add to make things easier in testing |
| 92 | component.cfgService = new ComponentConfigAdapter(); |
| 93 | component.deviceService = Mockito.mock(DeviceService.class); |
| 94 | component.storageService = new TestStorageService(); |
| 95 | component.coreService = Mockito.spy(new CoreServiceAdapter()); |
| 96 | component.oltDeviceService = Mockito.mock(OltDeviceService.class); |
| 97 | |
| 98 | doReturn(testAppId).when(component.coreService).registerApplication("org.opencord.olt"); |
| 99 | |
| 100 | component.discoveredSubscriberExecutor = |
| 101 | Executors.newSingleThreadScheduledExecutor( |
| 102 | groupedThreads("onos/olt", |
| 103 | "discovered-cp-%d", log)); |
| 104 | component.flowsExecutor = |
| 105 | Executors.newFixedThreadPool(component.flowProcessingThreads, |
| 106 | groupedThreads("onos/olt-service", |
| 107 | "flows-installer-%d")); |
| 108 | |
| 109 | component.subscriberExecutor = Executors.newFixedThreadPool(component.subscriberProcessingThreads, |
| 110 | groupedThreads("onos/olt-service", |
| 111 | "subscriber-installer-%d")); |
| 112 | SadisService sadisService = Mockito.mock(SadisService.class); |
| 113 | component.oltFlowService = Mockito.mock(OltFlowService.class); |
| 114 | component.sadisService = sadisService; |
| 115 | |
| 116 | // reset the spy on oltFlowService |
| 117 | reset(component.oltFlowService); |
| 118 | |
| 119 | component.bindSadisService(sadisService); |
Andrea Campanella | 61650a1 | 2022-01-24 18:09:44 -0800 | [diff] [blame] | 120 | component.eventsQueues = new HashMap<>(); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 121 | component.eventsQueues.put(cp, new LinkedBlockingQueue<>()); |
| 122 | |
| 123 | component.discoveredSubscriberExecutor.execute(() -> { |
| 124 | component.processDiscoveredSubscribers(); |
| 125 | }); |
| 126 | // create empty service for testing |
| 127 | List<UniTagInformation> uniTagInformationList = new LinkedList<>(); |
| 128 | UniTagInformation empty = new UniTagInformation.Builder().build(); |
| 129 | uniTagInformationList.add(empty); |
| 130 | SubscriberAndDeviceInformation si = new SubscriberAndDeviceInformation(); |
| 131 | si.setUniTagList(uniTagInformationList); |
| 132 | sub = new DiscoveredSubscriber(testDevice, |
| 133 | uniUpdateEnabled, DiscoveredSubscriber.Status.ADDED, |
| 134 | false, si); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 137 | @After |
| 138 | public void tearDown() { |
| 139 | component.deactivate(null); |
| 140 | } |
| 141 | |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 142 | @Test |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 143 | public void testProcessDiscoveredSubscribersBasicPortSuccess() throws Exception { |
| 144 | doReturn(true).when(component.deviceService).isAvailable(any()); |
| 145 | doReturn(sub.port).when(component.deviceService).getPort(any(), any()); |
| 146 | doReturn(true).when(component.oltFlowService).handleBasicPortFlows(eq(sub), eq(DEFAULT_BP_ID_DEFAULT), |
| 147 | eq(DEFAULT_BP_ID_DEFAULT)); |
| 148 | doReturn(true).when(component.oltDeviceService).isLocalLeader(cp.deviceId()); |
| 149 | |
| 150 | // adding the discovered subscriber to the queue |
| 151 | LinkedBlockingQueue<DiscoveredSubscriber> q = component.eventsQueues.get(cp); |
| 152 | q.add(sub); |
| 153 | component.eventsQueues.put(cp, q); |
| 154 | |
| 155 | // check that we're calling the correct method |
| 156 | TimeUnit.MILLISECONDS.sleep(600); |
| 157 | verify(component.oltFlowService, atLeastOnce()).handleBasicPortFlows(eq(sub), eq(DEFAULT_BP_ID_DEFAULT), |
| 158 | eq(DEFAULT_BP_ID_DEFAULT)); |
| 159 | |
| 160 | // check if the method doesn't throw an exception we're removing the subscriber from the queue |
| 161 | LinkedBlockingQueue<DiscoveredSubscriber> updatedQueue = component.eventsQueues.get(cp); |
| 162 | assert updatedQueue.isEmpty(); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 165 | @Test |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 166 | public void testProcessDiscoveredSubscribersBasicPortException() throws Exception { |
| 167 | doReturn(true).when(component.deviceService).isAvailable(any()); |
| 168 | doReturn(sub.port).when(component.deviceService).getPort(any(), any()); |
| 169 | doReturn(false).when(component.oltFlowService).handleBasicPortFlows(any(), eq(DEFAULT_BP_ID_DEFAULT), |
| 170 | eq(DEFAULT_BP_ID_DEFAULT)); |
| 171 | doReturn(true).when(component.oltDeviceService).isLocalLeader(cp.deviceId()); |
| 172 | // replace the queue with a spy |
| 173 | LinkedBlockingQueue<DiscoveredSubscriber> q = component.eventsQueues.get(cp); |
| 174 | LinkedBlockingQueue<DiscoveredSubscriber> spiedQueue = spy(q); |
| 175 | // adding the discovered subscriber to the queue |
| 176 | spiedQueue.add(sub); |
| 177 | component.eventsQueues.put(cp, spiedQueue); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 178 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 179 | TimeUnit.MILLISECONDS.sleep(600); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 180 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 181 | // check that we're calling the correct method, |
| 182 | // since the subscriber is not removed from the queue we're calling the method multiple times |
| 183 | verify(component.oltFlowService, atLeastOnce()).handleBasicPortFlows(eq(sub), eq(DEFAULT_BP_ID_DEFAULT), |
| 184 | eq(DEFAULT_BP_ID_DEFAULT)); |
| 185 | |
| 186 | // check if the method throws an exception we are not removing the subscriber from the queue |
| 187 | verify(spiedQueue, never()).remove(sub); |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 190 | @Test |
| 191 | public void testAddSubscriberToQueue() { |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 192 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 193 | // replace the queue with a spy |
| 194 | LinkedBlockingQueue<DiscoveredSubscriber> q = component.eventsQueues.get(cp); |
| 195 | LinkedBlockingQueue<DiscoveredSubscriber> spiedQueue = spy(q); |
| 196 | component.eventsQueues.put(cp, spiedQueue); |
| 197 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 198 | component.addSubscriberToQueue(sub); |
| 199 | component.addSubscriberToQueue(sub); |
| 200 | |
Matteo Scandolo | d7aa89c | 2021-12-07 10:21:34 -0800 | [diff] [blame] | 201 | verify(spiedQueue, times(2)).add(sub); |
| 202 | Assert.assertEquals(2, spiedQueue.size()); |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 203 | |
| 204 | |
| 205 | |
Matteo Scandolo | 962a6ad | 2018-12-11 15:39:42 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Matteo Scandolo | aa2adde | 2021-09-13 12:45:32 -0700 | [diff] [blame] | 208 | } |