Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 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; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 21 | import com.google.common.collect.HashMultimap; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 22 | import com.google.common.collect.Multimap; |
| 23 | import com.google.common.util.concurrent.MoreExecutors; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 24 | import org.apache.commons.lang.StringUtils; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 25 | import org.junit.Before; |
| 26 | import org.junit.Test; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 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; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 32 | import org.onosproject.mastership.MastershipService; |
| 33 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 34 | import org.onosproject.net.AbstractProjectableModel; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 35 | import org.onosproject.net.DefaultAnnotations; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 36 | import org.onosproject.net.DefaultDevice; |
| 37 | import org.onosproject.net.Device; |
| 38 | import org.onosproject.net.DeviceId; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 39 | import org.onosproject.net.PortNumber; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 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; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 48 | import org.onosproject.net.device.DefaultPortDescription; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 49 | import org.onosproject.net.device.DeviceDescription; |
| 50 | import org.onosproject.net.device.DeviceDescriptionDiscovery; |
| 51 | import org.onosproject.net.device.DeviceEvent; |
| 52 | import org.onosproject.net.device.DeviceListener; |
| 53 | import org.onosproject.net.device.DeviceProvider; |
| 54 | import org.onosproject.net.device.DeviceProviderRegistry; |
| 55 | import org.onosproject.net.device.DeviceProviderRegistryAdapter; |
| 56 | import org.onosproject.net.device.DeviceProviderService; |
| 57 | import org.onosproject.net.device.DeviceProviderServiceAdapter; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 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; |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 83 | import java.util.concurrent.CountDownLatch; |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 84 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 85 | import static org.easymock.EasyMock.createMock; |
| 86 | import static org.easymock.EasyMock.expect; |
| 87 | import static org.easymock.EasyMock.replay; |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 88 | import static org.hamcrest.Matchers.is; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 89 | import static org.junit.Assert.assertEquals; |
| 90 | import static org.junit.Assert.assertFalse; |
| 91 | import static org.junit.Assert.assertNotNull; |
| 92 | import static org.junit.Assert.assertNull; |
| 93 | import static org.junit.Assert.assertThat; |
| 94 | import static org.junit.Assert.assertTrue; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 95 | import static org.onosproject.provider.netconf.device.impl.NetconfDeviceProvider.APP_NAME; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 96 | import static org.onosproject.provider.netconf.device.impl.NetconfDeviceProvider.SCHEME_NAME; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Netconf device provider basic test. |
| 100 | */ |
| 101 | public class NetconfDeviceProviderTest { |
| 102 | |
| 103 | private final NetconfDeviceProvider provider = new NetconfDeviceProvider(); |
| 104 | private final NetconfController controller = new MockNetconfController(); |
| 105 | |
| 106 | //Provider Mock |
| 107 | private final DeviceProviderRegistry deviceRegistry = new MockDeviceProviderRegistry(); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 108 | private final MockDeviceProviderService providerService = new MockDeviceProviderService(); |
| 109 | private final MockDeviceService deviceService = new MockDeviceService(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 110 | private final MastershipService mastershipService = new MockMastershipService(); |
| 111 | private final Driver driver = new MockDriver(); |
| 112 | private final NetworkConfigRegistry cfgService = new MockNetworkConfigRegistry(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 113 | private final Set<ConfigFactory> cfgFactories = new HashSet<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 114 | private final DeviceKeyAdminService deviceKeyAdminService = new DeviceKeyAdminServiceAdapter(); |
| 115 | private final DeviceStore deviceStore = new MockDeviceStore(); |
| 116 | |
| 117 | //Class for testing |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 118 | private final NetconfDeviceConfig netconfDeviceConfig = new NetconfDeviceConfig(); |
| 119 | private final NetconfDeviceConfig netconfDeviceConfigSshKey = new NetconfDeviceConfig(); |
| 120 | private final NetconfDeviceConfig netconfDeviceConfigEmptyIpv4 = new NetconfDeviceConfig(); |
| 121 | private final NetconfDeviceConfig netconfDeviceConfigEmptyIpv6 = new NetconfDeviceConfig(); |
Yuta HIGUCHI | b6e0e91 | 2017-05-18 20:13:52 -0700 | [diff] [blame] | 122 | private final NetworkConfigEvent deviceAddedEvent = |
| 123 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 124 | DeviceId.deviceId(NETCONF_DEVICE_ID_STRING), |
| 125 | netconfDeviceConfig, null, |
| 126 | NetconfDeviceConfig.class); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 127 | private final NetworkConfigEvent deviceAddedEventTranslated = |
| 128 | new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, |
| 129 | DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD), |
| 130 | NetconfDeviceConfig.class); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 131 | private static final String NETCONF_DEVICE_ID_STRING = "netconf:1.1.1.1:830"; |
| 132 | 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] | 133 | 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] | 134 | private static final String IP_STRING = "1.1.1.1"; |
| 135 | private static final String IP_STRING_OLD = "1.1.1.2"; |
Yuta HIGUCHI | ed5b14d | 2017-04-25 15:30:23 -0700 | [diff] [blame] | 136 | 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] | 137 | private static final IpAddress IP = IpAddress.valueOf(IP_STRING); |
| 138 | private static final IpAddress IP_OLD = IpAddress.valueOf(IP_STRING_OLD); |
Yuta HIGUCHI | ed5b14d | 2017-04-25 15:30:23 -0700 | [diff] [blame] | 139 | private static final IpAddress IP_V6 = IpAddress.valueOf(IP_STRING_IPV6); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 140 | private static final int PORT = 830; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 141 | private static final String TEST = "test"; |
| 142 | private static final int DELAY_DISCOVERY = 500; |
Sean Condon | fae8e66 | 2016-12-15 10:25:13 +0000 | [diff] [blame] | 143 | private static final int DELAY_DURATION_DISCOVERY = 3000; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 144 | private static final int PORT_COUNT = 5; |
| 145 | private final TestDescription deviceDescription = new TestDescription(); |
| 146 | private final Device netconfDevice = new MockDevice(DeviceId.deviceId("netconf:127.0.0.1")); |
| 147 | private final Device notNetconfDevice = new MockDevice(DeviceId.deviceId("other:127.0.0.1")); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 148 | |
| 149 | //Testing Files |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 150 | private final InputStream jsonStream = NetconfDeviceProviderTest.class |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 151 | .getResourceAsStream("/device.json"); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 152 | private final InputStream jsonStreamSshKey = NetconfDeviceProviderTest.class |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 153 | .getResourceAsStream("/deviceSshKey.json"); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 154 | |
| 155 | //Provider related classes |
| 156 | private CoreService coreService; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 157 | private final ApplicationId appId = |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 158 | new DefaultApplicationId(100, APP_NAME); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 159 | private final DeviceDescriptionDiscovery descriptionDiscovery = new TestDescription(); |
| 160 | private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 161 | private final HashMap<DeviceId, Device> devices = new HashMap<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 162 | |
| 163 | //Controller related classes |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 164 | private final Set<NetconfDeviceListener> netconfDeviceListeners = new CopyOnWriteArraySet<>(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 165 | private boolean available = false; |
| 166 | private boolean firstRequest = true; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 167 | |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 168 | private CountDownLatch deviceAdded; |
| 169 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 170 | @Before |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 171 | public void setUp() throws IOException { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 172 | coreService = createMock(CoreService.class); |
| 173 | expect(coreService.registerApplication(APP_NAME)) |
| 174 | .andReturn(appId).anyTimes(); |
| 175 | replay(coreService); |
| 176 | provider.coreService = coreService; |
| 177 | provider.providerRegistry = deviceRegistry; |
| 178 | provider.mastershipService = mastershipService; |
| 179 | provider.deviceService = deviceService; |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 180 | provider.providerService = providerService; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 181 | provider.cfgService = cfgService; |
| 182 | provider.controller = controller; |
| 183 | provider.deviceKeyAdminService = deviceKeyAdminService; |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 184 | provider.componentConfigService = new ComponentConfigAdapter(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 185 | AbstractProjectableModel.setDriverService(null, new DriverServiceAdapter()); |
Thomas Vachuska | f3aaa8d | 2017-03-31 10:43:58 -0700 | [diff] [blame] | 186 | provider.activate(null); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 187 | devices.clear(); |
| 188 | available = false; |
| 189 | firstRequest = true; |
| 190 | DeviceId subject = DeviceId.deviceId(NETCONF_DEVICE_ID_STRING); |
Yuta HIGUCHI | ed5b14d | 2017-04-25 15:30:23 -0700 | [diff] [blame] | 191 | DeviceId subjectIpv6 = DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_IPV6); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 192 | String key = "netconf"; |
| 193 | ObjectMapper mapper = new ObjectMapper(); |
| 194 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 195 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 196 | netconfDeviceConfig.init(subject, key, jsonNode, mapper, delegate); |
| 197 | JsonNode jsonNodesshKey = mapper.readTree(jsonStreamSshKey); |
| 198 | netconfDeviceConfigSshKey.init(subject, key, jsonNodesshKey, mapper, delegate); |
| 199 | JsonNode jsonNodeEmpty = mapper.createObjectNode(); |
| 200 | netconfDeviceConfigEmptyIpv4.init(subject, key, jsonNodeEmpty, mapper, delegate); |
| 201 | netconfDeviceConfigEmptyIpv6.init(subjectIpv6, key, jsonNodeEmpty, mapper, delegate); |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 202 | deviceAdded = new CountDownLatch(0); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | @Test |
| 206 | public void activate() throws Exception { |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 207 | assertTrue("Provider should be registered", deviceRegistry.getProviders().contains(provider.id())); |
| 208 | assertEquals("Incorrect device service", deviceService, provider.deviceService); |
| 209 | assertEquals("Incorrect provider service", providerService, provider.providerService); |
Ray Milkey | 0271043 | 2018-02-13 17:08:28 -0800 | [diff] [blame] | 210 | assertTrue("Incorrect config factories", cfgFactories.contains(provider.factory)); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 211 | assertNotNull("Device listener should be added", deviceService.listener); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 212 | assertFalse("Thread to connect device should be running", |
| 213 | provider.executor.isShutdown() || provider.executor.isTerminated()); |
| 214 | assertFalse("Scheduled task to update device should be running", provider.scheduledTask.isCancelled()); |
| 215 | } |
| 216 | |
| 217 | @Test |
| 218 | public void deactivate() throws Exception { |
| 219 | provider.deactivate(); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 220 | assertNull("Device listener should be removed", deviceService.listener); |
Yuta HIGUCHI | fbd9ae9 | 2018-01-24 23:39:06 -0800 | [diff] [blame] | 221 | assertFalse("Provider should not be registered", deviceRegistry.getProviders().contains(provider.id())); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 222 | assertTrue("Thread to connect device should be shutdown", provider.executor.isShutdown()); |
| 223 | assertTrue("Scheduled task to update device should be shutdown", provider.scheduledTask.isCancelled()); |
| 224 | assertNull("Provider service should be null", provider.providerService); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 225 | assertTrue("Network config factories not removed", cfgFactories.isEmpty()); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 226 | assertEquals("Controller listener should be removed", 0, netconfDeviceListeners.size()); |
| 227 | } |
| 228 | |
| 229 | @Test |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 230 | public void configuration() { |
| 231 | assertTrue("Configuration should be valid", netconfDeviceConfig.isValid()); |
| 232 | assertThat(netconfDeviceConfig.ip(), is(IP)); |
| 233 | assertThat(netconfDeviceConfig.port(), is(PORT)); |
| 234 | assertThat(netconfDeviceConfig.username(), is(TEST)); |
| 235 | assertThat(netconfDeviceConfig.password(), is(TEST)); |
| 236 | assertThat(netconfDeviceConfigSshKey.sshKey(), is(TEST)); |
| 237 | } |
| 238 | |
| 239 | @Test |
| 240 | public void configurationDeviceIdIpv4() { |
| 241 | assertTrue("Configuration should be valid", netconfDeviceConfigEmptyIpv4.isValid()); |
| 242 | assertThat(netconfDeviceConfigEmptyIpv4.ip(), is(IP)); |
| 243 | assertThat(netconfDeviceConfigEmptyIpv4.port(), is(PORT)); |
| 244 | assertThat(netconfDeviceConfigEmptyIpv4.username(), is(StringUtils.EMPTY)); |
| 245 | assertThat(netconfDeviceConfigEmptyIpv4.password(), is(StringUtils.EMPTY)); |
| 246 | assertThat(netconfDeviceConfigEmptyIpv4.sshKey(), is(StringUtils.EMPTY)); |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void configurationDeviceIdIpv6() { |
| 251 | assertTrue("Configuration should be valid", netconfDeviceConfigEmptyIpv6.isValid()); |
Yuta HIGUCHI | ed5b14d | 2017-04-25 15:30:23 -0700 | [diff] [blame] | 252 | assertThat(netconfDeviceConfigEmptyIpv6.ip(), is(IP_V6)); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 253 | assertThat(netconfDeviceConfigEmptyIpv6.port(), is(PORT)); |
| 254 | assertThat(netconfDeviceConfigEmptyIpv6.username(), is(StringUtils.EMPTY)); |
| 255 | assertThat(netconfDeviceConfigEmptyIpv6.password(), is(StringUtils.EMPTY)); |
| 256 | assertThat(netconfDeviceConfigEmptyIpv6.sshKey(), is(StringUtils.EMPTY)); |
| 257 | } |
| 258 | |
| 259 | @Test |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 260 | public void addDeviceNew() throws InterruptedException { |
| 261 | // expecting 1 device add |
| 262 | deviceAdded = new CountDownLatch(1); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 263 | assertNotNull(providerService); |
| 264 | assertTrue("Event should be relevant", provider.cfgListener.isRelevant(deviceAddedEvent)); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 265 | available = true; |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 266 | assertFalse("Device should not be reachable" + NETCONF_DEVICE_ID_STRING, |
| 267 | provider.isReachable(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 268 | devices.clear(); |
| 269 | } |
| 270 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 271 | @Test |
| 272 | public void testDiscoverPortsAfterDeviceAdded() { |
| 273 | provider.executor = MoreExecutors.newDirectExecutorService(); |
| 274 | prepareMocks(PORT_COUNT); |
| 275 | |
| 276 | deviceService.listener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, netconfDevice)); |
| 277 | assertEquals("Ports should be added", PORT_COUNT, providerService.ports.get(netconfDevice.id()).size()); |
| 278 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame] | 279 | provider.triggerDisconnect(netconfDevice.id()); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 280 | assertEquals("Ports should be removed", 0, providerService.ports.get(netconfDevice.id()).size()); |
| 281 | } |
| 282 | |
| 283 | private void prepareMocks(int count) { |
Yuta HIGUCHI | d66fe0c | 2017-06-12 14:14:27 -0700 | [diff] [blame] | 284 | for (int i = 1; i <= count; i++) { |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 285 | deviceDescription.portDescriptions.add(DefaultPortDescription.builder() |
| 286 | .withPortNumber(PortNumber.portNumber(i)).isEnabled(true).build()); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 287 | } |
| 288 | } |
| 289 | |
| 290 | //TODO: check updates of the device description |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 291 | |
| 292 | |
| 293 | //Mock classes |
| 294 | private class MockNetconfController extends NetconfControllerAdapter { |
| 295 | |
| 296 | @Override |
| 297 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 298 | if (!netconfDeviceListeners.contains(listener)) { |
| 299 | netconfDeviceListeners.add(listener); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 305 | netconfDeviceListeners.remove(listener); |
| 306 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 307 | |
| 308 | @Override |
| 309 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
Yuta HIGUCHI | d66fe0c | 2017-06-12 14:14:27 -0700 | [diff] [blame] | 310 | netconfDeviceListeners.forEach(l -> l.deviceRemoved(deviceId)); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 311 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter { |
| 315 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 316 | final Set<ProviderId> providers = new HashSet<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 317 | |
| 318 | @Override |
| 319 | public DeviceProviderService register(DeviceProvider provider) { |
| 320 | providers.add(provider.id()); |
| 321 | return providerService; |
| 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public void unregister(DeviceProvider provider) { |
| 326 | providers.remove(provider.id()); |
| 327 | } |
| 328 | |
| 329 | @Override |
| 330 | public Set<ProviderId> getProviders() { |
| 331 | return providers; |
| 332 | } |
| 333 | |
| 334 | } |
| 335 | |
| 336 | private class MockDeviceService extends DeviceServiceAdapter { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 337 | DeviceListener listener = null; |
| 338 | |
| 339 | @Override |
Andrea Campanella | 42e7b86 | 2018-09-21 11:56:48 +0200 | [diff] [blame] | 340 | public boolean isAvailable(DeviceId deviceId) { |
| 341 | return true; |
| 342 | } |
| 343 | |
| 344 | @Override |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 345 | public Device getDevice(DeviceId deviceId) { |
| 346 | if (deviceId.toString().equals(NETCONF_DEVICE_ID_STRING)) { |
| 347 | return null; |
| 348 | } else if (deviceId.uri().getScheme().equals(SCHEME_NAME)) { |
| 349 | return netconfDevice; |
| 350 | } else { |
| 351 | return notNetconfDevice; |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 356 | @Override |
| 357 | public void addListener(DeviceListener listener) { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 358 | this.listener = listener; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public void removeListener(DeviceListener listener) { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 363 | this.listener = null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | private class MockDeviceProviderService extends DeviceProviderServiceAdapter { |
| 368 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 369 | final Multimap<DeviceId, PortDescription> ports = HashMultimap.create(); |
| 370 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 371 | @Override |
| 372 | public void deviceConnected(DeviceId deviceId, DeviceDescription desc) { |
| 373 | assertNotNull("DeviceId should be not null", deviceId); |
| 374 | assertNotNull("DeviceDescription should be not null", desc); |
| 375 | deviceStore.createOrUpdateDevice(ProviderId.NONE, deviceId, desc); |
| 376 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 377 | |
| 378 | @Override |
| 379 | public void updatePorts(DeviceId deviceId, |
| 380 | List<PortDescription> portDescriptions) { |
| 381 | for (PortDescription p : portDescriptions) { |
| 382 | ports.put(deviceId, p); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public void deviceDisconnected(DeviceId deviceId) { |
| 388 | ports.removeAll(deviceId); |
| 389 | } |
| 390 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | private class MockDeviceStore extends DeviceStoreAdapter { |
| 394 | |
| 395 | @Override |
| 396 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 397 | DeviceDescription desc) { |
| 398 | |
| 399 | devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(), |
| 400 | desc.manufacturer(), desc.hwVersion(), |
| 401 | desc.swVersion(), desc.serialNumber(), |
| 402 | desc.chassisId(), desc.annotations())); |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 403 | deviceAdded.countDown(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 404 | return null; |
| 405 | } |
| 406 | |
| 407 | @Override |
| 408 | public Device getDevice(DeviceId deviceId) { |
| 409 | return devices.get(deviceId); |
| 410 | } |
| 411 | |
| 412 | @Override |
| 413 | public int getDeviceCount() { |
| 414 | return devices.size(); |
| 415 | } |
| 416 | |
| 417 | } |
| 418 | |
| 419 | private class MockMastershipService extends MastershipServiceAdapter { |
| 420 | |
| 421 | @Override |
| 422 | public boolean isLocalMaster(DeviceId deviceId) { |
| 423 | return true; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 428 | NetconfDeviceConfig cfg = null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 429 | |
| 430 | @Override |
| 431 | public void registerConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 432 | cfgFactories.add(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | @Override |
| 436 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 437 | cfgFactories.remove(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | @Override |
| 441 | public void addListener(NetworkConfigListener listener) { |
| 442 | netCfgListeners.add(listener); |
| 443 | } |
| 444 | |
| 445 | @Override |
| 446 | public void removeListener(NetworkConfigListener listener) { |
| 447 | netCfgListeners.remove(listener); |
| 448 | } |
| 449 | |
| 450 | |
| 451 | @Override |
| 452 | 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] | 453 | if (available) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 454 | DeviceId did = (DeviceId) subject; |
| 455 | if (configClass.equals(NetconfDeviceConfig.class) |
| 456 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))) { |
| 457 | return (C) netconfDeviceConfig; |
| 458 | } else if (configClass.equals(NetconfDeviceConfig.class) |
| 459 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD))) { |
| 460 | if (firstRequest) { |
| 461 | firstRequest = false; |
| 462 | return null; |
| 463 | } |
| 464 | return (C) cfg; |
| 465 | } else { |
| 466 | return (C) new BasicDeviceConfig(); |
| 467 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 468 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 469 | return null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 470 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 471 | |
| 472 | @Override |
| 473 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, |
| 474 | JsonNode json) { |
| 475 | cfg = new NetconfDeviceConfig(); |
| 476 | ObjectMapper mapper = new ObjectMapper(); |
| 477 | cfg.init((DeviceId) subject, "netconf", mapper.createObjectNode(), mapper, null); |
| 478 | cfg.setIp(json.get("ip").asText()) |
| 479 | .setPort(json.get("port").asInt()) |
| 480 | .setUsername(json.get("username").asText()) |
| 481 | .setPassword(json.get("password").asText()) |
| 482 | .setSshKey(json.get("sshkey").asText()); |
| 483 | provider.cfgListener.event(deviceAddedEventTranslated); |
| 484 | return (C) cfg; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | 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] | 489 | Set<S> subjects = new HashSet<>(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 490 | if (available) { |
| 491 | if (cfg != null) { |
| 492 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD)); |
| 493 | } else { |
| 494 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING)); |
| 495 | } |
| 496 | } |
| 497 | return subjects; |
| 498 | } |
| 499 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 500 | } |
| 501 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 502 | private class MockDevice extends DefaultDevice { |
| 503 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 504 | MockDevice(DeviceId id) { |
| 505 | super(null, id, null, null, null, null, null, |
| 506 | null, DefaultAnnotations.EMPTY); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | @Override |
| 510 | protected Driver locateDriver() { |
| 511 | return driver; |
| 512 | } |
| 513 | |
| 514 | @Override |
| 515 | public Driver driver() { |
| 516 | return driver; |
| 517 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 518 | |
| 519 | @Override |
| 520 | public <B extends Behaviour> B as(Class<B> projectionClass) { |
| 521 | return (B) deviceDescription; |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public <B extends Behaviour> boolean is(Class<B> projectionClass) { |
| 526 | return projectionClass.isAssignableFrom(DeviceDescriptionDiscovery.class); |
| 527 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | private class MockDriver extends DriverAdapter { |
| 531 | @Override |
| 532 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) { |
| 533 | |
| 534 | return (T) descriptionDiscovery; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | private class TestDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 539 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 540 | final List<PortDescription> portDescriptions = new ArrayList<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 541 | |
| 542 | @Override |
| 543 | public DeviceDescription discoverDeviceDetails() { |
| 544 | return null; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public List<PortDescription> discoverPortDetails() { |
| 549 | return portDescriptions; |
| 550 | } |
| 551 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 552 | private void addPortDesc(PortDescription portDescription) { |
| 553 | portDescriptions.add(portDescription); |
| 554 | } |
| 555 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 556 | |
| 557 | private class MockDelegate implements ConfigApplyDelegate { |
| 558 | @Override |
| 559 | public void onApply(Config configFile) { |
| 560 | } |
| 561 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 562 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 563 | } |