blob: d420db81a82be26813ef66778969acbe3539c69f [file] [log] [blame]
Thanuj Ravindranath926e3162016-01-26 09:44:08 +05301/*
gyewan.an3c99ee72019-02-18 15:53:55 +09002 * Copyright 2019-present Open Networking Foundation
Thanuj Ravindranath926e3162016-01-26 09:44:08 +05303 *
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 HIGUCHIe3ae8212017-04-20 10:18:41 -070016package org.onosproject.netconf.ctl.impl;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053017
Sean Condon54d82432017-07-26 22:27:25 +010018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -070020
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030024import org.onlab.osgi.ComponentContextAdapter;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053025import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030026import org.onosproject.cfg.ComponentConfigAdapter;
27import org.onosproject.cfg.ComponentConfigService;
gyewan.an3c99ee72019-02-18 15:53:55 +090028import org.onosproject.cluster.ClusterService;
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +010029import org.onosproject.mastership.MastershipService;
30import org.onosproject.mastership.MastershipServiceAdapter;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053031import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010032import org.onosproject.net.config.Config;
33import org.onosproject.net.config.ConfigApplyDelegate;
34import org.onosproject.net.config.ConfigFactory;
35import org.onosproject.net.config.NetworkConfigListener;
36import org.onosproject.net.config.NetworkConfigRegistry;
37import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070038import org.onosproject.net.device.DeviceService;
39import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053040import org.onosproject.netconf.NetconfDevice;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053041import org.onosproject.netconf.NetconfDeviceInfo;
42import org.onosproject.netconf.NetconfDeviceListener;
43import org.onosproject.netconf.NetconfDeviceOutputEvent;
44import org.onosproject.netconf.NetconfDeviceOutputEventListener;
45import org.onosproject.netconf.NetconfException;
46import org.onosproject.netconf.NetconfSession;
Sean Condon54d82432017-07-26 22:27:25 +010047import org.onosproject.netconf.config.NetconfDeviceConfig;
48import org.onosproject.netconf.config.NetconfSshClientLib;
gyewan.an91d7e7e2019-01-17 15:12:48 +090049import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
50import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030051import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053052
Sean Condon54d82432017-07-26 22:27:25 +010053import java.io.ByteArrayInputStream;
54import java.io.InputStream;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053055import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030056import java.util.Dictionary;
57import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080058import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053059import java.util.Map;
60import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080061import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053062
gyewan.an3c99ee72019-02-18 15:53:55 +090063import static org.easymock.EasyMock.createMock;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053064import static org.hamcrest.Matchers.*;
65import static org.junit.Assert.*;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070066import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_CONNECT_TIMEOUT_DEFAULT;
67import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_IDLE_TIMEOUT_DEFAULT;
68import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053069
70/**
71 * Unit tests for the Netconf controller implementation test.
72 */
73public class NetconfControllerImplTest {
Sean Condon54d82432017-07-26 22:27:25 +010074 private final Set<ConfigFactory> cfgFactories = new HashSet<>();
75 private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
76 private boolean available = false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070077
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053078 NetconfControllerImpl ctrl;
79
80 //DeviceInfo
81 NetconfDeviceInfo deviceInfo1;
82 NetconfDeviceInfo deviceInfo2;
83 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070084 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053085
Sean Condon54d82432017-07-26 22:27:25 +010086 NetconfDeviceConfig deviceConfig10;
87 DeviceId deviceConfig10Id;
88
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053089 //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 Condon54d82432017-07-26 22:27:25 +0100106 private static final String DEVICE_10_IP = "10.10.10.10";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700107 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530108
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 Campanella7e6200a2016-03-21 09:48:40 -0700112 private static final int IPV6_DEVICE_PORT = 14;
Sean Condon54d82432017-07-26 22:27:25 +0100113 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 Campanella7e6200a2016-03-21 09:48:40 -0700120
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300121 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700122 private static DeviceService deviceService = new NetconfDeviceServiceMock();
123 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Sean Condon54d82432017-07-26 22:27:25 +0100124 private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100125 private final MastershipService mastershipService = new MockmastershipService();
gyewan.an91d7e7e2019-01-17 15:12:48 +0900126 private final ClusterCommunicationService clusterCommunicationService =
127 new ClusterCommunicationServiceMock();
gyewan.an3c99ee72019-02-18 15:53:55 +0900128 private final ClusterService mockClusterService = createMock(ClusterService.class);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530129
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300130 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530131
132 @Before
133 public void setUp() throws Exception {
134 ctrl = new NetconfControllerImpl();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700135 ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300136 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700137 ctrl.deviceService = deviceService;
138 ctrl.deviceKeyService = deviceKeyService;
Sean Condon54d82432017-07-26 22:27:25 +0100139 ctrl.netCfgService = netCfgService;
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100140 ctrl.mastershipService = mastershipService;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700141 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
142 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
143 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
gyewan.an91d7e7e2019-01-17 15:12:48 +0900144 ctrl.clusterCommunicator = clusterCommunicationService;
gyewan.an3c99ee72019-02-18 15:53:55 +0900145 ctrl.clusterService = mockClusterService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530146
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 HIGUCHI5233dec2018-05-02 15:22:37 -0700150 deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530151 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700152 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530153
Sean Condon54d82432017-07-26 22:27:25 +0100154 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 HIGUCHI5233dec2018-05-02 15:22:37 -0700165 " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" +
Sean Condon54d82432017-07-26 22:27:25 +0100166 "}";
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 Ravindranath926e3162016-01-26 09:44:08 +0530174 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 Campanella7e6200a2016-03-21 09:48:40 -0700192 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530193 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700194 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530195 }
196
197 @After
198 public void tearDown() {
199 ctrl.deactivate();
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700200 // resetting static variables..
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700201 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
202 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
203 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530204 }
205
206 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300207 * Test initialization of component configuration.
208 */
209 @Test
210 public void testActivate() {
Sean Condon334ad692016-12-13 17:56:56 +0000211 assertEquals("Incorrect NetConf connect timeout, should be default",
212 5, ctrl.netconfConnectTimeout);
213 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700214 5, ctrl.netconfReplyTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300215 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000216 assertEquals("Incorrect NetConf connect timeout, should be default",
217 5, ctrl.netconfConnectTimeout);
218 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700219 5, ctrl.netconfReplyTimeout);
220 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300221
222 /**
223 * Test modification of component configuration.
224 */
225 @Test
226 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000227 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700228 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300229 assertEquals("Incorrect NetConf session timeout, should be default",
230 5, ctrl.netconfReplyTimeout);
231 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000232 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700233 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300234 assertEquals("Incorrect NetConf session timeout",
235 1, ctrl.netconfReplyTimeout);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700236 assertEquals(NetconfSshClientLib.APACHE_MINA.toString(), ctrl.sshLibrary.toString());
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300237 }
238
239 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530240 * 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.an3c99ee72019-02-18 15:53:55 +0900246 NetconfDeviceListener deviceListener1 = createMock(NetconfDeviceListener.class);
247 NetconfDeviceListener deviceListener2 = createMock(NetconfDeviceListener.class);
248 NetconfDeviceListener deviceListener3 = createMock(NetconfDeviceListener.class);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530249
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 Campanella7e6200a2016-03-21 09:48:40 -0700255 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530256
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 Campanella86294db2016-03-07 11:42:49 -0800263 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 Ravindranath926e3162016-01-26 09:44:08 +0530271 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 Campanella7e6200a2016-03-21 09:48:40 -0700295 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530296 } finally {
297 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
298 }
299 }
300
301 /**
Sean Condon54d82432017-07-26 22:27:25 +0100302 * 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 HIGUCHI5233dec2018-05-02 15:22:37 -0700326 NetconfSshClientLib.APACHE_MINA);
Sean Condon54d82432017-07-26 22:27:25 +0100327 }
328
329 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530330 * 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 Condon54d82432017-07-26 22:27:25 +0100335 NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
336 NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
David K. Bainbridge56e90232018-12-18 23:25:08 -0800337 assertTrue(String.format("Incorrect device connection from '%s' we get '%s' contains '%s'",
338 deviceInfo1, ctrl.getDevicesMap(), deviceId1),
339 ctrl.getDevicesMap().containsKey(deviceId1));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530340 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
341 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
342 }
343
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700344 /**
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 Ravindranath926e3162016-01-26 09:44:08 +0530356
357 /**
358 * Check for connect devices already added to the map.
359 */
360 @Test
361 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700362 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
363 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530364 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700365 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530366 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700367 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530368 }
369
370 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800371 * Check that disconnectDevice actually disconnects the device and removes it.
372 */
373 @Test
374 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700375 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800376 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
377 }
378
379 /**
380 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530381 */
382 @Test
383 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700384 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530385 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 Campanella7e6200a2016-03-21 09:48:40 -0700403 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530404 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 Campanella7e6200a2016-03-21 09:48:40 -0700409 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530410 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 Ravindranath926e3162016-01-26 09:44:08 +0530418 * 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 Campanella7e6200a2016-03-21 09:48:40 -0700427 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
gyewan.an3c99ee72019-02-18 15:53:55 +0900428 netconfSession = createMock(NetconfSession.class);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530429 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 Papazois4752cfa2016-04-25 14:52:12 +0300457
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 Condon334ad692016-12-13 17:56:56 +0000489 if (key.equals("netconfConnectTimeout")) {
490 return "2";
491 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300492 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700493 } else if (key.equals("sshLibrary")) {
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700494 return NetconfSshClientLib.APACHE_MINA.toString();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300495 }
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 Condon54d82432017-07-26 22:27:25 +0100509
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 Campanellaa2a6c3c2018-12-11 12:56:38 +0100551
552 private class MockmastershipService extends MastershipServiceAdapter {
553 @Override
554 public boolean isLocalMaster(DeviceId deviceId) {
555 return true;
556 }
557 }
gyewan.an91d7e7e2019-01-17 15:12:48 +0900558 private class ClusterCommunicationServiceMock extends ClusterCommunicationServiceAdapter {
559 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530560}