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 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 22 | import org.apache.commons.lang.StringUtils; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.ChassisId; |
| 26 | import org.onlab.packet.IpAddress; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigAdapter; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.incubator.net.config.basics.ConfigException; |
| 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 34 | import org.onosproject.net.AbstractProjectableModel; |
| 35 | import org.onosproject.net.Annotations; |
| 36 | import org.onosproject.net.DefaultDevice; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.config.Config; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 40 | import org.onosproject.net.config.ConfigApplyDelegate; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 41 | import org.onosproject.net.config.ConfigFactory; |
| 42 | import org.onosproject.net.config.NetworkConfigEvent; |
| 43 | import org.onosproject.net.config.NetworkConfigListener; |
| 44 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 45 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 46 | import org.onosproject.net.config.basics.BasicDeviceConfig; |
| 47 | import org.onosproject.net.device.DeviceDescription; |
| 48 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 49 | import org.onosproject.net.device.DeviceEvent; |
| 50 | import org.onosproject.net.device.DeviceListener; |
| 51 | import org.onosproject.net.device.DeviceProvider; |
| 52 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 53 | import org.onosproject.net.device.DeviceProviderRegistryAdapter; |
| 54 | import org.onosproject.net.device.DeviceProviderService; |
| 55 | import org.onosproject.net.device.DeviceProviderServiceAdapter; |
| 56 | import org.onosproject.net.device.DeviceService; |
| 57 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 58 | import org.onosproject.net.device.DeviceStore; |
| 59 | import org.onosproject.net.device.DeviceStoreAdapter; |
| 60 | import org.onosproject.net.device.PortDescription; |
| 61 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 62 | import org.onosproject.net.driver.Behaviour; |
| 63 | import org.onosproject.net.driver.Driver; |
| 64 | import org.onosproject.net.driver.DriverAdapter; |
| 65 | import org.onosproject.net.driver.DriverHandler; |
| 66 | import org.onosproject.net.driver.DriverServiceAdapter; |
| 67 | import org.onosproject.net.key.DeviceKeyAdminService; |
| 68 | import org.onosproject.net.key.DeviceKeyAdminServiceAdapter; |
| 69 | import org.onosproject.net.provider.ProviderId; |
| 70 | import org.onosproject.netconf.NetconfController; |
| 71 | import org.onosproject.netconf.NetconfDeviceListener; |
| 72 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 73 | import java.io.IOException; |
| 74 | import java.io.InputStream; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 75 | import java.util.ArrayList; |
| 76 | import java.util.HashMap; |
| 77 | import java.util.HashSet; |
| 78 | import java.util.List; |
| 79 | import java.util.Set; |
| 80 | import java.util.concurrent.CopyOnWriteArraySet; |
| 81 | |
| 82 | import static org.easymock.EasyMock.*; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 83 | import static org.hamcrest.Matchers.is; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 84 | import static org.junit.Assert.*; |
| 85 | import static org.onlab.junit.TestTools.assertAfter; |
| 86 | import static org.onosproject.provider.netconf.device.impl.NetconfDeviceProvider.APP_NAME; |
| 87 | |
| 88 | /** |
| 89 | * Netconf device provider basic test. |
| 90 | */ |
| 91 | public class NetconfDeviceProviderTest { |
| 92 | |
| 93 | private final NetconfDeviceProvider provider = new NetconfDeviceProvider(); |
| 94 | private final NetconfController controller = new MockNetconfController(); |
| 95 | |
| 96 | //Provider Mock |
| 97 | private final DeviceProviderRegistry deviceRegistry = new MockDeviceProviderRegistry(); |
| 98 | private final DeviceProviderService providerService = new MockDeviceProviderService(); |
| 99 | private final DeviceService deviceService = new MockDeviceService(); |
| 100 | private final MastershipService mastershipService = new MockMastershipService(); |
| 101 | private final Driver driver = new MockDriver(); |
| 102 | private final NetworkConfigRegistry cfgService = new MockNetworkConfigRegistry(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 103 | private final Set<ConfigFactory> cfgFactories = new HashSet<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 104 | private final DeviceKeyAdminService deviceKeyAdminService = new DeviceKeyAdminServiceAdapter(); |
| 105 | private final DeviceStore deviceStore = new MockDeviceStore(); |
| 106 | |
| 107 | //Class for testing |
| 108 | private final NetworkConfigEvent deviceAddedEvent = |
| 109 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 110 | DeviceId.deviceId(NETCONF_DEVICE_ID_STRING), NetconfDeviceConfig.class); |
| 111 | private final NetconfDeviceConfig netconfDeviceConfig = new NetconfDeviceConfig(); |
| 112 | private final NetconfDeviceConfig netconfDeviceConfigSshKey = new NetconfDeviceConfig(); |
| 113 | private final NetconfDeviceConfig netconfDeviceConfigEmptyIpv4 = new NetconfDeviceConfig(); |
| 114 | private final NetconfDeviceConfig netconfDeviceConfigEmptyIpv6 = new NetconfDeviceConfig(); |
| 115 | private final NetworkConfigEvent deviceAddedEventOld = |
| 116 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 117 | null, NetconfProviderConfig.class); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 118 | private final NetworkConfigEvent deviceAddedEventTranslated = |
| 119 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 120 | DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD), |
| 121 | NetconfDeviceConfig.class); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 122 | private final NetconfProviderConfig netconfProviderConfig = new MockNetconfProviderConfig(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 123 | private static final String NETCONF_DEVICE_ID_STRING = "netconf:1.1.1.1:830"; |
| 124 | private static final String NETCONF_DEVICE_ID_STRING_OLD = "netconf:1.1.1.2:1"; |
| 125 | private static final String NETCONF_DEVICE_ID_STRING_IPv6 = "netconf:2001:0db8:0000:0000:0000:ff00:0042:8329:830"; |
| 126 | private static final String IP_STRING = "1.1.1.1"; |
| 127 | private static final String IP_STRING_OLD = "1.1.1.2"; |
| 128 | private static final String IP_STRING_IPv6 = "2001:0db8:0000:0000:0000:ff00:0042:8329"; |
| 129 | private static final IpAddress IP = IpAddress.valueOf(IP_STRING); |
| 130 | private static final IpAddress IP_OLD = IpAddress.valueOf(IP_STRING_OLD); |
| 131 | private static final IpAddress IP_v6 = IpAddress.valueOf(IP_STRING_IPv6); |
| 132 | private static final int PORT = 830; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 133 | private static final String TEST = "test"; |
| 134 | private static final int DELAY_DISCOVERY = 500; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 135 | private static final int DELAY_DURATION_DISCOVERY = 1500; |
| 136 | |
| 137 | //Testing Files |
| 138 | InputStream jsonStream = NetconfDeviceProviderTest.class |
| 139 | .getResourceAsStream("/device.json"); |
| 140 | InputStream jsonStreamSshKey = NetconfDeviceProviderTest.class |
| 141 | .getResourceAsStream("/deviceSshKey.json"); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 142 | |
| 143 | //Provider related classes |
| 144 | private CoreService coreService; |
| 145 | private ApplicationId appId = |
| 146 | new DefaultApplicationId(100, APP_NAME); |
| 147 | private DeviceDescriptionDiscovery descriptionDiscovery = new TestDescription(); |
| 148 | private Set<DeviceListener> deviceListeners = new HashSet<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 149 | private Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 150 | private HashMap<DeviceId, Device> devices = new HashMap<>(); |
| 151 | |
| 152 | //Controller related classes |
| 153 | private Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 154 | private boolean available = false; |
| 155 | private boolean firstRequest = true; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 156 | |
| 157 | @Before |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 158 | public void setUp() throws IOException { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 159 | coreService = createMock(CoreService.class); |
| 160 | expect(coreService.registerApplication(APP_NAME)) |
| 161 | .andReturn(appId).anyTimes(); |
| 162 | replay(coreService); |
| 163 | provider.coreService = coreService; |
| 164 | provider.providerRegistry = deviceRegistry; |
| 165 | provider.mastershipService = mastershipService; |
| 166 | provider.deviceService = deviceService; |
| 167 | provider.cfgService = cfgService; |
| 168 | provider.controller = controller; |
| 169 | provider.deviceKeyAdminService = deviceKeyAdminService; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 170 | provider.componentConfigService = new ComponentConfigAdapter(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 171 | AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter()); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 172 | provider.activate(null); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 173 | devices.clear(); |
| 174 | available = false; |
| 175 | firstRequest = true; |
| 176 | DeviceId subject = DeviceId.deviceId(NETCONF_DEVICE_ID_STRING); |
| 177 | DeviceId subjectIpv6 = DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_IPv6); |
| 178 | String key = "netconf"; |
| 179 | ObjectMapper mapper = new ObjectMapper(); |
| 180 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 181 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 182 | netconfDeviceConfig.init(subject, key, jsonNode, mapper, delegate); |
| 183 | JsonNode jsonNodesshKey = mapper.readTree(jsonStreamSshKey); |
| 184 | netconfDeviceConfigSshKey.init(subject, key, jsonNodesshKey, mapper, delegate); |
| 185 | JsonNode jsonNodeEmpty = mapper.createObjectNode(); |
| 186 | netconfDeviceConfigEmptyIpv4.init(subject, key, jsonNodeEmpty, mapper, delegate); |
| 187 | netconfDeviceConfigEmptyIpv6.init(subjectIpv6, key, jsonNodeEmpty, mapper, delegate); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | @Test |
| 191 | public void activate() throws Exception { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 192 | assertTrue("Provider should be registered", deviceRegistry.getProviders().contains(provider.id())); |
| 193 | assertEquals("Incorrect device service", deviceService, provider.deviceService); |
| 194 | assertEquals("Incorrect provider service", providerService, provider.providerService); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 195 | assertTrue("Incorrect config factories", cfgFactories.containsAll(provider.factories)); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 196 | assertEquals("Device listener should be added", 1, deviceListeners.size()); |
| 197 | assertFalse("Thread to connect device should be running", |
| 198 | provider.executor.isShutdown() || provider.executor.isTerminated()); |
| 199 | assertFalse("Scheduled task to update device should be running", provider.scheduledTask.isCancelled()); |
| 200 | } |
| 201 | |
| 202 | @Test |
| 203 | public void deactivate() throws Exception { |
| 204 | provider.deactivate(); |
| 205 | assertEquals("Device listener should be removed", 0, deviceListeners.size()); |
| 206 | assertFalse("Provider should not be registered", deviceRegistry.getProviders().contains(provider)); |
| 207 | assertTrue("Thread to connect device should be shutdown", provider.executor.isShutdown()); |
| 208 | assertTrue("Scheduled task to update device should be shutdown", provider.scheduledTask.isCancelled()); |
| 209 | assertNull("Provider service should be null", provider.providerService); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 210 | assertTrue("Network config factories not removed", cfgFactories.isEmpty()); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 211 | assertEquals("Controller listener should be removed", 0, netconfDeviceListeners.size()); |
| 212 | } |
| 213 | |
| 214 | @Test |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 215 | public void configuration() { |
| 216 | assertTrue("Configuration should be valid", netconfDeviceConfig.isValid()); |
| 217 | assertThat(netconfDeviceConfig.ip(), is(IP)); |
| 218 | assertThat(netconfDeviceConfig.port(), is(PORT)); |
| 219 | assertThat(netconfDeviceConfig.username(), is(TEST)); |
| 220 | assertThat(netconfDeviceConfig.password(), is(TEST)); |
| 221 | assertThat(netconfDeviceConfigSshKey.sshKey(), is(TEST)); |
| 222 | } |
| 223 | |
| 224 | @Test |
| 225 | public void configurationDeviceIdIpv4() { |
| 226 | assertTrue("Configuration should be valid", netconfDeviceConfigEmptyIpv4.isValid()); |
| 227 | assertThat(netconfDeviceConfigEmptyIpv4.ip(), is(IP)); |
| 228 | assertThat(netconfDeviceConfigEmptyIpv4.port(), is(PORT)); |
| 229 | assertThat(netconfDeviceConfigEmptyIpv4.username(), is(StringUtils.EMPTY)); |
| 230 | assertThat(netconfDeviceConfigEmptyIpv4.password(), is(StringUtils.EMPTY)); |
| 231 | assertThat(netconfDeviceConfigEmptyIpv4.sshKey(), is(StringUtils.EMPTY)); |
| 232 | } |
| 233 | |
| 234 | @Test |
| 235 | public void configurationDeviceIdIpv6() { |
| 236 | assertTrue("Configuration should be valid", netconfDeviceConfigEmptyIpv6.isValid()); |
| 237 | assertThat(netconfDeviceConfigEmptyIpv6.ip(), is(IP_v6)); |
| 238 | assertThat(netconfDeviceConfigEmptyIpv6.port(), is(PORT)); |
| 239 | assertThat(netconfDeviceConfigEmptyIpv6.username(), is(StringUtils.EMPTY)); |
| 240 | assertThat(netconfDeviceConfigEmptyIpv6.password(), is(StringUtils.EMPTY)); |
| 241 | assertThat(netconfDeviceConfigEmptyIpv6.sshKey(), is(StringUtils.EMPTY)); |
| 242 | } |
| 243 | |
| 244 | @Test |
| 245 | public void addDeviceOld() { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 246 | assertNotNull(providerService); |
| 247 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEvent)); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 248 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEventOld)); |
| 249 | available = true; |
| 250 | provider.cfgListener.event(deviceAddedEventOld); |
| 251 | |
| 252 | assertAfter(DELAY_DISCOVERY, DELAY_DURATION_DISCOVERY, () -> { |
| 253 | assertEquals("Device should be added", 1, deviceStore.getDeviceCount()); |
| 254 | assertTrue("Device incorrectly added" + NETCONF_DEVICE_ID_STRING_OLD, |
| 255 | devices.containsKey(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD))); |
| 256 | }); |
| 257 | devices.clear(); |
| 258 | } |
| 259 | |
| 260 | @Test |
| 261 | public void addDeviceNew() { |
| 262 | assertNotNull(providerService); |
| 263 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEvent)); |
| 264 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEventOld)); |
| 265 | available = true; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 266 | provider.cfgListener.event(deviceAddedEvent); |
| 267 | |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 268 | assertAfter(DELAY_DISCOVERY, DELAY_DURATION_DISCOVERY, () -> { |
| 269 | assertEquals("Device should be added", 1, deviceStore.getDeviceCount()); |
| 270 | assertTrue("Device incorrectly added" + NETCONF_DEVICE_ID_STRING, |
| 271 | devices.containsKey(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))); |
| 272 | }); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 273 | devices.clear(); |
| 274 | } |
| 275 | |
| 276 | //TODO: implement ports discovery and check updates of the device description |
| 277 | |
| 278 | |
| 279 | //Mock classes |
| 280 | private class MockNetconfController extends NetconfControllerAdapter { |
| 281 | |
| 282 | @Override |
| 283 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 284 | if (!netconfDeviceListeners.contains(listener)) { |
| 285 | netconfDeviceListeners.add(listener); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | @Override |
| 290 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 291 | netconfDeviceListeners.remove(listener); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter { |
| 296 | |
| 297 | Set<ProviderId> providers = new HashSet<>(); |
| 298 | |
| 299 | @Override |
| 300 | public DeviceProviderService register(DeviceProvider provider) { |
| 301 | providers.add(provider.id()); |
| 302 | return providerService; |
| 303 | } |
| 304 | |
| 305 | @Override |
| 306 | public void unregister(DeviceProvider provider) { |
| 307 | providers.remove(provider.id()); |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public Set<ProviderId> getProviders() { |
| 312 | return providers; |
| 313 | } |
| 314 | |
| 315 | } |
| 316 | |
| 317 | private class MockDeviceService extends DeviceServiceAdapter { |
| 318 | @Override |
| 319 | public void addListener(DeviceListener listener) { |
| 320 | deviceListeners.add(listener); |
| 321 | } |
| 322 | |
| 323 | @Override |
| 324 | public void removeListener(DeviceListener listener) { |
| 325 | deviceListeners.remove(listener); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | private class MockDeviceProviderService extends DeviceProviderServiceAdapter { |
| 330 | |
| 331 | @Override |
| 332 | public void deviceConnected(DeviceId deviceId, DeviceDescription desc) { |
| 333 | assertNotNull("DeviceId should be not null", deviceId); |
| 334 | assertNotNull("DeviceDescription should be not null", desc); |
| 335 | deviceStore.createOrUpdateDevice(ProviderId.NONE, deviceId, desc); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | private class MockDeviceStore extends DeviceStoreAdapter { |
| 340 | |
| 341 | @Override |
| 342 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 343 | DeviceDescription desc) { |
| 344 | |
| 345 | devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(), |
| 346 | desc.manufacturer(), desc.hwVersion(), |
| 347 | desc.swVersion(), desc.serialNumber(), |
| 348 | desc.chassisId(), desc.annotations())); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 349 | return null; |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | public Device getDevice(DeviceId deviceId) { |
| 354 | return devices.get(deviceId); |
| 355 | } |
| 356 | |
| 357 | @Override |
| 358 | public int getDeviceCount() { |
| 359 | return devices.size(); |
| 360 | } |
| 361 | |
| 362 | } |
| 363 | |
| 364 | private class MockMastershipService extends MastershipServiceAdapter { |
| 365 | |
| 366 | @Override |
| 367 | public boolean isLocalMaster(DeviceId deviceId) { |
| 368 | return true; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 373 | NetconfDeviceConfig cfg = null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 374 | |
| 375 | @Override |
| 376 | public void registerConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 377 | cfgFactories.add(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 382 | cfgFactories.remove(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public void addListener(NetworkConfigListener listener) { |
| 387 | netCfgListeners.add(listener); |
| 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void removeListener(NetworkConfigListener listener) { |
| 392 | netCfgListeners.remove(listener); |
| 393 | } |
| 394 | |
| 395 | |
| 396 | @Override |
| 397 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 398 | if (available) { |
| 399 | if (configClass.equals(NetconfProviderConfig.class)) { |
| 400 | return (C) netconfProviderConfig; |
| 401 | } |
| 402 | DeviceId did = (DeviceId) subject; |
| 403 | if (configClass.equals(NetconfDeviceConfig.class) |
| 404 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))) { |
| 405 | return (C) netconfDeviceConfig; |
| 406 | } else if (configClass.equals(NetconfDeviceConfig.class) |
| 407 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD))) { |
| 408 | if (firstRequest) { |
| 409 | firstRequest = false; |
| 410 | return null; |
| 411 | } |
| 412 | return (C) cfg; |
| 413 | } else { |
| 414 | return (C) new BasicDeviceConfig(); |
| 415 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 416 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 417 | return null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 418 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 419 | |
| 420 | @Override |
| 421 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, |
| 422 | JsonNode json) { |
| 423 | cfg = new NetconfDeviceConfig(); |
| 424 | ObjectMapper mapper = new ObjectMapper(); |
| 425 | cfg.init((DeviceId) subject, "netconf", mapper.createObjectNode(), mapper, null); |
| 426 | cfg.setIp(json.get("ip").asText()) |
| 427 | .setPort(json.get("port").asInt()) |
| 428 | .setUsername(json.get("username").asText()) |
| 429 | .setPassword(json.get("password").asText()) |
| 430 | .setSshKey(json.get("sshkey").asText()); |
| 431 | provider.cfgListener.event(deviceAddedEventTranslated); |
| 432 | return (C) cfg; |
| 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public <S, C extends Config<S>> Set<S> getSubjects(Class<S> subjectClass, Class<C> configClass) { |
| 437 | Set<S> subjects = new HashSet<S>(); |
| 438 | if (available) { |
| 439 | if (cfg != null) { |
| 440 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD)); |
| 441 | } else { |
| 442 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING)); |
| 443 | } |
| 444 | } |
| 445 | return subjects; |
| 446 | } |
| 447 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | private class MockNetconfProviderConfig extends NetconfProviderConfig { |
| 451 | protected NetconfDeviceAddress deviceInfo = |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 452 | new NetconfDeviceAddress(IP_OLD, 1, TEST, TEST); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 453 | |
| 454 | @Override |
| 455 | public Set<NetconfProviderConfig.NetconfDeviceAddress> getDevicesAddresses() throws ConfigException { |
| 456 | return ImmutableSet.of(deviceInfo); |
| 457 | } |
| 458 | |
| 459 | } |
| 460 | |
| 461 | private class MockDevice extends DefaultDevice { |
| 462 | |
| 463 | public MockDevice(ProviderId providerId, DeviceId id, Type type, |
| 464 | String manufacturer, String hwVersion, String swVersion, |
| 465 | String serialNumber, ChassisId chassisId, Annotations... annotations) { |
| 466 | super(providerId, id, type, manufacturer, hwVersion, swVersion, serialNumber, |
| 467 | chassisId, annotations); |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | protected Driver locateDriver() { |
| 472 | return driver; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public Driver driver() { |
| 477 | return driver; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | private class MockDriver extends DriverAdapter { |
| 482 | @Override |
| 483 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) { |
| 484 | |
| 485 | return (T) descriptionDiscovery; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | private class TestDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 490 | |
| 491 | List<PortDescription> portDescriptions = new ArrayList<>(); |
| 492 | |
| 493 | @Override |
| 494 | public DeviceDescription discoverDeviceDetails() { |
| 495 | return null; |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public List<PortDescription> discoverPortDetails() { |
| 500 | return portDescriptions; |
| 501 | } |
| 502 | |
| 503 | private void addDeviceDetails() { |
| 504 | |
| 505 | } |
| 506 | |
| 507 | private void addPortDesc(PortDescription portDescription) { |
| 508 | portDescriptions.add(portDescription); |
| 509 | } |
| 510 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame^] | 511 | |
| 512 | private class MockDelegate implements ConfigApplyDelegate { |
| 513 | @Override |
| 514 | public void onApply(Config configFile) { |
| 515 | } |
| 516 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 517 | } |