Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.onosproject.provider.netconf.device.impl; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableSet; |
| 20 | import org.junit.Before; |
Ray Milkey | 1fd702a | 2016-12-05 15:11:06 -0800 | [diff] [blame] | 21 | import org.junit.Ignore; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.ChassisId; |
| 24 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.core.DefaultApplicationId; |
| 29 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 32 | import org.onosproject.net.AbstractProjectableModel; |
| 33 | import org.onosproject.net.Annotations; |
| 34 | import org.onosproject.net.DefaultDevice; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.config.Config; |
| 38 | import org.onosproject.net.config.ConfigFactory; |
| 39 | import org.onosproject.net.config.NetworkConfigEvent; |
| 40 | import org.onosproject.net.config.NetworkConfigListener; |
| 41 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 42 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 43 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 44 | import org.onosproject.net.device.DeviceDescription; |
| 45 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 46 | import org.onosproject.net.device.DeviceEvent; |
| 47 | import org.onosproject.net.device.DeviceListener; |
| 48 | import org.onosproject.net.device.DeviceProvider; |
| 49 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 50 | import org.onosproject.net.device.DeviceProviderRegistryAdapter; |
| 51 | import org.onosproject.net.device.DeviceProviderService; |
| 52 | import org.onosproject.net.device.DeviceProviderServiceAdapter; |
| 53 | import org.onosproject.net.device.DeviceService; |
| 54 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 55 | import org.onosproject.net.device.DeviceStore; |
| 56 | import org.onosproject.net.device.DeviceStoreAdapter; |
| 57 | import org.onosproject.net.device.PortDescription; |
| 58 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 59 | import org.onosproject.net.driver.Behaviour; |
| 60 | import org.onosproject.net.driver.Driver; |
| 61 | import org.onosproject.net.driver.DriverAdapter; |
| 62 | import org.onosproject.net.driver.DriverHandler; |
| 63 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 64 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 65 | import org.onosproject.net.key.DeviceKeyAdminServiceAdapter; |
| 66 | import org.onosproject.net.provider.ProviderId; |
| 67 | import org.onosproject.netconf.NetconfController; |
| 68 | import org.onosproject.netconf.NetconfDeviceListener; |
| 69 | |
| 70 | import java.util.ArrayList; |
| 71 | import java.util.HashMap; |
| 72 | import java.util.HashSet; |
| 73 | import java.util.List; |
| 74 | import java.util.Set; |
| 75 | import java.util.concurrent.CopyOnWriteArraySet; |
| 76 | |
| 77 | import static org.easymock.EasyMock.*; |
| 78 | import static org.junit.Assert.*; |
| 79 | import static org.onlab.junit.TestTools.assertAfter; |
| 80 | import static org.onosproject.provider.netconf.device.impl.NetconfDeviceProvider.APP_NAME; |
| 81 | |
| 82 | /** |
| 83 | * Netconf device provider basic test. |
| 84 | */ |
| 85 | public class NetconfDeviceProviderTest { |
| 86 | |
| 87 | private final NetconfDeviceProvider provider = new NetconfDeviceProvider(); |
| 88 | private final NetconfController controller = new MockNetconfController(); |
| 89 | |
| 90 | //Provider Mock |
| 91 | private final DeviceProviderRegistry deviceRegistry = new MockDeviceProviderRegistry(); |
| 92 | private final DeviceProviderService providerService = new MockDeviceProviderService(); |
| 93 | private final DeviceService deviceService = new MockDeviceService(); |
| 94 | private final MastershipService mastershipService = new MockMastershipService(); |
| 95 | private final Driver driver = new MockDriver(); |
| 96 | private final NetworkConfigRegistry cfgService = new MockNetworkConfigRegistry(); |
| 97 | private final DeviceKeyAdminService deviceKeyAdminService = new DeviceKeyAdminServiceAdapter(); |
| 98 | private final DeviceStore deviceStore = new MockDeviceStore(); |
| 99 | |
| 100 | //Class for testing |
| 101 | private final NetworkConfigEvent deviceAddedEvent = |
| 102 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 103 | null, NetconfProviderConfig.class); |
| 104 | private final NetconfProviderConfig netconfProviderConfig = new MockNetconfProviderConfig(); |
| 105 | private static final String IP = "1.1.1.1"; |
| 106 | private static final String TEST = "test"; |
| 107 | private static final int DELAY_DISCOVERY = 500; |
| 108 | |
| 109 | //Provider related classes |
| 110 | private CoreService coreService; |
| 111 | private ApplicationId appId = |
| 112 | new DefaultApplicationId(100, APP_NAME); |
| 113 | private DeviceDescriptionDiscovery descriptionDiscovery = new TestDescription(); |
| 114 | private Set<DeviceListener> deviceListeners = new HashSet<>(); |
| 115 | private ConfigFactory cfgFactory; |
| 116 | private Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 117 | private HashMap<DeviceId, Device> devices = new HashMap<>(); |
| 118 | |
| 119 | //Controller related classes |
| 120 | private Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
| 121 | |
| 122 | @Before |
| 123 | public void setUp() { |
| 124 | coreService = createMock(CoreService.class); |
| 125 | expect(coreService.registerApplication(APP_NAME)) |
| 126 | .andReturn(appId).anyTimes(); |
| 127 | replay(coreService); |
| 128 | provider.coreService = coreService; |
| 129 | provider.providerRegistry = deviceRegistry; |
| 130 | provider.mastershipService = mastershipService; |
| 131 | provider.deviceService = deviceService; |
| 132 | provider.cfgService = cfgService; |
| 133 | provider.controller = controller; |
| 134 | provider.deviceKeyAdminService = deviceKeyAdminService; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 135 | provider.componentConfigService = new ComponentConfigAdapter(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 136 | AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter()); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 137 | provider.activate(null); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Test |
| 141 | public void activate() throws Exception { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 142 | assertTrue("Provider should be registered", deviceRegistry.getProviders().contains(provider.id())); |
| 143 | assertEquals("Incorrect device service", deviceService, provider.deviceService); |
| 144 | assertEquals("Incorrect provider service", providerService, provider.providerService); |
| 145 | assertEquals("Device listener should be added", 1, deviceListeners.size()); |
| 146 | assertFalse("Thread to connect device should be running", |
| 147 | provider.executor.isShutdown() || provider.executor.isTerminated()); |
| 148 | assertFalse("Scheduled task to update device should be running", provider.scheduledTask.isCancelled()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void deactivate() throws Exception { |
| 153 | provider.deactivate(); |
| 154 | assertEquals("Device listener should be removed", 0, deviceListeners.size()); |
| 155 | assertFalse("Provider should not be registered", deviceRegistry.getProviders().contains(provider)); |
| 156 | assertTrue("Thread to connect device should be shutdown", provider.executor.isShutdown()); |
| 157 | assertTrue("Scheduled task to update device should be shutdown", provider.scheduledTask.isCancelled()); |
| 158 | assertNull("Provider service should be null", provider.providerService); |
| 159 | assertEquals("Controller listener should be removed", 0, netconfDeviceListeners.size()); |
| 160 | } |
| 161 | |
| 162 | @Test |
Ray Milkey | 1fd702a | 2016-12-05 15:11:06 -0800 | [diff] [blame] | 163 | @Ignore("Test is brittle") |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 164 | public void addDevice() { |
| 165 | assertNotNull(providerService); |
| 166 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEvent)); |
| 167 | provider.cfgListener.event(deviceAddedEvent); |
| 168 | |
| 169 | assertAfter(DELAY_DISCOVERY, () -> |
| 170 | assertEquals("Device should be added", 1, deviceStore.getDeviceCount())); |
| 171 | devices.clear(); |
| 172 | } |
| 173 | |
| 174 | //TODO: implement ports discovery and check updates of the device description |
| 175 | |
| 176 | |
| 177 | //Mock classes |
| 178 | private class MockNetconfController extends NetconfControllerAdapter { |
| 179 | |
| 180 | @Override |
| 181 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 182 | if (!netconfDeviceListeners.contains(listener)) { |
| 183 | netconfDeviceListeners.add(listener); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 189 | netconfDeviceListeners.remove(listener); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter { |
| 194 | |
| 195 | Set<ProviderId> providers = new HashSet<>(); |
| 196 | |
| 197 | @Override |
| 198 | public DeviceProviderService register(DeviceProvider provider) { |
| 199 | providers.add(provider.id()); |
| 200 | return providerService; |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void unregister(DeviceProvider provider) { |
| 205 | providers.remove(provider.id()); |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public Set<ProviderId> getProviders() { |
| 210 | return providers; |
| 211 | } |
| 212 | |
| 213 | } |
| 214 | |
| 215 | private class MockDeviceService extends DeviceServiceAdapter { |
| 216 | @Override |
| 217 | public void addListener(DeviceListener listener) { |
| 218 | deviceListeners.add(listener); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void removeListener(DeviceListener listener) { |
| 223 | deviceListeners.remove(listener); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | private class MockDeviceProviderService extends DeviceProviderServiceAdapter { |
| 228 | |
| 229 | @Override |
| 230 | public void deviceConnected(DeviceId deviceId, DeviceDescription desc) { |
| 231 | assertNotNull("DeviceId should be not null", deviceId); |
| 232 | assertNotNull("DeviceDescription should be not null", desc); |
| 233 | deviceStore.createOrUpdateDevice(ProviderId.NONE, deviceId, desc); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private class MockDeviceStore extends DeviceStoreAdapter { |
| 238 | |
| 239 | @Override |
| 240 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 241 | DeviceDescription desc) { |
| 242 | |
| 243 | devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(), |
| 244 | desc.manufacturer(), desc.hwVersion(), |
| 245 | desc.swVersion(), desc.serialNumber(), |
| 246 | desc.chassisId(), desc.annotations())); |
| 247 | |
| 248 | return null; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public Device getDevice(DeviceId deviceId) { |
| 253 | return devices.get(deviceId); |
| 254 | } |
| 255 | |
| 256 | @Override |
| 257 | public int getDeviceCount() { |
| 258 | return devices.size(); |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | |
| 263 | private class MockMastershipService extends MastershipServiceAdapter { |
| 264 | |
| 265 | @Override |
| 266 | public boolean isLocalMaster(DeviceId deviceId) { |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 272 | |
| 273 | @Override |
| 274 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 275 | cfgFactory = configFactory; |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 280 | cfgFactory = null; |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | public void addListener(NetworkConfigListener listener) { |
| 285 | netCfgListeners.add(listener); |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public void removeListener(NetworkConfigListener listener) { |
| 290 | netCfgListeners.remove(listener); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | @Override |
| 295 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 296 | if (configClass.equals(NetconfProviderConfig.class)) { |
| 297 | return (C) netconfProviderConfig; |
| 298 | } else { |
| 299 | return (C) new BasicDeviceConfig(); |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | private class MockNetconfProviderConfig extends NetconfProviderConfig { |
| 305 | protected NetconfDeviceAddress deviceInfo = |
| 306 | new NetconfDeviceAddress(IpAddress.valueOf(IP), 1, TEST, TEST); |
| 307 | |
| 308 | @Override |
| 309 | public Set<NetconfProviderConfig.NetconfDeviceAddress> getDevicesAddresses() throws ConfigException { |
| 310 | return ImmutableSet.of(deviceInfo); |
| 311 | } |
| 312 | |
| 313 | } |
| 314 | |
| 315 | private class MockDevice extends DefaultDevice { |
| 316 | |
| 317 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 318 | String manufacturer, String hwVersion, String swVersion, |
| 319 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 320 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 321 | chassisId, annotations); |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | protected Driver locateDriver() { |
| 326 | return driver; |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public Driver driver() { |
| 331 | return driver; |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | private class MockDriver extends DriverAdapter { |
| 336 | @Override |
| 337 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) { |
| 338 | |
| 339 | return (T) descriptionDiscovery; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | private class TestDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 344 | |
| 345 | List<PortDescription> portDescriptions = new ArrayList<>(); |
| 346 | |
| 347 | @Override |
| 348 | public DeviceDescription discoverDeviceDetails() { |
| 349 | return null; |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public List<PortDescription> discoverPortDetails() { |
| 354 | return portDescriptions; |
| 355 | } |
| 356 | |
| 357 | private void addDeviceDetails() { |
| 358 | |
| 359 | } |
| 360 | |
| 361 | private void addPortDesc(PortDescription portDescription) { |
| 362 | portDescriptions.add(portDescription); |
| 363 | } |
| 364 | } |
| 365 | } |