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