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