blob: d77c222688a366fd9a39662cfde390da93246a2e [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;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053020import org.easymock.EasyMock;
21import 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;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053028import org.onosproject.net.DeviceId;
Sean Condon54d82432017-07-26 22:27:25 +010029import org.onosproject.net.config.Config;
30import org.onosproject.net.config.ConfigApplyDelegate;
31import org.onosproject.net.config.ConfigFactory;
32import org.onosproject.net.config.NetworkConfigListener;
33import org.onosproject.net.config.NetworkConfigRegistry;
34import org.onosproject.net.config.NetworkConfigRegistryAdapter;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070035import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053037import org.onosproject.netconf.NetconfDevice;
38import org.onosproject.netconf.NetconfDeviceFactory;
39import 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.*;
61
62/**
63 * Unit tests for the Netconf controller implementation test.
64 */
65public class NetconfControllerImplTest {
Sean Condon54d82432017-07-26 22:27:25 +010066 private final Set<ConfigFactory> cfgFactories = new HashSet<>();
67 private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
68 private boolean available = false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070069
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053070 NetconfControllerImpl ctrl;
71
72 //DeviceInfo
73 NetconfDeviceInfo deviceInfo1;
74 NetconfDeviceInfo deviceInfo2;
75 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070076 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053077
Sean Condon54d82432017-07-26 22:27:25 +010078 NetconfDeviceConfig deviceConfig10;
79 DeviceId deviceConfig10Id;
80
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053081 //Devices & DeviceId
82 NetconfDevice device1;
83 DeviceId deviceId1;
84 NetconfDevice device2;
85 DeviceId deviceId2;
86
87 //Events
88 NetconfDeviceOutputEvent eventForDeviceInfo1;
89 NetconfDeviceOutputEvent eventForDeviceInfo2;
90
91 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
92 private NetconfDeviceOutputEventListener reflectedDownListener;
93
94 //Test Device IP addresses and ports
95 private static final String DEVICE_1_IP = "10.10.10.11";
96 private static final String DEVICE_2_IP = "10.10.10.12";
97 private static final String BAD_DEVICE_IP = "10.10.10.13";
Sean Condon54d82432017-07-26 22:27:25 +010098 private static final String DEVICE_10_IP = "10.10.10.10";
Andrea Campanella7e6200a2016-03-21 09:48:40 -070099 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530100
101 private static final int DEVICE_1_PORT = 11;
102 private static final int DEVICE_2_PORT = 12;
103 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700104 private static final int IPV6_DEVICE_PORT = 14;
Sean Condon54d82432017-07-26 22:27:25 +0100105 private static final int DEVICE_10_PORT = 10;
106
107 private static final String DEVICE_10_USERNAME = "device10";
108 private static final String DEVICE_10_PASSWORD = "010";
109 private static final int DEVICE_10_CONNECT_TIMEOUT = 10;
110 private static final int DEVICE_10_REPLY_TIMEOUT = 11;
111 private static final int DEVICE_10_IDLE_TIMEOUT = 12;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700112
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300113 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700114 private static DeviceService deviceService = new NetconfDeviceServiceMock();
115 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Sean Condon54d82432017-07-26 22:27:25 +0100116 private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530117
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300118 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530119
120 @Before
121 public void setUp() throws Exception {
122 ctrl = new NetconfControllerImpl();
123 ctrl.deviceFactory = new TestNetconfDeviceFactory();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300124 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700125 ctrl.deviceService = deviceService;
126 ctrl.deviceKeyService = deviceKeyService;
Sean Condon54d82432017-07-26 22:27:25 +0100127 ctrl.netCfgService = netCfgService;
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700128 NetconfControllerImpl.netconfConnectTimeout = NetconfControllerImpl.DEFAULT_CONNECT_TIMEOUT_SECONDS;
129 NetconfControllerImpl.netconfIdleTimeout = NetconfControllerImpl.DEFAULT_IDLE_TIMEOUT_SECONDS;
130 NetconfControllerImpl.netconfReplyTimeout = NetconfControllerImpl.DEFAULT_REPLY_TIMEOUT_SECONDS;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530131
132 //Creating mock devices
133 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
134 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
Sean Condon54d82432017-07-26 22:27:25 +0100135 deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.ETHZ_SSH2));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530136 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700137 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530138
Sean Condon54d82432017-07-26 22:27:25 +0100139 deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
140 //Create a JSON entry just like Network Config accepts
141 ObjectMapper mapper = new ObjectMapper();
142 String jsonMessage = "{\n" +
143 " \"ip\":\"" + DEVICE_10_IP + "\",\n" +
144 " \"port\":" + DEVICE_10_PORT + ",\n" +
145 " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" +
146 " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" +
147 " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" +
148 " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" +
149 " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" +
150 " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.ETHZ_SSH2.toString() + "\"\n" +
151 "}";
152 InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
153 JsonNode jsonNode = mapper.readTree(jsonStream);
154 jsonStream.close();
155 ConfigApplyDelegate delegate = new MockDelegate();
156 deviceConfig10 = new NetconfDeviceConfig();
157 deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
158
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530159 device1 = new TestNetconfDevice(deviceInfo1);
160 deviceId1 = deviceInfo1.getDeviceId();
161 device2 = new TestNetconfDevice(deviceInfo2);
162 deviceId2 = deviceInfo2.getDeviceId();
163
164 //Adding to the map for testing get device calls.
165 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
166 field1.setAccessible(true);
167 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
168 reflectedDeviceMap.put(deviceId1, device1);
169 reflectedDeviceMap.put(deviceId2, device2);
170
171 //Creating mock events for testing NetconfDeviceOutputEventListener
172 Field field2 = ctrl.getClass().getDeclaredField("downListener");
173 field2.setAccessible(true);
174 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
175
176 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700177 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530178 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700179 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530180 }
181
182 @After
183 public void tearDown() {
184 ctrl.deactivate();
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700185 // resetting static variables..
186 NetconfControllerImpl.netconfConnectTimeout = NetconfControllerImpl.DEFAULT_CONNECT_TIMEOUT_SECONDS;
187 NetconfControllerImpl.netconfIdleTimeout = NetconfControllerImpl.DEFAULT_IDLE_TIMEOUT_SECONDS;
188 NetconfControllerImpl.netconfReplyTimeout = NetconfControllerImpl.DEFAULT_REPLY_TIMEOUT_SECONDS;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530189 }
190
191 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300192 * Test initialization of component configuration.
193 */
194 @Test
195 public void testActivate() {
Sean Condon334ad692016-12-13 17:56:56 +0000196 assertEquals("Incorrect NetConf connect timeout, should be default",
197 5, ctrl.netconfConnectTimeout);
198 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700199 5, ctrl.netconfReplyTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300200 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000201 assertEquals("Incorrect NetConf connect timeout, should be default",
202 5, ctrl.netconfConnectTimeout);
203 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700204 5, ctrl.netconfReplyTimeout);
205 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300206
207 /**
208 * Test modification of component configuration.
209 */
210 @Test
211 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000212 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700213 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300214 assertEquals("Incorrect NetConf session timeout, should be default",
215 5, ctrl.netconfReplyTimeout);
216 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000217 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700218 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300219 assertEquals("Incorrect NetConf session timeout",
220 1, ctrl.netconfReplyTimeout);
Sean Condon54d82432017-07-26 22:27:25 +0100221 assertEquals("ethz-ssh2", ctrl.sshLibrary.toString());
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300222 }
223
224 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530225 * Test to add DeviceListeners,
226 * and also to check whether the netconfDeviceListeners set is
227 * updating or not which was present in NetconfControllerImpl class.
228 */
229 @Test
230 public void testAddRemoveDeviceListener() {
231 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
232 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
233 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
234
235 ctrl.addDeviceListener(deviceListener1);
236 ctrl.addDeviceListener(deviceListener2);
237 ctrl.addDeviceListener(deviceListener3);
238 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
239 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700240 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530241
242 ctrl.removeDeviceListener(deviceListener1);
243 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
244 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
245 }
246
247 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800248 public void testGetNetconfDevices() {
249 Set<DeviceId> devices = new HashSet<>();
250 devices.add(deviceId1);
251 devices.add(deviceId2);
252 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
253 }
254
255 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530256 public void testGetNetconfDevice() {
257 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
258 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
259
260 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
261 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
262 }
263
264 @Test
265 public void testGetNetconfDeviceWithIPPort() {
266 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
267 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
268
269 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
270 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
271 }
272
273 /**
274 * Check for bad device connection. In this case the device map shouldn't get modified.
275 */
276 @Test(expected = NetconfException.class)
277 public void testConnectBadDevice() throws Exception {
278 reflectedDeviceMap.clear();
279 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700280 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530281 } finally {
282 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
283 }
284 }
285
286 /**
Sean Condon54d82432017-07-26 22:27:25 +0100287 * Check for connection by netconfDeviceConfig.
288 */
289 @Test
290 public void testConnectDeviceNetConfig10() throws Exception {
291 NetconfDevice fetchedDevice10 = ctrl.connectDevice(deviceConfig10Id);
292 assertEquals("Incorrect device fetched - ip",
293 fetchedDevice10.getDeviceInfo().ip().toString(), DEVICE_10_IP);
294 assertEquals("Incorrect device fetched - port",
295 fetchedDevice10.getDeviceInfo().port(), DEVICE_10_PORT);
296 assertEquals("Incorrect device fetched - username",
297 fetchedDevice10.getDeviceInfo().name(), DEVICE_10_USERNAME);
298 assertEquals("Incorrect device fetched - password",
299 fetchedDevice10.getDeviceInfo().password(), DEVICE_10_PASSWORD);
300 assertEquals("Incorrect device fetched - connectTimeout",
301 fetchedDevice10.getDeviceInfo().getConnectTimeoutSec().getAsInt(),
302 DEVICE_10_CONNECT_TIMEOUT);
303 assertEquals("Incorrect device fetched - replyTimeout",
304 fetchedDevice10.getDeviceInfo().getReplyTimeoutSec().getAsInt(),
305 DEVICE_10_REPLY_TIMEOUT);
306 assertEquals("Incorrect device fetched - idleTimeout",
307 fetchedDevice10.getDeviceInfo().getIdleTimeoutSec().getAsInt(),
308 DEVICE_10_IDLE_TIMEOUT);
309 assertEquals("Incorrect device fetched - sshClient",
310 fetchedDevice10.getDeviceInfo().sshClientLib().get(),
311 NetconfSshClientLib.ETHZ_SSH2);
312 }
313
314 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530315 * Check for correct device connection. In this case the device map get modified.
316 */
317 @Test
318 public void testConnectCorrectDevice() throws Exception {
319 reflectedDeviceMap.clear();
Sean Condon54d82432017-07-26 22:27:25 +0100320 NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
321 NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530322 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
323 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
324 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
325 }
326
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700327 /**
328 * Check for correct ipv6 device connection. In this case the device map get modified.
329 */
330 @Test
331 public void testConnectCorrectIpv6Device() throws Exception {
332 reflectedDeviceMap.clear();
333 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
334 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
335 .containsKey(deviceInfoIpV6.getDeviceId()));
336 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
337 }
338
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530339
340 /**
341 * Check for connect devices already added to the map.
342 */
343 @Test
344 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700345 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
346 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530347 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700348 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530349 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700350 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530351 }
352
353 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800354 * Check that disconnectDevice actually disconnects the device and removes it.
355 */
356 @Test
357 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700358 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800359 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
360 }
361
362 /**
363 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530364 */
365 @Test
366 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700367 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530368 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
369 }
370
371 /**
372 * Test to get the connected device map.
373 */
374 @Test
375 public void testGetDevicesMap() {
376 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
377 }
378
379 /**
380 * Test to check whether the DeviceDownEventListener removes the device from the map when session
381 * for a particular device getting closed.
382 */
383 @Test
384 public void testDeviceDownEventListener() throws Exception {
385 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700386 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530387 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
388 assertFalse("Irrelevant Device Event", result1);
389 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
390 reflectedDownListener.event(eventForDeviceInfo1);
391 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700392 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530393 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
394 assertTrue("Irrelevant Device Event", result2);
395 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
396 reflectedDownListener.event(eventForDeviceInfo2);
397 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
398 }
399
400 /**
401 * Mock NetconfDeviceFactory class.
402 */
403 private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
404
405 @Override
406 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
407 return new TestNetconfDevice(netconfDeviceInfo);
408 }
409 }
410
411 /**
412 * Mock NetconfDeviceImpl class, used for creating test devices.
413 */
414 protected class TestNetconfDevice implements NetconfDevice {
415 private NetconfDeviceInfo netconfDeviceInfo;
416 private boolean deviceState = false;
417 private NetconfSession netconfSession;
418
419 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
420 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700421 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530422 netconfSession = EasyMock.createMock(NetconfSession.class);
423 deviceState = true;
424 } else {
425 throw new NetconfException("Cannot create Connection and Session");
426 }
427 }
428
429 @Override
430 public boolean isActive() {
431 return deviceState;
432 }
433
434 @Override
435 public NetconfSession getSession() {
436 return netconfSession;
437 }
438
439 @Override
440 public void disconnect() {
441 deviceState = false;
442 netconfSession = null;
443 }
444
445 @Override
446 public NetconfDeviceInfo getDeviceInfo() {
447 return netconfDeviceInfo;
448 }
449
450 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300451
452 private class MockComponentContext extends ComponentContextAdapter {
453 @Override
454 public Dictionary getProperties() {
455 return new MockDictionary();
456 }
457 }
458
459 private class MockDictionary extends Dictionary {
460
461 @Override
462 public int size() {
463 return 0;
464 }
465
466 @Override
467 public boolean isEmpty() {
468 return false;
469 }
470
471 @Override
472 public Enumeration keys() {
473 return null;
474 }
475
476 @Override
477 public Enumeration elements() {
478 return null;
479 }
480
481 @Override
482 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000483 if (key.equals("netconfConnectTimeout")) {
484 return "2";
485 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300486 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700487 } else if (key.equals("sshLibrary")) {
488 return "ethz-ssh2";
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300489 }
490 return null;
491 }
492
493 @Override
494 public Object put(Object key, Object value) {
495 return null;
496 }
497
498 @Override
499 public Object remove(Object key) {
500 return null;
501 }
502 }
Sean Condon54d82432017-07-26 22:27:25 +0100503
504 private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
505 NetconfDeviceConfig cfg = null;
506
507 @Override
508 public void registerConfigFactory(ConfigFactory configFactory) {
509 cfgFactories.add(configFactory);
510 }
511
512 @Override
513 public void unregisterConfigFactory(ConfigFactory configFactory) {
514 cfgFactories.remove(configFactory);
515 }
516
517 @Override
518 public void addListener(NetworkConfigListener listener) {
519 netCfgListeners.add(listener);
520 }
521
522 @Override
523 public void removeListener(NetworkConfigListener listener) {
524 netCfgListeners.remove(listener);
525 }
526
527
528 @Override
529 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
530 DeviceId did = (DeviceId) subject;
531 if (configClass.equals(NetconfDeviceConfig.class)
532 && did.equals(deviceConfig10Id)) {
533 return (C) deviceConfig10;
534 }
535 return null;
536 }
537
538 }
539
540 private class MockDelegate implements ConfigApplyDelegate {
541 @Override
542 public void onApply(Config configFile) {
543 }
544 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530545}