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