blob: bbac12367b6b3b809c5b333074e2ee47cea0c940 [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;
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;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030049import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053050
Sean Condon54d82432017-07-26 22:27:25 +010051import java.io.ByteArrayInputStream;
52import java.io.InputStream;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053053import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030054import java.util.Dictionary;
55import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080056import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053057import java.util.Map;
58import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080059import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053060
61import static org.hamcrest.Matchers.*;
62import static org.junit.Assert.*;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070063import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_CONNECT_TIMEOUT_DEFAULT;
64import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_IDLE_TIMEOUT_DEFAULT;
65import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053066
67/**
68 * Unit tests for the Netconf controller implementation test.
69 */
70public class NetconfControllerImplTest {
Sean Condon54d82432017-07-26 22:27:25 +010071 private final Set<ConfigFactory> cfgFactories = new HashSet<>();
72 private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
73 private boolean available = false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070074
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053075 NetconfControllerImpl ctrl;
76
77 //DeviceInfo
78 NetconfDeviceInfo deviceInfo1;
79 NetconfDeviceInfo deviceInfo2;
80 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070081 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053082
Sean Condon54d82432017-07-26 22:27:25 +010083 NetconfDeviceConfig deviceConfig10;
84 DeviceId deviceConfig10Id;
85
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053086 //Devices & DeviceId
87 NetconfDevice device1;
88 DeviceId deviceId1;
89 NetconfDevice device2;
90 DeviceId deviceId2;
91
92 //Events
93 NetconfDeviceOutputEvent eventForDeviceInfo1;
94 NetconfDeviceOutputEvent eventForDeviceInfo2;
95
96 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
97 private NetconfDeviceOutputEventListener reflectedDownListener;
98
99 //Test Device IP addresses and ports
100 private static final String DEVICE_1_IP = "10.10.10.11";
101 private static final String DEVICE_2_IP = "10.10.10.12";
102 private static final String BAD_DEVICE_IP = "10.10.10.13";
Sean Condon54d82432017-07-26 22:27:25 +0100103 private static final String DEVICE_10_IP = "10.10.10.10";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700104 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530105
106 private static final int DEVICE_1_PORT = 11;
107 private static final int DEVICE_2_PORT = 12;
108 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700109 private static final int IPV6_DEVICE_PORT = 14;
Sean Condon54d82432017-07-26 22:27:25 +0100110 private static final int DEVICE_10_PORT = 10;
111
112 private static final String DEVICE_10_USERNAME = "device10";
113 private static final String DEVICE_10_PASSWORD = "010";
114 private static final int DEVICE_10_CONNECT_TIMEOUT = 10;
115 private static final int DEVICE_10_REPLY_TIMEOUT = 11;
116 private static final int DEVICE_10_IDLE_TIMEOUT = 12;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700117
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300118 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700119 private static DeviceService deviceService = new NetconfDeviceServiceMock();
120 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Sean Condon54d82432017-07-26 22:27:25 +0100121 private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100122 private final MastershipService mastershipService = new MockmastershipService();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530123
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300124 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530125
126 @Before
127 public void setUp() throws Exception {
128 ctrl = new NetconfControllerImpl();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700129 ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300130 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700131 ctrl.deviceService = deviceService;
132 ctrl.deviceKeyService = deviceKeyService;
Sean Condon54d82432017-07-26 22:27:25 +0100133 ctrl.netCfgService = netCfgService;
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100134 ctrl.mastershipService = mastershipService;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700135 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
136 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
137 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530138
139 //Creating mock devices
140 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
141 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700142 deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530143 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700144 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530145
Sean Condon54d82432017-07-26 22:27:25 +0100146 deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
147 //Create a JSON entry just like Network Config accepts
148 ObjectMapper mapper = new ObjectMapper();
149 String jsonMessage = "{\n" +
150 " \"ip\":\"" + DEVICE_10_IP + "\",\n" +
151 " \"port\":" + DEVICE_10_PORT + ",\n" +
152 " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" +
153 " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" +
154 " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" +
155 " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" +
156 " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" +
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700157 " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" +
Sean Condon54d82432017-07-26 22:27:25 +0100158 "}";
159 InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
160 JsonNode jsonNode = mapper.readTree(jsonStream);
161 jsonStream.close();
162 ConfigApplyDelegate delegate = new MockDelegate();
163 deviceConfig10 = new NetconfDeviceConfig();
164 deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
165
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530166 device1 = new TestNetconfDevice(deviceInfo1);
167 deviceId1 = deviceInfo1.getDeviceId();
168 device2 = new TestNetconfDevice(deviceInfo2);
169 deviceId2 = deviceInfo2.getDeviceId();
170
171 //Adding to the map for testing get device calls.
172 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
173 field1.setAccessible(true);
174 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
175 reflectedDeviceMap.put(deviceId1, device1);
176 reflectedDeviceMap.put(deviceId2, device2);
177
178 //Creating mock events for testing NetconfDeviceOutputEventListener
179 Field field2 = ctrl.getClass().getDeclaredField("downListener");
180 field2.setAccessible(true);
181 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
182
183 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700184 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530185 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700186 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530187 }
188
189 @After
190 public void tearDown() {
191 ctrl.deactivate();
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700192 // resetting static variables..
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700193 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
194 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
195 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530196 }
197
198 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300199 * Test initialization of component configuration.
200 */
201 @Test
202 public void testActivate() {
Sean Condon334ad692016-12-13 17:56:56 +0000203 assertEquals("Incorrect NetConf connect timeout, should be default",
204 5, ctrl.netconfConnectTimeout);
205 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700206 5, ctrl.netconfReplyTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300207 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000208 assertEquals("Incorrect NetConf connect timeout, should be default",
209 5, ctrl.netconfConnectTimeout);
210 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700211 5, ctrl.netconfReplyTimeout);
212 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300213
214 /**
215 * Test modification of component configuration.
216 */
217 @Test
218 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000219 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700220 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300221 assertEquals("Incorrect NetConf session timeout, should be default",
222 5, ctrl.netconfReplyTimeout);
223 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000224 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700225 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300226 assertEquals("Incorrect NetConf session timeout",
227 1, ctrl.netconfReplyTimeout);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700228 assertEquals(NetconfSshClientLib.APACHE_MINA.toString(), ctrl.sshLibrary.toString());
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300229 }
230
231 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530232 * Test to add DeviceListeners,
233 * and also to check whether the netconfDeviceListeners set is
234 * updating or not which was present in NetconfControllerImpl class.
235 */
236 @Test
237 public void testAddRemoveDeviceListener() {
238 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
239 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
240 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
241
242 ctrl.addDeviceListener(deviceListener1);
243 ctrl.addDeviceListener(deviceListener2);
244 ctrl.addDeviceListener(deviceListener3);
245 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
246 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700247 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530248
249 ctrl.removeDeviceListener(deviceListener1);
250 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
251 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
252 }
253
254 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800255 public void testGetNetconfDevices() {
256 Set<DeviceId> devices = new HashSet<>();
257 devices.add(deviceId1);
258 devices.add(deviceId2);
259 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
260 }
261
262 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530263 public void testGetNetconfDevice() {
264 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
265 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
266
267 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
268 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
269 }
270
271 @Test
272 public void testGetNetconfDeviceWithIPPort() {
273 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
274 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
275
276 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
277 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
278 }
279
280 /**
281 * Check for bad device connection. In this case the device map shouldn't get modified.
282 */
283 @Test(expected = NetconfException.class)
284 public void testConnectBadDevice() throws Exception {
285 reflectedDeviceMap.clear();
286 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700287 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530288 } finally {
289 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
290 }
291 }
292
293 /**
Sean Condon54d82432017-07-26 22:27:25 +0100294 * Check for connection by netconfDeviceConfig.
295 */
296 @Test
297 public void testConnectDeviceNetConfig10() throws Exception {
298 NetconfDevice fetchedDevice10 = ctrl.connectDevice(deviceConfig10Id);
299 assertEquals("Incorrect device fetched - ip",
300 fetchedDevice10.getDeviceInfo().ip().toString(), DEVICE_10_IP);
301 assertEquals("Incorrect device fetched - port",
302 fetchedDevice10.getDeviceInfo().port(), DEVICE_10_PORT);
303 assertEquals("Incorrect device fetched - username",
304 fetchedDevice10.getDeviceInfo().name(), DEVICE_10_USERNAME);
305 assertEquals("Incorrect device fetched - password",
306 fetchedDevice10.getDeviceInfo().password(), DEVICE_10_PASSWORD);
307 assertEquals("Incorrect device fetched - connectTimeout",
308 fetchedDevice10.getDeviceInfo().getConnectTimeoutSec().getAsInt(),
309 DEVICE_10_CONNECT_TIMEOUT);
310 assertEquals("Incorrect device fetched - replyTimeout",
311 fetchedDevice10.getDeviceInfo().getReplyTimeoutSec().getAsInt(),
312 DEVICE_10_REPLY_TIMEOUT);
313 assertEquals("Incorrect device fetched - idleTimeout",
314 fetchedDevice10.getDeviceInfo().getIdleTimeoutSec().getAsInt(),
315 DEVICE_10_IDLE_TIMEOUT);
316 assertEquals("Incorrect device fetched - sshClient",
317 fetchedDevice10.getDeviceInfo().sshClientLib().get(),
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700318 NetconfSshClientLib.APACHE_MINA);
Sean Condon54d82432017-07-26 22:27:25 +0100319 }
320
321 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530322 * Check for correct device connection. In this case the device map get modified.
323 */
324 @Test
325 public void testConnectCorrectDevice() throws Exception {
326 reflectedDeviceMap.clear();
Sean Condon54d82432017-07-26 22:27:25 +0100327 NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
328 NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
David K. Bainbridge56e90232018-12-18 23:25:08 -0800329 assertTrue(String.format("Incorrect device connection from '%s' we get '%s' contains '%s'",
330 deviceInfo1, ctrl.getDevicesMap(), deviceId1),
331 ctrl.getDevicesMap().containsKey(deviceId1));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530332 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
333 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
334 }
335
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700336 /**
337 * Check for correct ipv6 device connection. In this case the device map get modified.
338 */
339 @Test
340 public void testConnectCorrectIpv6Device() throws Exception {
341 reflectedDeviceMap.clear();
342 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
343 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
344 .containsKey(deviceInfoIpV6.getDeviceId()));
345 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
346 }
347
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530348
349 /**
350 * Check for connect devices already added to the map.
351 */
352 @Test
353 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700354 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
355 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530356 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700357 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530358 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700359 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530360 }
361
362 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800363 * Check that disconnectDevice actually disconnects the device and removes it.
364 */
365 @Test
366 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700367 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800368 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
369 }
370
371 /**
372 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530373 */
374 @Test
375 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700376 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530377 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
378 }
379
380 /**
381 * Test to get the connected device map.
382 */
383 @Test
384 public void testGetDevicesMap() {
385 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
386 }
387
388 /**
389 * Test to check whether the DeviceDownEventListener removes the device from the map when session
390 * for a particular device getting closed.
391 */
392 @Test
393 public void testDeviceDownEventListener() throws Exception {
394 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700395 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530396 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
397 assertFalse("Irrelevant Device Event", result1);
398 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
399 reflectedDownListener.event(eventForDeviceInfo1);
400 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700401 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530402 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
403 assertTrue("Irrelevant Device Event", result2);
404 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
405 reflectedDownListener.event(eventForDeviceInfo2);
406 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
407 }
408
409 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530410 * Mock NetconfDeviceImpl class, used for creating test devices.
411 */
412 protected class TestNetconfDevice implements NetconfDevice {
413 private NetconfDeviceInfo netconfDeviceInfo;
414 private boolean deviceState = false;
415 private NetconfSession netconfSession;
416
417 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
418 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700419 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530420 netconfSession = EasyMock.createMock(NetconfSession.class);
421 deviceState = true;
422 } else {
423 throw new NetconfException("Cannot create Connection and Session");
424 }
425 }
426
427 @Override
428 public boolean isActive() {
429 return deviceState;
430 }
431
432 @Override
433 public NetconfSession getSession() {
434 return netconfSession;
435 }
436
437 @Override
438 public void disconnect() {
439 deviceState = false;
440 netconfSession = null;
441 }
442
443 @Override
444 public NetconfDeviceInfo getDeviceInfo() {
445 return netconfDeviceInfo;
446 }
447
448 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300449
450 private class MockComponentContext extends ComponentContextAdapter {
451 @Override
452 public Dictionary getProperties() {
453 return new MockDictionary();
454 }
455 }
456
457 private class MockDictionary extends Dictionary {
458
459 @Override
460 public int size() {
461 return 0;
462 }
463
464 @Override
465 public boolean isEmpty() {
466 return false;
467 }
468
469 @Override
470 public Enumeration keys() {
471 return null;
472 }
473
474 @Override
475 public Enumeration elements() {
476 return null;
477 }
478
479 @Override
480 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000481 if (key.equals("netconfConnectTimeout")) {
482 return "2";
483 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300484 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700485 } else if (key.equals("sshLibrary")) {
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700486 return NetconfSshClientLib.APACHE_MINA.toString();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300487 }
488 return null;
489 }
490
491 @Override
492 public Object put(Object key, Object value) {
493 return null;
494 }
495
496 @Override
497 public Object remove(Object key) {
498 return null;
499 }
500 }
Sean Condon54d82432017-07-26 22:27:25 +0100501
502 private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
503 NetconfDeviceConfig cfg = null;
504
505 @Override
506 public void registerConfigFactory(ConfigFactory configFactory) {
507 cfgFactories.add(configFactory);
508 }
509
510 @Override
511 public void unregisterConfigFactory(ConfigFactory configFactory) {
512 cfgFactories.remove(configFactory);
513 }
514
515 @Override
516 public void addListener(NetworkConfigListener listener) {
517 netCfgListeners.add(listener);
518 }
519
520 @Override
521 public void removeListener(NetworkConfigListener listener) {
522 netCfgListeners.remove(listener);
523 }
524
525
526 @Override
527 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
528 DeviceId did = (DeviceId) subject;
529 if (configClass.equals(NetconfDeviceConfig.class)
530 && did.equals(deviceConfig10Id)) {
531 return (C) deviceConfig10;
532 }
533 return null;
534 }
535
536 }
537
538 private class MockDelegate implements ConfigApplyDelegate {
539 @Override
540 public void onApply(Config configFile) {
541 }
542 }
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100543
544 private class MockmastershipService extends MastershipServiceAdapter {
545 @Override
546 public boolean isLocalMaster(DeviceId deviceId) {
547 return true;
548 }
549 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530550}