Michele Santuari | 208b167 | 2016-08-04 17:33:12 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | 208b167 | 2016-08-04 17:33:12 +0200 | [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 | */ |
| 16 | package org.onosproject.provider.linkdiscovery.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.osgi.ComponentContextAdapter; |
| 22 | import org.onlab.packet.ChassisId; |
| 23 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.DefaultApplicationId; |
| 27 | import org.onosproject.mastership.MastershipService; |
| 28 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 29 | import org.onosproject.net.AbstractProjectableModel; |
| 30 | import org.onosproject.net.AnnotationKeys; |
| 31 | import org.onosproject.net.Annotations; |
| 32 | import org.onosproject.net.ConnectPoint; |
| 33 | import org.onosproject.net.DefaultAnnotations; |
| 34 | import org.onosproject.net.DefaultDevice; |
| 35 | import org.onosproject.net.DefaultLink; |
| 36 | import org.onosproject.net.Device; |
| 37 | import org.onosproject.net.DeviceId; |
| 38 | import org.onosproject.net.Link; |
| 39 | import org.onosproject.net.LinkKey; |
| 40 | import org.onosproject.net.PortNumber; |
| 41 | import org.onosproject.net.behaviour.LinkDiscovery; |
| 42 | import org.onosproject.net.device.DeviceListener; |
| 43 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 44 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 45 | import org.onosproject.net.driver.Behaviour; |
| 46 | import org.onosproject.net.driver.Driver; |
| 47 | import org.onosproject.net.driver.DriverAdapter; |
| 48 | import org.onosproject.net.driver.DriverHandler; |
| 49 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 50 | import org.onosproject.net.link.DefaultLinkDescription; |
| 51 | import org.onosproject.net.link.LinkDescription; |
| 52 | import org.onosproject.net.link.LinkProviderRegistryAdapter; |
| 53 | import org.onosproject.net.link.LinkProviderServiceAdapter; |
| 54 | import org.onosproject.net.link.LinkServiceAdapter; |
| 55 | import org.onosproject.net.provider.ProviderId; |
| 56 | |
| 57 | import java.util.Dictionary; |
| 58 | import java.util.HashSet; |
| 59 | import java.util.Hashtable; |
| 60 | import java.util.Set; |
| 61 | |
| 62 | import static org.easymock.EasyMock.*; |
| 63 | import static org.junit.Assert.*; |
| 64 | import static org.onlab.junit.TestTools.assertAfter; |
| 65 | import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.APP_NAME; |
| 66 | import static org.onosproject.provider.linkdiscovery.impl.LinkDiscoveryProvider.SCHEME_NAME; |
| 67 | |
| 68 | /** |
| 69 | * Test for polling mechanism of the NetconfLinkProvider. |
| 70 | */ |
| 71 | public class LinkDiscoveryProviderTest { |
| 72 | |
| 73 | private static final ComponentContextAdapter CONTEXT = |
| 74 | new ComponentContextAdapter() { |
| 75 | @Override |
| 76 | public Dictionary getProperties() { |
| 77 | Hashtable<String, Integer> props = new Hashtable<>(); |
| 78 | props.put("linkPollFrequencySeconds", 2); |
| 79 | props.put("linkPollDelaySeconds", 1); |
| 80 | return props; |
| 81 | } |
| 82 | }; |
| 83 | // Network Mocks |
| 84 | private static final DeviceId DEVICE_ID_1 = DeviceId.deviceId("netconf:1.1.1.1"); |
| 85 | private static final DeviceId DEVICE_ID_2 = DeviceId.deviceId("netconf:1.1.1.2"); |
| 86 | private static final ConnectPoint CP11 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(1)); |
| 87 | private static final ConnectPoint CP12 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(2)); |
| 88 | private static final ConnectPoint CP13 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(3)); |
| 89 | private static final ConnectPoint CP14 = new ConnectPoint(DEVICE_ID_1, PortNumber.portNumber(4)); |
| 90 | private static final ConnectPoint CP21 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(1)); |
| 91 | private static final ConnectPoint CP22 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(2)); |
| 92 | private static final ConnectPoint CP23 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(3)); |
| 93 | private static final ConnectPoint CP24 = new ConnectPoint(DEVICE_ID_2, PortNumber.portNumber(4)); |
| 94 | private static final DefaultAnnotations DEVICE_ANNOTATIONS = DefaultAnnotations.builder() |
| 95 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build(); |
| 96 | private static final DefaultAnnotations LINK_ANNOTATIONS = DefaultAnnotations.builder() |
| 97 | .set(AnnotationKeys.PROTOCOL, SCHEME_NAME.toUpperCase()).build(); |
| 98 | private static final LinkKey LINKKEY1 = LinkKey.linkKey(CP11, CP21); |
| 99 | private static final LinkDescription LINK1 = new DefaultLinkDescription(CP11, CP21, |
| 100 | Link.Type.DIRECT, |
| 101 | LINK_ANNOTATIONS); |
| 102 | private static final LinkKey LINKKEY2 = LinkKey.linkKey(CP12, CP22); |
| 103 | private static final LinkDescription LINK2 = new DefaultLinkDescription(CP12, CP22, |
| 104 | Link.Type.DIRECT, |
| 105 | LINK_ANNOTATIONS); |
| 106 | private static final LinkKey LINKKEY3 = LinkKey.linkKey(CP13, CP23); |
| 107 | private static final LinkDescription LINK3 = new DefaultLinkDescription(CP13, CP23, |
| 108 | Link.Type.DIRECT, |
| 109 | DefaultAnnotations.builder() |
| 110 | .build()); |
| 111 | private static final LinkKey LINKKEY4 = LinkKey.linkKey(CP14, CP24); |
| 112 | private static final LinkDescription LINK4 = new DefaultLinkDescription(CP14, CP24, |
| 113 | Link.Type.DIRECT, |
| 114 | DefaultAnnotations.builder().build()); |
| 115 | //Service Mocks |
| 116 | private final MockDeviceService deviceService = new MockDeviceService(); |
| 117 | private final LinkProviderRegistryAdapter linkRegistry = new LinkProviderRegistryAdapter(); |
| 118 | private final MastershipService mastershipService = new MockMastershipService(); |
| 119 | private final MockLinkService linkService = new MockLinkService(); |
| 120 | private final Driver driver = new MockDriver(); |
| 121 | //Provider related classes |
| 122 | private LinkProviderServiceAdapter providerService; |
| 123 | private CoreService coreService; |
| 124 | private LinkDiscoveryProvider provider = new LinkDiscoveryProvider(); |
| 125 | private final Device device1 = new MockDevice(provider.id(), DEVICE_ID_1, Device.Type.SWITCH, |
| 126 | "foo.inc", "0", "0", "0", new ChassisId(), |
| 127 | DEVICE_ANNOTATIONS); |
| 128 | private Set<DeviceListener> deviceListeners = new HashSet<>(); |
| 129 | private ApplicationId appId = |
| 130 | new DefaultApplicationId(100, APP_NAME); |
| 131 | private TestLink testLink = new TestLink(); |
| 132 | |
| 133 | @Before |
| 134 | public void setUp() { |
| 135 | coreService = createMock(CoreService.class); |
| 136 | expect(coreService.registerApplication(appId.name())) |
| 137 | .andReturn(appId).anyTimes(); |
| 138 | replay(coreService); |
| 139 | provider.coreService = coreService; |
| 140 | provider.providerRegistry = linkRegistry; |
| 141 | provider.deviceService = deviceService; |
| 142 | provider.mastershipService = mastershipService; |
| 143 | provider.linkService = linkService; |
| 144 | provider.cfgService = new ComponentConfigAdapter(); |
| 145 | AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter()); |
| 146 | provider.activate(null); |
| 147 | providerService = linkRegistry.registeredProvider(); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void activate() throws Exception { |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 152 | assertTrue("Provider should be registered", linkRegistry.getProviders().contains(provider.id())); |
Michele Santuari | 208b167 | 2016-08-04 17:33:12 +0200 | [diff] [blame] | 153 | assertEquals("Device service should be registered", provider.deviceService, deviceService); |
| 154 | assertEquals("Device listener should be added", 1, deviceListeners.size()); |
| 155 | assertNotNull("Registration expected", providerService); |
| 156 | assertEquals("Incorrect provider", provider, providerService.provider()); |
| 157 | assertFalse("Executor should be running", provider.executor.isShutdown()); |
| 158 | assertFalse("Executor should be running", provider.executor.isTerminated()); |
| 159 | assertEquals("Incorrect polling frequency, should be default", 10, |
| 160 | provider.linkPollFrequencySeconds); |
| 161 | assertEquals("Incorrect polling delay , should be default", 20, |
| 162 | provider.linkPollDelaySeconds); |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void modified() throws Exception { |
| 167 | provider.modified(CONTEXT); |
| 168 | assertEquals("Incorrect polling frequency, should be default", 2, |
| 169 | provider.linkPollFrequencySeconds); |
| 170 | assertEquals("Incorrect polling delay , should be default", 1, |
| 171 | provider.linkPollDelaySeconds); |
| 172 | |
| 173 | } |
| 174 | |
| 175 | |
| 176 | @Test |
| 177 | public void deactivate() throws Exception { |
| 178 | provider.deactivate(); |
| 179 | assertEquals("Device listener should be removed", 0, deviceListeners.size()); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 180 | assertFalse("Provider should not be registered", linkRegistry.getProviders().contains(provider.id())); |
Michele Santuari | 208b167 | 2016-08-04 17:33:12 +0200 | [diff] [blame] | 181 | assertTrue(provider.executor.isShutdown()); |
| 182 | assertNull(provider.providerService); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | @Test |
| 187 | public void linksTestForStoredDevice() { |
| 188 | provider.modified(CONTEXT); |
| 189 | providerService.discoveredLinkDescriptions().put(LINKKEY1, LINK1); |
| 190 | providerService.discoveredLinkDescriptions().put(LINKKEY2, LINK2); |
| 191 | providerService.discoveredLinkDescriptions().put(LINKKEY4, LINK4); |
| 192 | testLink.addLinkDesc(LINK2); |
| 193 | testLink.addLinkDesc(LINK3); |
| 194 | assertAfter(1100, () -> { |
| 195 | assertEquals("Total number of link must be 3", 3, providerService.discoveredLinkDescriptions().size()); |
| 196 | assertFalse("Link1 should be removed", |
| 197 | providerService.discoveredLinkDescriptions().containsKey(LINKKEY1)); |
| 198 | assertTrue("Link2 should be present", |
| 199 | providerService.discoveredLinkDescriptions().containsKey(LINKKEY2)); |
| 200 | assertTrue("Link3 should be added", |
| 201 | providerService.discoveredLinkDescriptions().containsKey(LINKKEY3)); |
| 202 | assertEquals("Link3 should be annotated", SCHEME_NAME.toUpperCase(), |
| 203 | providerService.discoveredLinkDescriptions() |
| 204 | .get(LINKKEY3).annotations().value(AnnotationKeys.PROTOCOL)); |
| 205 | assertTrue("Link4 should be present because it is not related to the LinkDiscovery", |
| 206 | providerService.discoveredLinkDescriptions().containsKey(LINKKEY4)); |
| 207 | |
| 208 | }); |
| 209 | clear(); |
| 210 | } |
| 211 | |
| 212 | private void clear() { |
| 213 | testLink.clearLinkDesc(); |
| 214 | providerService.discoveredLinkDescriptions().clear(); |
| 215 | providerService.discoveredLinks().clear(); |
| 216 | } |
| 217 | |
| 218 | private class MockDeviceService extends DeviceServiceAdapter { |
| 219 | |
| 220 | @Override |
| 221 | public Iterable<Device> getAvailableDevices() { |
| 222 | return ImmutableSet.of(device1); |
| 223 | } |
| 224 | |
| 225 | @Override |
| 226 | public void addListener(DeviceListener listener) { |
| 227 | deviceListeners.add(listener); |
| 228 | } |
| 229 | |
| 230 | @Override |
| 231 | public void removeListener(DeviceListener listener) { |
| 232 | deviceListeners.remove(listener); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | private class MockMastershipService extends MastershipServiceAdapter { |
| 237 | |
| 238 | @Override |
| 239 | public boolean isLocalMaster(DeviceId deviceId) { |
| 240 | return true; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | private class MockLinkService extends LinkServiceAdapter { |
| 245 | @Override |
DongRyeol Cha | 9547fc1 | 2018-07-24 15:26:16 +0900 | [diff] [blame] | 246 | public Set<Link> getDeviceIngressLinks(DeviceId deviceId) { |
Michele Santuari | 208b167 | 2016-08-04 17:33:12 +0200 | [diff] [blame] | 247 | Set<Link> links = new HashSet<>(); |
| 248 | providerService.discoveredLinkDescriptions().values() |
| 249 | .forEach(x -> links.add(DefaultLink.builder() |
| 250 | .providerId(provider.id()) |
| 251 | .src(x.src()) |
| 252 | .dst(x.dst()) |
| 253 | .type(x.type()) |
| 254 | .isExpected(x.isExpected()) |
| 255 | .annotations(x.annotations()).build())); |
| 256 | return ImmutableSet.copyOf(links); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | private class MockDevice extends DefaultDevice { |
| 261 | |
| 262 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 263 | String manufacturer, String hwVersion, String swVersion, |
| 264 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 265 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 266 | chassisId, annotations); |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | protected Driver locateDriver() { |
| 271 | return driver; |
| 272 | } |
| 273 | |
| 274 | @Override |
| 275 | public Driver driver() { |
| 276 | return driver; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | private class MockDriver extends DriverAdapter { |
| 281 | @Override |
| 282 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) { |
| 283 | |
| 284 | return (T) testLink; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | private class TestLink extends AbstractHandlerBehaviour implements LinkDiscovery { |
| 289 | Set<LinkDescription> linkDescriptions = new HashSet<>(); |
| 290 | |
| 291 | @Override |
| 292 | public Set<LinkDescription> getLinks() { |
| 293 | return ImmutableSet.copyOf(linkDescriptions); |
| 294 | } |
| 295 | |
| 296 | private void addLinkDesc(LinkDescription link) { |
| 297 | linkDescriptions.add(link); |
| 298 | } |
| 299 | |
| 300 | private void clearLinkDesc() { |
| 301 | linkDescriptions.clear(); |
| 302 | } |
| 303 | |
| 304 | } |
| 305 | } |