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