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