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