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