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; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 266 | provider.cfgListener.event(deviceAddedEvent); |
| 267 | |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 268 | deviceAdded.await(); |
| 269 | assertEquals("Device should be added", 1, deviceStore.getDeviceCount()); |
| 270 | assertTrue("Device incorrectly added" + NETCONF_DEVICE_ID_STRING, |
| 271 | devices.containsKey(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 272 | devices.clear(); |
| 273 | } |
| 274 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 275 | @Test |
| 276 | public void testDiscoverPortsAfterDeviceAdded() { |
| 277 | provider.executor = MoreExecutors.newDirectExecutorService(); |
| 278 | prepareMocks(PORT_COUNT); |
| 279 | |
| 280 | deviceService.listener.event(new DeviceEvent(DeviceEvent.Type.DEVICE_ADDED, netconfDevice)); |
| 281 | assertEquals("Ports should be added", PORT_COUNT, providerService.ports.get(netconfDevice.id()).size()); |
| 282 | |
Thomas Vachuska | 5b38dc0 | 2018-05-10 15:24:40 -0700 | [diff] [blame^] | 283 | provider.triggerDisconnect(netconfDevice.id()); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 284 | assertEquals("Ports should be removed", 0, providerService.ports.get(netconfDevice.id()).size()); |
| 285 | } |
| 286 | |
| 287 | private void prepareMocks(int count) { |
Yuta HIGUCHI | d66fe0c | 2017-06-12 14:14:27 -0700 | [diff] [blame] | 288 | for (int i = 1; i <= count; i++) { |
Yuta HIGUCHI | 53e4796 | 2018-03-01 23:50:48 -0800 | [diff] [blame] | 289 | deviceDescription.portDescriptions.add(DefaultPortDescription.builder() |
| 290 | .withPortNumber(PortNumber.portNumber(i)).isEnabled(true).build()); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | //TODO: check updates of the device description |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 295 | |
| 296 | |
| 297 | //Mock classes |
| 298 | private class MockNetconfController extends NetconfControllerAdapter { |
| 299 | |
| 300 | @Override |
| 301 | public void addDeviceListener(NetconfDeviceListener listener) { |
| 302 | if (!netconfDeviceListeners.contains(listener)) { |
| 303 | netconfDeviceListeners.add(listener); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | @Override |
| 308 | public void removeDeviceListener(NetconfDeviceListener listener) { |
| 309 | netconfDeviceListeners.remove(listener); |
| 310 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 311 | |
| 312 | @Override |
| 313 | public void disconnectDevice(DeviceId deviceId, boolean remove) { |
Yuta HIGUCHI | d66fe0c | 2017-06-12 14:14:27 -0700 | [diff] [blame] | 314 | netconfDeviceListeners.forEach(l -> l.deviceRemoved(deviceId)); |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 315 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | private class MockDeviceProviderRegistry extends DeviceProviderRegistryAdapter { |
| 319 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 320 | final Set<ProviderId> providers = new HashSet<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 321 | |
| 322 | @Override |
| 323 | public DeviceProviderService register(DeviceProvider provider) { |
| 324 | providers.add(provider.id()); |
| 325 | return providerService; |
| 326 | } |
| 327 | |
| 328 | @Override |
| 329 | public void unregister(DeviceProvider provider) { |
| 330 | providers.remove(provider.id()); |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public Set<ProviderId> getProviders() { |
| 335 | return providers; |
| 336 | } |
| 337 | |
| 338 | } |
| 339 | |
| 340 | private class MockDeviceService extends DeviceServiceAdapter { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 341 | DeviceListener listener = null; |
| 342 | |
| 343 | @Override |
| 344 | public Device getDevice(DeviceId deviceId) { |
| 345 | if (deviceId.toString().equals(NETCONF_DEVICE_ID_STRING)) { |
| 346 | return null; |
| 347 | } else if (deviceId.uri().getScheme().equals(SCHEME_NAME)) { |
| 348 | return netconfDevice; |
| 349 | } else { |
| 350 | return notNetconfDevice; |
| 351 | } |
| 352 | |
| 353 | } |
| 354 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 355 | @Override |
| 356 | public void addListener(DeviceListener listener) { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 357 | this.listener = listener; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void removeListener(DeviceListener listener) { |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 362 | this.listener = null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | private class MockDeviceProviderService extends DeviceProviderServiceAdapter { |
| 367 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 368 | final Multimap<DeviceId, PortDescription> ports = HashMultimap.create(); |
| 369 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 370 | @Override |
| 371 | public void deviceConnected(DeviceId deviceId, DeviceDescription desc) { |
| 372 | assertNotNull("DeviceId should be not null", deviceId); |
| 373 | assertNotNull("DeviceDescription should be not null", desc); |
| 374 | deviceStore.createOrUpdateDevice(ProviderId.NONE, deviceId, desc); |
| 375 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 376 | |
| 377 | @Override |
| 378 | public void updatePorts(DeviceId deviceId, |
| 379 | List<PortDescription> portDescriptions) { |
| 380 | for (PortDescription p : portDescriptions) { |
| 381 | ports.put(deviceId, p); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | @Override |
| 386 | public void deviceDisconnected(DeviceId deviceId) { |
| 387 | ports.removeAll(deviceId); |
| 388 | } |
| 389 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private class MockDeviceStore extends DeviceStoreAdapter { |
| 393 | |
| 394 | @Override |
| 395 | public DeviceEvent createOrUpdateDevice(ProviderId providerId, DeviceId deviceId, |
| 396 | DeviceDescription desc) { |
| 397 | |
| 398 | devices.put(deviceId, new DefaultDevice(providerId, deviceId, desc.type(), |
| 399 | desc.manufacturer(), desc.hwVersion(), |
| 400 | desc.swVersion(), desc.serialNumber(), |
| 401 | desc.chassisId(), desc.annotations())); |
Yuta HIGUCHI | 795dff4 | 2017-06-23 13:36:44 -0700 | [diff] [blame] | 402 | deviceAdded.countDown(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 403 | return null; |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | public Device getDevice(DeviceId deviceId) { |
| 408 | return devices.get(deviceId); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | public int getDeviceCount() { |
| 413 | return devices.size(); |
| 414 | } |
| 415 | |
| 416 | } |
| 417 | |
| 418 | private class MockMastershipService extends MastershipServiceAdapter { |
| 419 | |
| 420 | @Override |
| 421 | public boolean isLocalMaster(DeviceId deviceId) { |
| 422 | return true; |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 427 | NetconfDeviceConfig cfg = null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 428 | |
| 429 | @Override |
| 430 | public void registerConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 431 | cfgFactories.add(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | @Override |
| 435 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 436 | cfgFactories.remove(configFactory); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public void addListener(NetworkConfigListener listener) { |
| 441 | netCfgListeners.add(listener); |
| 442 | } |
| 443 | |
| 444 | @Override |
| 445 | public void removeListener(NetworkConfigListener listener) { |
| 446 | netCfgListeners.remove(listener); |
| 447 | } |
| 448 | |
| 449 | |
| 450 | @Override |
| 451 | 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] | 452 | if (available) { |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 453 | DeviceId did = (DeviceId) subject; |
| 454 | if (configClass.equals(NetconfDeviceConfig.class) |
| 455 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING))) { |
| 456 | return (C) netconfDeviceConfig; |
| 457 | } else if (configClass.equals(NetconfDeviceConfig.class) |
| 458 | && did.equals(DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD))) { |
| 459 | if (firstRequest) { |
| 460 | firstRequest = false; |
| 461 | return null; |
| 462 | } |
| 463 | return (C) cfg; |
| 464 | } else { |
| 465 | return (C) new BasicDeviceConfig(); |
| 466 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 467 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 468 | return null; |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 469 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 470 | |
| 471 | @Override |
| 472 | public <S, C extends Config<S>> C applyConfig(S subject, Class<C> configClass, |
| 473 | JsonNode json) { |
| 474 | cfg = new NetconfDeviceConfig(); |
| 475 | ObjectMapper mapper = new ObjectMapper(); |
| 476 | cfg.init((DeviceId) subject, "netconf", mapper.createObjectNode(), mapper, null); |
| 477 | cfg.setIp(json.get("ip").asText()) |
| 478 | .setPort(json.get("port").asInt()) |
| 479 | .setUsername(json.get("username").asText()) |
| 480 | .setPassword(json.get("password").asText()) |
| 481 | .setSshKey(json.get("sshkey").asText()); |
| 482 | provider.cfgListener.event(deviceAddedEventTranslated); |
| 483 | return (C) cfg; |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | 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] | 488 | Set<S> subjects = new HashSet<>(); |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 489 | if (available) { |
| 490 | if (cfg != null) { |
| 491 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING_OLD)); |
| 492 | } else { |
| 493 | subjects.add((S) DeviceId.deviceId(NETCONF_DEVICE_ID_STRING)); |
| 494 | } |
| 495 | } |
| 496 | return subjects; |
| 497 | } |
| 498 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 501 | private class MockDevice extends DefaultDevice { |
| 502 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 503 | MockDevice(DeviceId id) { |
| 504 | super(null, id, null, null, null, null, null, |
| 505 | null, DefaultAnnotations.EMPTY); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
| 509 | protected Driver locateDriver() { |
| 510 | return driver; |
| 511 | } |
| 512 | |
| 513 | @Override |
| 514 | public Driver driver() { |
| 515 | return driver; |
| 516 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 517 | |
| 518 | @Override |
| 519 | public <B extends Behaviour> B as(Class<B> projectionClass) { |
| 520 | return (B) deviceDescription; |
| 521 | } |
| 522 | |
| 523 | @Override |
| 524 | public <B extends Behaviour> boolean is(Class<B> projectionClass) { |
| 525 | return projectionClass.isAssignableFrom(DeviceDescriptionDiscovery.class); |
| 526 | } |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | private class MockDriver extends DriverAdapter { |
| 530 | @Override |
| 531 | public <T extends Behaviour> T createBehaviour(DriverHandler handler, Class<T> behaviourClass) { |
| 532 | |
| 533 | return (T) descriptionDiscovery; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | private class TestDescription extends AbstractHandlerBehaviour implements DeviceDescriptionDiscovery { |
| 538 | |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 539 | final List<PortDescription> portDescriptions = new ArrayList<>(); |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 540 | |
| 541 | @Override |
| 542 | public DeviceDescription discoverDeviceDetails() { |
| 543 | return null; |
| 544 | } |
| 545 | |
| 546 | @Override |
| 547 | public List<PortDescription> discoverPortDetails() { |
| 548 | return portDescriptions; |
| 549 | } |
| 550 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 551 | private void addPortDesc(PortDescription portDescription) { |
| 552 | portDescriptions.add(portDescription); |
| 553 | } |
| 554 | } |
Andrea Campanella | 34cf65c | 2017-04-12 13:51:32 +0200 | [diff] [blame] | 555 | |
| 556 | private class MockDelegate implements ConfigApplyDelegate { |
| 557 | @Override |
| 558 | public void onApply(Config configFile) { |
| 559 | } |
| 560 | } |
Michal Mach | 26a90fa | 2017-06-07 11:12:46 +0200 | [diff] [blame] | 561 | |
Michele Santuari | 576f09c | 2016-09-28 14:20:00 +0200 | [diff] [blame] | 562 | } |