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