blob: 44011f1202ee8b7ccb499809446244a870d8f0c3 [file] [log] [blame]
Thanuj Ravindranath926e3162016-01-26 09:44:08 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-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.easymock.EasyMock;
22import org.junit.After;
23import org.junit.Before;
24import org.junit.Test;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030025import org.onlab.osgi.ComponentContextAdapter;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053026import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030027import org.onosproject.cfg.ComponentConfigAdapter;
28import org.onosproject.cfg.ComponentConfigService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053029import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010030import org.onosproject.net.config.Config;
31import org.onosproject.net.config.ConfigApplyDelegate;
32import org.onosproject.net.config.ConfigFactory;
33import org.onosproject.net.config.NetworkConfigListener;
34import org.onosproject.net.config.NetworkConfigRegistry;
35import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070036import org.onosproject.net.device.DeviceService;
37import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053038import org.onosproject.netconf.NetconfDevice;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053039import org.onosproject.netconf.NetconfDeviceInfo;
40import org.onosproject.netconf.NetconfDeviceListener;
41import org.onosproject.netconf.NetconfDeviceOutputEvent;
42import org.onosproject.netconf.NetconfDeviceOutputEventListener;
43import org.onosproject.netconf.NetconfException;
44import org.onosproject.netconf.NetconfSession;
Sean Condon54d82432017-07-26 22:27:25 +010045import org.onosproject.netconf.config.NetconfDeviceConfig;
46import org.onosproject.netconf.config.NetconfSshClientLib;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030047import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053048
Sean Condon54d82432017-07-26 22:27:25 +010049import java.io.ByteArrayInputStream;
50import java.io.InputStream;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053051import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030052import java.util.Dictionary;
53import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080054import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053055import java.util.Map;
56import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080057import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053058
59import static org.hamcrest.Matchers.*;
60import static org.junit.Assert.*;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070061import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_CONNECT_TIMEOUT_DEFAULT;
62import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_IDLE_TIMEOUT_DEFAULT;
63import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053064
65/**
66 * Unit tests for the Netconf controller implementation test.
67 */
68public class NetconfControllerImplTest {
Sean Condon54d82432017-07-26 22:27:25 +010069 private final Set<ConfigFactory> cfgFactories = new HashSet<>();
70 private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
71 private boolean available = false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070072
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053073 NetconfControllerImpl ctrl;
74
75 //DeviceInfo
76 NetconfDeviceInfo deviceInfo1;
77 NetconfDeviceInfo deviceInfo2;
78 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070079 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053080
Sean Condon54d82432017-07-26 22:27:25 +010081 NetconfDeviceConfig deviceConfig10;
82 DeviceId deviceConfig10Id;
83
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053084 //Devices & DeviceId
85 NetconfDevice device1;
86 DeviceId deviceId1;
87 NetconfDevice device2;
88 DeviceId deviceId2;
89
90 //Events
91 NetconfDeviceOutputEvent eventForDeviceInfo1;
92 NetconfDeviceOutputEvent eventForDeviceInfo2;
93
94 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
95 private NetconfDeviceOutputEventListener reflectedDownListener;
96
97 //Test Device IP addresses and ports
98 private static final String DEVICE_1_IP = "10.10.10.11";
99 private static final String DEVICE_2_IP = "10.10.10.12";
100 private static final String BAD_DEVICE_IP = "10.10.10.13";
Sean Condon54d82432017-07-26 22:27:25 +0100101 private static final String DEVICE_10_IP = "10.10.10.10";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700102 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530103
104 private static final int DEVICE_1_PORT = 11;
105 private static final int DEVICE_2_PORT = 12;
106 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700107 private static final int IPV6_DEVICE_PORT = 14;
Sean Condon54d82432017-07-26 22:27:25 +0100108 private static final int DEVICE_10_PORT = 10;
109
110 private static final String DEVICE_10_USERNAME = "device10";
111 private static final String DEVICE_10_PASSWORD = "010";
112 private static final int DEVICE_10_CONNECT_TIMEOUT = 10;
113 private static final int DEVICE_10_REPLY_TIMEOUT = 11;
114 private static final int DEVICE_10_IDLE_TIMEOUT = 12;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700115
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300116 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700117 private static DeviceService deviceService = new NetconfDeviceServiceMock();
118 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Sean Condon54d82432017-07-26 22:27:25 +0100119 private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530120
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300121 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530122
123 @Before
124 public void setUp() throws Exception {
125 ctrl = new NetconfControllerImpl();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700126 ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300127 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700128 ctrl.deviceService = deviceService;
129 ctrl.deviceKeyService = deviceKeyService;
Sean Condon54d82432017-07-26 22:27:25 +0100130 ctrl.netCfgService = netCfgService;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700131 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
132 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
133 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530134
135 //Creating mock devices
136 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
137 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700138 deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530139 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700140 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530141
Sean Condon54d82432017-07-26 22:27:25 +0100142 deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
143 //Create a JSON entry just like Network Config accepts
144 ObjectMapper mapper = new ObjectMapper();
145 String jsonMessage = "{\n" +
146 " \"ip\":\"" + DEVICE_10_IP + "\",\n" +
147 " \"port\":" + DEVICE_10_PORT + ",\n" +
148 " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" +
149 " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" +
150 " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" +
151 " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" +
152 " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" +
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700153 " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" +
Sean Condon54d82432017-07-26 22:27:25 +0100154 "}";
155 InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
156 JsonNode jsonNode = mapper.readTree(jsonStream);
157 jsonStream.close();
158 ConfigApplyDelegate delegate = new MockDelegate();
159 deviceConfig10 = new NetconfDeviceConfig();
160 deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
161
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530162 device1 = new TestNetconfDevice(deviceInfo1);
163 deviceId1 = deviceInfo1.getDeviceId();
164 device2 = new TestNetconfDevice(deviceInfo2);
165 deviceId2 = deviceInfo2.getDeviceId();
166
167 //Adding to the map for testing get device calls.
168 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
169 field1.setAccessible(true);
170 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
171 reflectedDeviceMap.put(deviceId1, device1);
172 reflectedDeviceMap.put(deviceId2, device2);
173
174 //Creating mock events for testing NetconfDeviceOutputEventListener
175 Field field2 = ctrl.getClass().getDeclaredField("downListener");
176 field2.setAccessible(true);
177 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
178
179 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700180 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530181 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700182 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530183 }
184
185 @After
186 public void tearDown() {
187 ctrl.deactivate();
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700188 // resetting static variables..
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700189 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
190 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
191 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530192 }
193
194 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300195 * Test initialization of component configuration.
196 */
197 @Test
198 public void testActivate() {
Sean Condon334ad692016-12-13 17:56:56 +0000199 assertEquals("Incorrect NetConf connect timeout, should be default",
200 5, ctrl.netconfConnectTimeout);
201 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700202 5, ctrl.netconfReplyTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300203 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000204 assertEquals("Incorrect NetConf connect timeout, should be default",
205 5, ctrl.netconfConnectTimeout);
206 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700207 5, ctrl.netconfReplyTimeout);
208 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300209
210 /**
211 * Test modification of component configuration.
212 */
213 @Test
214 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000215 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700216 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300217 assertEquals("Incorrect NetConf session timeout, should be default",
218 5, ctrl.netconfReplyTimeout);
219 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000220 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700221 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300222 assertEquals("Incorrect NetConf session timeout",
223 1, ctrl.netconfReplyTimeout);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700224 assertEquals(NetconfSshClientLib.APACHE_MINA.toString(), ctrl.sshLibrary.toString());
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300225 }
226
227 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530228 * Test to add DeviceListeners,
229 * and also to check whether the netconfDeviceListeners set is
230 * updating or not which was present in NetconfControllerImpl class.
231 */
232 @Test
233 public void testAddRemoveDeviceListener() {
234 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
235 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
236 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
237
238 ctrl.addDeviceListener(deviceListener1);
239 ctrl.addDeviceListener(deviceListener2);
240 ctrl.addDeviceListener(deviceListener3);
241 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
242 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700243 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530244
245 ctrl.removeDeviceListener(deviceListener1);
246 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
247 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
248 }
249
250 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800251 public void testGetNetconfDevices() {
252 Set<DeviceId> devices = new HashSet<>();
253 devices.add(deviceId1);
254 devices.add(deviceId2);
255 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
256 }
257
258 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530259 public void testGetNetconfDevice() {
260 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
261 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
262
263 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
264 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
265 }
266
267 @Test
268 public void testGetNetconfDeviceWithIPPort() {
269 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
270 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
271
272 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
273 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
274 }
275
276 /**
277 * Check for bad device connection. In this case the device map shouldn't get modified.
278 */
279 @Test(expected = NetconfException.class)
280 public void testConnectBadDevice() throws Exception {
281 reflectedDeviceMap.clear();
282 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700283 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530284 } finally {
285 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
286 }
287 }
288
289 /**
Sean Condon54d82432017-07-26 22:27:25 +0100290 * Check for connection by netconfDeviceConfig.
291 */
292 @Test
293 public void testConnectDeviceNetConfig10() throws Exception {
294 NetconfDevice fetchedDevice10 = ctrl.connectDevice(deviceConfig10Id);
295 assertEquals("Incorrect device fetched - ip",
296 fetchedDevice10.getDeviceInfo().ip().toString(), DEVICE_10_IP);
297 assertEquals("Incorrect device fetched - port",
298 fetchedDevice10.getDeviceInfo().port(), DEVICE_10_PORT);
299 assertEquals("Incorrect device fetched - username",
300 fetchedDevice10.getDeviceInfo().name(), DEVICE_10_USERNAME);
301 assertEquals("Incorrect device fetched - password",
302 fetchedDevice10.getDeviceInfo().password(), DEVICE_10_PASSWORD);
303 assertEquals("Incorrect device fetched - connectTimeout",
304 fetchedDevice10.getDeviceInfo().getConnectTimeoutSec().getAsInt(),
305 DEVICE_10_CONNECT_TIMEOUT);
306 assertEquals("Incorrect device fetched - replyTimeout",
307 fetchedDevice10.getDeviceInfo().getReplyTimeoutSec().getAsInt(),
308 DEVICE_10_REPLY_TIMEOUT);
309 assertEquals("Incorrect device fetched - idleTimeout",
310 fetchedDevice10.getDeviceInfo().getIdleTimeoutSec().getAsInt(),
311 DEVICE_10_IDLE_TIMEOUT);
312 assertEquals("Incorrect device fetched - sshClient",
313 fetchedDevice10.getDeviceInfo().sshClientLib().get(),
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700314 NetconfSshClientLib.APACHE_MINA);
Sean Condon54d82432017-07-26 22:27:25 +0100315 }
316
317 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530318 * Check for correct device connection. In this case the device map get modified.
319 */
320 @Test
321 public void testConnectCorrectDevice() throws Exception {
322 reflectedDeviceMap.clear();
Sean Condon54d82432017-07-26 22:27:25 +0100323 NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
324 NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530325 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
326 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
327 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
328 }
329
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700330 /**
331 * Check for correct ipv6 device connection. In this case the device map get modified.
332 */
333 @Test
334 public void testConnectCorrectIpv6Device() throws Exception {
335 reflectedDeviceMap.clear();
336 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
337 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
338 .containsKey(deviceInfoIpV6.getDeviceId()));
339 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
340 }
341
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530342
343 /**
344 * Check for connect devices already added to the map.
345 */
346 @Test
347 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700348 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
349 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530350 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700351 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530352 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700353 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530354 }
355
356 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800357 * Check that disconnectDevice actually disconnects the device and removes it.
358 */
359 @Test
360 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700361 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800362 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
363 }
364
365 /**
366 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530367 */
368 @Test
369 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700370 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530371 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
372 }
373
374 /**
375 * Test to get the connected device map.
376 */
377 @Test
378 public void testGetDevicesMap() {
379 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
380 }
381
382 /**
383 * Test to check whether the DeviceDownEventListener removes the device from the map when session
384 * for a particular device getting closed.
385 */
386 @Test
387 public void testDeviceDownEventListener() throws Exception {
388 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700389 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530390 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
391 assertFalse("Irrelevant Device Event", result1);
392 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
393 reflectedDownListener.event(eventForDeviceInfo1);
394 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700395 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530396 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
397 assertTrue("Irrelevant Device Event", result2);
398 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
399 reflectedDownListener.event(eventForDeviceInfo2);
400 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
401 }
402
403 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530404 * Mock NetconfDeviceImpl class, used for creating test devices.
405 */
406 protected class TestNetconfDevice implements NetconfDevice {
407 private NetconfDeviceInfo netconfDeviceInfo;
408 private boolean deviceState = false;
409 private NetconfSession netconfSession;
410
411 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
412 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700413 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530414 netconfSession = EasyMock.createMock(NetconfSession.class);
415 deviceState = true;
416 } else {
417 throw new NetconfException("Cannot create Connection and Session");
418 }
419 }
420
421 @Override
422 public boolean isActive() {
423 return deviceState;
424 }
425
426 @Override
427 public NetconfSession getSession() {
428 return netconfSession;
429 }
430
431 @Override
432 public void disconnect() {
433 deviceState = false;
434 netconfSession = null;
435 }
436
437 @Override
438 public NetconfDeviceInfo getDeviceInfo() {
439 return netconfDeviceInfo;
440 }
441
442 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300443
444 private class MockComponentContext extends ComponentContextAdapter {
445 @Override
446 public Dictionary getProperties() {
447 return new MockDictionary();
448 }
449 }
450
451 private class MockDictionary extends Dictionary {
452
453 @Override
454 public int size() {
455 return 0;
456 }
457
458 @Override
459 public boolean isEmpty() {
460 return false;
461 }
462
463 @Override
464 public Enumeration keys() {
465 return null;
466 }
467
468 @Override
469 public Enumeration elements() {
470 return null;
471 }
472
473 @Override
474 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000475 if (key.equals("netconfConnectTimeout")) {
476 return "2";
477 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300478 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700479 } else if (key.equals("sshLibrary")) {
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700480 return NetconfSshClientLib.APACHE_MINA.toString();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300481 }
482 return null;
483 }
484
485 @Override
486 public Object put(Object key, Object value) {
487 return null;
488 }
489
490 @Override
491 public Object remove(Object key) {
492 return null;
493 }
494 }
Sean Condon54d82432017-07-26 22:27:25 +0100495
496 private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
497 NetconfDeviceConfig cfg = null;
498
499 @Override
500 public void registerConfigFactory(ConfigFactory configFactory) {
501 cfgFactories.add(configFactory);
502 }
503
504 @Override
505 public void unregisterConfigFactory(ConfigFactory configFactory) {
506 cfgFactories.remove(configFactory);
507 }
508
509 @Override
510 public void addListener(NetworkConfigListener listener) {
511 netCfgListeners.add(listener);
512 }
513
514 @Override
515 public void removeListener(NetworkConfigListener listener) {
516 netCfgListeners.remove(listener);
517 }
518
519
520 @Override
521 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
522 DeviceId did = (DeviceId) subject;
523 if (configClass.equals(NetconfDeviceConfig.class)
524 && did.equals(deviceConfig10Id)) {
525 return (C) deviceConfig10;
526 }
527 return null;
528 }
529
530 }
531
532 private class MockDelegate implements ConfigApplyDelegate {
533 @Override
534 public void onApply(Config configFile) {
535 }
536 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530537}