Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [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 | */ |
Yuta HIGUCHI | e3ae821 | 2017-04-20 10:18:41 -0700 | [diff] [blame] | 16 | package org.onosproject.netconf.ctl.impl; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 17 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 20 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 21 | import org.easymock.EasyMock; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 25 | import org.onlab.osgi.ComponentContextAdapter; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 28 | import org.onosproject.cfg.ComponentConfigService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 29 | import org.onosproject.net.DeviceId; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 30 | import org.onosproject.net.config.Config; |
| 31 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 32 | import org.onosproject.net.config.ConfigFactory; |
| 33 | import org.onosproject.net.config.NetworkConfigListener; |
| 34 | import org.onosproject.net.config.NetworkConfigRegistry; |
| 35 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 36 | import org.onosproject.net.device.DeviceService; |
| 37 | import org.onosproject.net.key.DeviceKeyService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 38 | import org.onosproject.netconf.NetconfDevice; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 39 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 40 | import org.onosproject.netconf.NetconfDeviceListener; |
| 41 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 42 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 43 | import org.onosproject.netconf.NetconfException; |
| 44 | import org.onosproject.netconf.NetconfSession; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 45 | import org.onosproject.netconf.config.NetconfDeviceConfig; |
| 46 | import org.onosproject.netconf.config.NetconfSshClientLib; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 47 | import org.osgi.service.component.ComponentContext; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 48 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 49 | import java.io.ByteArrayInputStream; |
| 50 | import java.io.InputStream; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 51 | import java.lang.reflect.Field; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 52 | import java.util.Dictionary; |
| 53 | import java.util.Enumeration; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 54 | import java.util.HashSet; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 55 | import java.util.Map; |
| 56 | import java.util.Optional; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 57 | import java.util.Set; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 58 | |
| 59 | import static org.hamcrest.Matchers.*; |
| 60 | import static org.junit.Assert.*; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 61 | import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_CONNECT_TIMEOUT_DEFAULT; |
| 62 | import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_IDLE_TIMEOUT_DEFAULT; |
| 63 | import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_REPLY_TIMEOUT_DEFAULT; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * Unit tests for the Netconf controller implementation test. |
| 67 | */ |
| 68 | public class NetconfControllerImplTest { |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 69 | private final Set<ConfigFactory> cfgFactories = new HashSet<>(); |
| 70 | private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>(); |
| 71 | private boolean available = false; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 72 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 73 | NetconfControllerImpl ctrl; |
| 74 | |
| 75 | //DeviceInfo |
| 76 | NetconfDeviceInfo deviceInfo1; |
| 77 | NetconfDeviceInfo deviceInfo2; |
| 78 | NetconfDeviceInfo badDeviceInfo3; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 79 | NetconfDeviceInfo deviceInfoIpV6; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 80 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 81 | NetconfDeviceConfig deviceConfig10; |
| 82 | DeviceId deviceConfig10Id; |
| 83 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 84 | //Devices & DeviceId |
| 85 | NetconfDevice device1; |
| 86 | DeviceId deviceId1; |
| 87 | NetconfDevice device2; |
| 88 | DeviceId deviceId2; |
| 89 | |
| 90 | //Events |
| 91 | NetconfDeviceOutputEvent eventForDeviceInfo1; |
| 92 | NetconfDeviceOutputEvent eventForDeviceInfo2; |
| 93 | |
| 94 | private Map<DeviceId, NetconfDevice> reflectedDeviceMap; |
| 95 | private NetconfDeviceOutputEventListener reflectedDownListener; |
| 96 | |
| 97 | //Test Device IP addresses and ports |
| 98 | private static final String DEVICE_1_IP = "10.10.10.11"; |
| 99 | private static final String DEVICE_2_IP = "10.10.10.12"; |
| 100 | private static final String BAD_DEVICE_IP = "10.10.10.13"; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 101 | private static final String DEVICE_10_IP = "10.10.10.10"; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 102 | private static final String DEVICE_IPV6 = "2001:db8::1"; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 103 | |
| 104 | private static final int DEVICE_1_PORT = 11; |
| 105 | private static final int DEVICE_2_PORT = 12; |
| 106 | private static final int BAD_DEVICE_PORT = 13; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 107 | private static final int IPV6_DEVICE_PORT = 14; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 108 | private static final int DEVICE_10_PORT = 10; |
| 109 | |
| 110 | private static final String DEVICE_10_USERNAME = "device10"; |
| 111 | private static final String DEVICE_10_PASSWORD = "010"; |
| 112 | private static final int DEVICE_10_CONNECT_TIMEOUT = 10; |
| 113 | private static final int DEVICE_10_REPLY_TIMEOUT = 11; |
| 114 | private static final int DEVICE_10_IDLE_TIMEOUT = 12; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 115 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 116 | private static ComponentConfigService cfgService = new ComponentConfigAdapter(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 117 | private static DeviceService deviceService = new NetconfDeviceServiceMock(); |
| 118 | private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock(); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 119 | private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry(); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 120 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 121 | private final ComponentContext context = new MockComponentContext(); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 122 | |
| 123 | @Before |
| 124 | public void setUp() throws Exception { |
| 125 | ctrl = new NetconfControllerImpl(); |
Yuta HIGUCHI | 2ee4fba | 2018-06-12 16:21:06 -0700 | [diff] [blame] | 126 | ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 127 | ctrl.cfgService = cfgService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 128 | ctrl.deviceService = deviceService; |
| 129 | ctrl.deviceKeyService = deviceKeyService; |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 130 | ctrl.netCfgService = netCfgService; |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 131 | NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
| 132 | NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
| 133 | NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 134 | |
| 135 | //Creating mock devices |
| 136 | deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT); |
| 137 | deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT); |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 138 | deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA)); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 139 | badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 140 | deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 141 | |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 142 | deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT); |
| 143 | //Create a JSON entry just like Network Config accepts |
| 144 | ObjectMapper mapper = new ObjectMapper(); |
| 145 | String jsonMessage = "{\n" + |
| 146 | " \"ip\":\"" + DEVICE_10_IP + "\",\n" + |
| 147 | " \"port\":" + DEVICE_10_PORT + ",\n" + |
| 148 | " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" + |
| 149 | " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" + |
| 150 | " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" + |
| 151 | " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" + |
| 152 | " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" + |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 153 | " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" + |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 154 | "}"; |
| 155 | InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes()); |
| 156 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 157 | jsonStream.close(); |
| 158 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 159 | deviceConfig10 = new NetconfDeviceConfig(); |
| 160 | deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate); |
| 161 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 162 | device1 = new TestNetconfDevice(deviceInfo1); |
| 163 | deviceId1 = deviceInfo1.getDeviceId(); |
| 164 | device2 = new TestNetconfDevice(deviceInfo2); |
| 165 | deviceId2 = deviceInfo2.getDeviceId(); |
| 166 | |
| 167 | //Adding to the map for testing get device calls. |
| 168 | Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap"); |
| 169 | field1.setAccessible(true); |
| 170 | reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl); |
| 171 | reflectedDeviceMap.put(deviceId1, device1); |
| 172 | reflectedDeviceMap.put(deviceId2, device2); |
| 173 | |
| 174 | //Creating mock events for testing NetconfDeviceOutputEventListener |
| 175 | Field field2 = ctrl.getClass().getDeclaredField("downListener"); |
| 176 | field2.setAccessible(true); |
| 177 | reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl); |
| 178 | |
| 179 | eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null, |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 180 | null, Optional.of(1), deviceInfo1); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 181 | eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 182 | null, Optional.of(2), deviceInfo2); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | @After |
| 186 | public void tearDown() { |
| 187 | ctrl.deactivate(); |
Yuta HIGUCHI | 09ae368 | 2017-08-14 18:56:54 -0700 | [diff] [blame] | 188 | // resetting static variables.. |
Thomas Vachuska | 00b5d4f | 2018-10-30 15:13:20 -0700 | [diff] [blame] | 189 | NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT; |
| 190 | NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT; |
| 191 | NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | /** |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 195 | * Test initialization of component configuration. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testActivate() { |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 199 | assertEquals("Incorrect NetConf connect timeout, should be default", |
| 200 | 5, ctrl.netconfConnectTimeout); |
| 201 | assertEquals("Incorrect NetConf reply timeout, should be default", |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 202 | 5, ctrl.netconfReplyTimeout); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 203 | ctrl.activate(null); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 204 | assertEquals("Incorrect NetConf connect timeout, should be default", |
| 205 | 5, ctrl.netconfConnectTimeout); |
| 206 | assertEquals("Incorrect NetConf reply timeout, should be default", |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 207 | 5, ctrl.netconfReplyTimeout); |
| 208 | } |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 209 | |
| 210 | /** |
| 211 | * Test modification of component configuration. |
| 212 | */ |
| 213 | @Test |
| 214 | public void testModified() { |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 215 | assertEquals("Incorrect NetConf connect timeout, should be default", |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 216 | 5, ctrl.netconfConnectTimeout); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 217 | assertEquals("Incorrect NetConf session timeout, should be default", |
| 218 | 5, ctrl.netconfReplyTimeout); |
| 219 | ctrl.modified(context); |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 220 | assertEquals("Incorrect NetConf connect timeout, should be default", |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 221 | 2, ctrl.netconfConnectTimeout); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 222 | assertEquals("Incorrect NetConf session timeout", |
| 223 | 1, ctrl.netconfReplyTimeout); |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 224 | assertEquals(NetconfSshClientLib.APACHE_MINA.toString(), ctrl.sshLibrary.toString()); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | /** |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 228 | * Test to add DeviceListeners, |
| 229 | * and also to check whether the netconfDeviceListeners set is |
| 230 | * updating or not which was present in NetconfControllerImpl class. |
| 231 | */ |
| 232 | @Test |
| 233 | public void testAddRemoveDeviceListener() { |
| 234 | NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class); |
| 235 | NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class); |
| 236 | NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class); |
| 237 | |
| 238 | ctrl.addDeviceListener(deviceListener1); |
| 239 | ctrl.addDeviceListener(deviceListener2); |
| 240 | ctrl.addDeviceListener(deviceListener3); |
| 241 | assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3)); |
| 242 | assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1, |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 243 | deviceListener2, deviceListener3)); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 244 | |
| 245 | ctrl.removeDeviceListener(deviceListener1); |
| 246 | assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2)); |
| 247 | assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3)); |
| 248 | } |
| 249 | |
| 250 | @Test |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 251 | public void testGetNetconfDevices() { |
| 252 | Set<DeviceId> devices = new HashSet<>(); |
| 253 | devices.add(deviceId1); |
| 254 | devices.add(deviceId2); |
| 255 | assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices)); |
| 256 | } |
| 257 | |
| 258 | @Test |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 259 | public void testGetNetconfDevice() { |
| 260 | NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1); |
| 261 | assertThat("Incorrect device fetched", fetchedDevice1, is(device1)); |
| 262 | |
| 263 | NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2); |
| 264 | assertThat("Incorrect device fetched", fetchedDevice2, is(device2)); |
| 265 | } |
| 266 | |
| 267 | @Test |
| 268 | public void testGetNetconfDeviceWithIPPort() { |
| 269 | NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT); |
| 270 | assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip()); |
| 271 | |
| 272 | NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT); |
| 273 | assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip()); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Check for bad device connection. In this case the device map shouldn't get modified. |
| 278 | */ |
| 279 | @Test(expected = NetconfException.class) |
| 280 | public void testConnectBadDevice() throws Exception { |
| 281 | reflectedDeviceMap.clear(); |
| 282 | try { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 283 | ctrl.connectDevice(badDeviceInfo3.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 284 | } finally { |
| 285 | assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size()); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 290 | * Check for connection by netconfDeviceConfig. |
| 291 | */ |
| 292 | @Test |
| 293 | public void testConnectDeviceNetConfig10() throws Exception { |
| 294 | NetconfDevice fetchedDevice10 = ctrl.connectDevice(deviceConfig10Id); |
| 295 | assertEquals("Incorrect device fetched - ip", |
| 296 | fetchedDevice10.getDeviceInfo().ip().toString(), DEVICE_10_IP); |
| 297 | assertEquals("Incorrect device fetched - port", |
| 298 | fetchedDevice10.getDeviceInfo().port(), DEVICE_10_PORT); |
| 299 | assertEquals("Incorrect device fetched - username", |
| 300 | fetchedDevice10.getDeviceInfo().name(), DEVICE_10_USERNAME); |
| 301 | assertEquals("Incorrect device fetched - password", |
| 302 | fetchedDevice10.getDeviceInfo().password(), DEVICE_10_PASSWORD); |
| 303 | assertEquals("Incorrect device fetched - connectTimeout", |
| 304 | fetchedDevice10.getDeviceInfo().getConnectTimeoutSec().getAsInt(), |
| 305 | DEVICE_10_CONNECT_TIMEOUT); |
| 306 | assertEquals("Incorrect device fetched - replyTimeout", |
| 307 | fetchedDevice10.getDeviceInfo().getReplyTimeoutSec().getAsInt(), |
| 308 | DEVICE_10_REPLY_TIMEOUT); |
| 309 | assertEquals("Incorrect device fetched - idleTimeout", |
| 310 | fetchedDevice10.getDeviceInfo().getIdleTimeoutSec().getAsInt(), |
| 311 | DEVICE_10_IDLE_TIMEOUT); |
| 312 | assertEquals("Incorrect device fetched - sshClient", |
| 313 | fetchedDevice10.getDeviceInfo().sshClientLib().get(), |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 314 | NetconfSshClientLib.APACHE_MINA); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 318 | * Check for correct device connection. In this case the device map get modified. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testConnectCorrectDevice() throws Exception { |
| 322 | reflectedDeviceMap.clear(); |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 323 | NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 324 | NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId()); |
David K. Bainbridge | 56e9023 | 2018-12-18 23:25:08 -0800 | [diff] [blame^] | 325 | assertTrue(String.format("Incorrect device connection from '%s' we get '%s' contains '%s'", |
| 326 | deviceInfo1, ctrl.getDevicesMap(), deviceId1), |
| 327 | ctrl.getDevicesMap().containsKey(deviceId1)); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 328 | assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2)); |
| 329 | assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size()); |
| 330 | } |
| 331 | |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Check for correct ipv6 device connection. In this case the device map get modified. |
| 334 | */ |
| 335 | @Test |
| 336 | public void testConnectCorrectIpv6Device() throws Exception { |
| 337 | reflectedDeviceMap.clear(); |
| 338 | ctrl.connectDevice(deviceInfoIpV6.getDeviceId()); |
| 339 | assertTrue("Incorrect device connection", ctrl.getDevicesMap() |
| 340 | .containsKey(deviceInfoIpV6.getDeviceId())); |
| 341 | assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size()); |
| 342 | } |
| 343 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 344 | |
| 345 | /** |
| 346 | * Check for connect devices already added to the map. |
| 347 | */ |
| 348 | @Test |
| 349 | public void testConnectAlreadyExistingDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 350 | NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 351 | NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 352 | assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 353 | deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 354 | assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 355 | deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 359 | * Check that disconnectDevice actually disconnects the device and removes it. |
| 360 | */ |
| 361 | @Test |
| 362 | public void testDisconnectDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 363 | ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 364 | assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Checks that disconnectDevice actually disconnects the device and removes it. |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 369 | */ |
| 370 | @Test |
| 371 | public void testRemoveDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 372 | ctrl.removeDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 373 | assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Test to get the connected device map. |
| 378 | */ |
| 379 | @Test |
| 380 | public void testGetDevicesMap() { |
| 381 | assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size()); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Test to check whether the DeviceDownEventListener removes the device from the map when session |
| 386 | * for a particular device getting closed. |
| 387 | */ |
| 388 | @Test |
| 389 | public void testDeviceDownEventListener() throws Exception { |
| 390 | reflectedDeviceMap.clear(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 391 | ctrl.connectDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 392 | boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2); |
| 393 | assertFalse("Irrelevant Device Event", result1); |
| 394 | assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size()); |
| 395 | reflectedDownListener.event(eventForDeviceInfo1); |
| 396 | assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size()); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 397 | ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 398 | boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2); |
| 399 | assertTrue("Irrelevant Device Event", result2); |
| 400 | assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size()); |
| 401 | reflectedDownListener.event(eventForDeviceInfo2); |
| 402 | assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size()); |
| 403 | } |
| 404 | |
| 405 | /** |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 406 | * Mock NetconfDeviceImpl class, used for creating test devices. |
| 407 | */ |
| 408 | protected class TestNetconfDevice implements NetconfDevice { |
| 409 | private NetconfDeviceInfo netconfDeviceInfo; |
| 410 | private boolean deviceState = false; |
| 411 | private NetconfSession netconfSession; |
| 412 | |
| 413 | public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException { |
| 414 | netconfDeviceInfo = deviceInfo; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 415 | if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) { |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 416 | netconfSession = EasyMock.createMock(NetconfSession.class); |
| 417 | deviceState = true; |
| 418 | } else { |
| 419 | throw new NetconfException("Cannot create Connection and Session"); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | @Override |
| 424 | public boolean isActive() { |
| 425 | return deviceState; |
| 426 | } |
| 427 | |
| 428 | @Override |
| 429 | public NetconfSession getSession() { |
| 430 | return netconfSession; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public void disconnect() { |
| 435 | deviceState = false; |
| 436 | netconfSession = null; |
| 437 | } |
| 438 | |
| 439 | @Override |
| 440 | public NetconfDeviceInfo getDeviceInfo() { |
| 441 | return netconfDeviceInfo; |
| 442 | } |
| 443 | |
| 444 | } |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 445 | |
| 446 | private class MockComponentContext extends ComponentContextAdapter { |
| 447 | @Override |
| 448 | public Dictionary getProperties() { |
| 449 | return new MockDictionary(); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | private class MockDictionary extends Dictionary { |
| 454 | |
| 455 | @Override |
| 456 | public int size() { |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | @Override |
| 461 | public boolean isEmpty() { |
| 462 | return false; |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public Enumeration keys() { |
| 467 | return null; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public Enumeration elements() { |
| 472 | return null; |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public Object get(Object key) { |
Sean Condon | 334ad69 | 2016-12-13 17:56:56 +0000 | [diff] [blame] | 477 | if (key.equals("netconfConnectTimeout")) { |
| 478 | return "2"; |
| 479 | } else if (key.equals("netconfReplyTimeout")) { |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 480 | return "1"; |
Andrea Campanella | 7bbe7b1 | 2017-05-03 16:03:38 -0700 | [diff] [blame] | 481 | } else if (key.equals("sshLibrary")) { |
Yuta HIGUCHI | 5233dec | 2018-05-02 15:22:37 -0700 | [diff] [blame] | 482 | return NetconfSshClientLib.APACHE_MINA.toString(); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 483 | } |
| 484 | return null; |
| 485 | } |
| 486 | |
| 487 | @Override |
| 488 | public Object put(Object key, Object value) { |
| 489 | return null; |
| 490 | } |
| 491 | |
| 492 | @Override |
| 493 | public Object remove(Object key) { |
| 494 | return null; |
| 495 | } |
| 496 | } |
Sean Condon | 54d8243 | 2017-07-26 22:27:25 +0100 | [diff] [blame] | 497 | |
| 498 | private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter { |
| 499 | NetconfDeviceConfig cfg = null; |
| 500 | |
| 501 | @Override |
| 502 | public void registerConfigFactory(ConfigFactory configFactory) { |
| 503 | cfgFactories.add(configFactory); |
| 504 | } |
| 505 | |
| 506 | @Override |
| 507 | public void unregisterConfigFactory(ConfigFactory configFactory) { |
| 508 | cfgFactories.remove(configFactory); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public void addListener(NetworkConfigListener listener) { |
| 513 | netCfgListeners.add(listener); |
| 514 | } |
| 515 | |
| 516 | @Override |
| 517 | public void removeListener(NetworkConfigListener listener) { |
| 518 | netCfgListeners.remove(listener); |
| 519 | } |
| 520 | |
| 521 | |
| 522 | @Override |
| 523 | public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) { |
| 524 | DeviceId did = (DeviceId) subject; |
| 525 | if (configClass.equals(NetconfDeviceConfig.class) |
| 526 | && did.equals(deviceConfig10Id)) { |
| 527 | return (C) deviceConfig10; |
| 528 | } |
| 529 | return null; |
| 530 | } |
| 531 | |
| 532 | } |
| 533 | |
| 534 | private class MockDelegate implements ConfigApplyDelegate { |
| 535 | @Override |
| 536 | public void onApply(Config configFile) { |
| 537 | } |
| 538 | } |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 539 | } |