blob: e50358476b0803e88b11cfc0c953d70d9badf8fb [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());
David K. Bainbridge56e90232018-12-18 23:25:08 -0800325 assertTrue(String.format("Incorrect device connection from '%s' we get '%s' contains '%s'",
326 deviceInfo1, ctrl.getDevicesMap(), deviceId1),
327 ctrl.getDevicesMap().containsKey(deviceId1));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530328 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
329 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
330 }
331
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700332 /**
333 * Check for correct ipv6 device connection. In this case the device map get modified.
334 */
335 @Test
336 public void testConnectCorrectIpv6Device() throws Exception {
337 reflectedDeviceMap.clear();
338 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
339 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
340 .containsKey(deviceInfoIpV6.getDeviceId()));
341 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
342 }
343
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530344
345 /**
346 * Check for connect devices already added to the map.
347 */
348 @Test
349 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700350 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
351 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530352 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700353 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530354 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700355 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530356 }
357
358 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800359 * Check that disconnectDevice actually disconnects the device and removes it.
360 */
361 @Test
362 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700363 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800364 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
365 }
366
367 /**
368 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530369 */
370 @Test
371 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700372 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530373 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
374 }
375
376 /**
377 * Test to get the connected device map.
378 */
379 @Test
380 public void testGetDevicesMap() {
381 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
382 }
383
384 /**
385 * Test to check whether the DeviceDownEventListener removes the device from the map when session
386 * for a particular device getting closed.
387 */
388 @Test
389 public void testDeviceDownEventListener() throws Exception {
390 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700391 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530392 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
393 assertFalse("Irrelevant Device Event", result1);
394 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
395 reflectedDownListener.event(eventForDeviceInfo1);
396 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700397 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530398 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
399 assertTrue("Irrelevant Device Event", result2);
400 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
401 reflectedDownListener.event(eventForDeviceInfo2);
402 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
403 }
404
405 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530406 * Mock NetconfDeviceImpl class, used for creating test devices.
407 */
408 protected class TestNetconfDevice implements NetconfDevice {
409 private NetconfDeviceInfo netconfDeviceInfo;
410 private boolean deviceState = false;
411 private NetconfSession netconfSession;
412
413 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
414 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700415 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530416 netconfSession = EasyMock.createMock(NetconfSession.class);
417 deviceState = true;
418 } else {
419 throw new NetconfException("Cannot create Connection and Session");
420 }
421 }
422
423 @Override
424 public boolean isActive() {
425 return deviceState;
426 }
427
428 @Override
429 public NetconfSession getSession() {
430 return netconfSession;
431 }
432
433 @Override
434 public void disconnect() {
435 deviceState = false;
436 netconfSession = null;
437 }
438
439 @Override
440 public NetconfDeviceInfo getDeviceInfo() {
441 return netconfDeviceInfo;
442 }
443
444 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300445
446 private class MockComponentContext extends ComponentContextAdapter {
447 @Override
448 public Dictionary getProperties() {
449 return new MockDictionary();
450 }
451 }
452
453 private class MockDictionary extends Dictionary {
454
455 @Override
456 public int size() {
457 return 0;
458 }
459
460 @Override
461 public boolean isEmpty() {
462 return false;
463 }
464
465 @Override
466 public Enumeration keys() {
467 return null;
468 }
469
470 @Override
471 public Enumeration elements() {
472 return null;
473 }
474
475 @Override
476 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000477 if (key.equals("netconfConnectTimeout")) {
478 return "2";
479 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300480 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700481 } else if (key.equals("sshLibrary")) {
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700482 return NetconfSshClientLib.APACHE_MINA.toString();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300483 }
484 return null;
485 }
486
487 @Override
488 public Object put(Object key, Object value) {
489 return null;
490 }
491
492 @Override
493 public Object remove(Object key) {
494 return null;
495 }
496 }
Sean Condon54d82432017-07-26 22:27:25 +0100497
498 private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
499 NetconfDeviceConfig cfg = null;
500
501 @Override
502 public void registerConfigFactory(ConfigFactory configFactory) {
503 cfgFactories.add(configFactory);
504 }
505
506 @Override
507 public void unregisterConfigFactory(ConfigFactory configFactory) {
508 cfgFactories.remove(configFactory);
509 }
510
511 @Override
512 public void addListener(NetworkConfigListener listener) {
513 netCfgListeners.add(listener);
514 }
515
516 @Override
517 public void removeListener(NetworkConfigListener listener) {
518 netCfgListeners.remove(listener);
519 }
520
521
522 @Override
523 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
524 DeviceId did = (DeviceId) subject;
525 if (configClass.equals(NetconfDeviceConfig.class)
526 && did.equals(deviceConfig10Id)) {
527 return (C) deviceConfig10;
528 }
529 return null;
530 }
531
532 }
533
534 private class MockDelegate implements ConfigApplyDelegate {
535 @Override
536 public void onApply(Config configFile) {
537 }
538 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530539}