blob: 5e54559ee446a77eef421765b67b52bf49852152 [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;
gyewan.an91d7e7e2019-01-17 15:12:48 +090049import org.onosproject.store.cluster.messaging.ClusterCommunicationService;
50import org.onosproject.store.cluster.messaging.ClusterCommunicationServiceAdapter;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030051import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053052
Sean Condon54d82432017-07-26 22:27:25 +010053import java.io.ByteArrayInputStream;
54import java.io.InputStream;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053055import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030056import java.util.Dictionary;
57import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080058import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053059import java.util.Map;
60import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080061import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053062
63import static org.hamcrest.Matchers.*;
64import static org.junit.Assert.*;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -070065import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_CONNECT_TIMEOUT_DEFAULT;
66import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_IDLE_TIMEOUT_DEFAULT;
67import static org.onosproject.netconf.ctl.impl.OsgiPropertyConstants.NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053068
69/**
70 * Unit tests for the Netconf controller implementation test.
71 */
72public class NetconfControllerImplTest {
Sean Condon54d82432017-07-26 22:27:25 +010073 private final Set<ConfigFactory> cfgFactories = new HashSet<>();
74 private final Set<NetworkConfigListener> netCfgListeners = new HashSet<>();
75 private boolean available = false;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070076
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053077 NetconfControllerImpl ctrl;
78
79 //DeviceInfo
80 NetconfDeviceInfo deviceInfo1;
81 NetconfDeviceInfo deviceInfo2;
82 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070083 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053084
Sean Condon54d82432017-07-26 22:27:25 +010085 NetconfDeviceConfig deviceConfig10;
86 DeviceId deviceConfig10Id;
87
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053088 //Devices & DeviceId
89 NetconfDevice device1;
90 DeviceId deviceId1;
91 NetconfDevice device2;
92 DeviceId deviceId2;
93
94 //Events
95 NetconfDeviceOutputEvent eventForDeviceInfo1;
96 NetconfDeviceOutputEvent eventForDeviceInfo2;
97
98 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
99 private NetconfDeviceOutputEventListener reflectedDownListener;
100
101 //Test Device IP addresses and ports
102 private static final String DEVICE_1_IP = "10.10.10.11";
103 private static final String DEVICE_2_IP = "10.10.10.12";
104 private static final String BAD_DEVICE_IP = "10.10.10.13";
Sean Condon54d82432017-07-26 22:27:25 +0100105 private static final String DEVICE_10_IP = "10.10.10.10";
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700106 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530107
108 private static final int DEVICE_1_PORT = 11;
109 private static final int DEVICE_2_PORT = 12;
110 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700111 private static final int IPV6_DEVICE_PORT = 14;
Sean Condon54d82432017-07-26 22:27:25 +0100112 private static final int DEVICE_10_PORT = 10;
113
114 private static final String DEVICE_10_USERNAME = "device10";
115 private static final String DEVICE_10_PASSWORD = "010";
116 private static final int DEVICE_10_CONNECT_TIMEOUT = 10;
117 private static final int DEVICE_10_REPLY_TIMEOUT = 11;
118 private static final int DEVICE_10_IDLE_TIMEOUT = 12;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700119
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300120 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700121 private static DeviceService deviceService = new NetconfDeviceServiceMock();
122 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Sean Condon54d82432017-07-26 22:27:25 +0100123 private final NetworkConfigRegistry netCfgService = new MockNetworkConfigRegistry();
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100124 private final MastershipService mastershipService = new MockmastershipService();
gyewan.an91d7e7e2019-01-17 15:12:48 +0900125 private final ClusterCommunicationService clusterCommunicationService =
126 new ClusterCommunicationServiceMock();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530127
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300128 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530129
130 @Before
131 public void setUp() throws Exception {
132 ctrl = new NetconfControllerImpl();
Yuta HIGUCHI2ee4fba2018-06-12 16:21:06 -0700133 ctrl.deviceFactory = (ncDevInfo) -> new TestNetconfDevice(ncDevInfo);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300134 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700135 ctrl.deviceService = deviceService;
136 ctrl.deviceKeyService = deviceKeyService;
Sean Condon54d82432017-07-26 22:27:25 +0100137 ctrl.netCfgService = netCfgService;
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100138 ctrl.mastershipService = mastershipService;
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700139 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
140 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
141 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
gyewan.an91d7e7e2019-01-17 15:12:48 +0900142 ctrl.clusterCommunicator = clusterCommunicationService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530143
144 //Creating mock devices
145 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
146 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700147 deviceInfo2.setSshClientLib(Optional.of(NetconfSshClientLib.APACHE_MINA));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530148 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700149 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530150
Sean Condon54d82432017-07-26 22:27:25 +0100151 deviceConfig10Id = DeviceId.deviceId("netconf:" + DEVICE_10_IP + ":" + DEVICE_10_PORT);
152 //Create a JSON entry just like Network Config accepts
153 ObjectMapper mapper = new ObjectMapper();
154 String jsonMessage = "{\n" +
155 " \"ip\":\"" + DEVICE_10_IP + "\",\n" +
156 " \"port\":" + DEVICE_10_PORT + ",\n" +
157 " \"username\":\"" + DEVICE_10_USERNAME + "\",\n" +
158 " \"password\":\"" + DEVICE_10_PASSWORD + "\",\n" +
159 " \"" + NetconfDeviceConfig.CONNECT_TIMEOUT + "\":" + DEVICE_10_CONNECT_TIMEOUT + ",\n" +
160 " \"" + NetconfDeviceConfig.REPLY_TIMEOUT + "\":" + DEVICE_10_REPLY_TIMEOUT + ",\n" +
161 " \"" + NetconfDeviceConfig.IDLE_TIMEOUT + "\":" + DEVICE_10_IDLE_TIMEOUT + ",\n" +
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700162 " \"" + NetconfDeviceConfig.SSHCLIENT + "\":\"" + NetconfSshClientLib.APACHE_MINA.toString() + "\"\n" +
Sean Condon54d82432017-07-26 22:27:25 +0100163 "}";
164 InputStream jsonStream = new ByteArrayInputStream(jsonMessage.getBytes());
165 JsonNode jsonNode = mapper.readTree(jsonStream);
166 jsonStream.close();
167 ConfigApplyDelegate delegate = new MockDelegate();
168 deviceConfig10 = new NetconfDeviceConfig();
169 deviceConfig10.init(deviceConfig10Id, "netconf", jsonNode, mapper, delegate);
170
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530171 device1 = new TestNetconfDevice(deviceInfo1);
172 deviceId1 = deviceInfo1.getDeviceId();
173 device2 = new TestNetconfDevice(deviceInfo2);
174 deviceId2 = deviceInfo2.getDeviceId();
175
176 //Adding to the map for testing get device calls.
177 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
178 field1.setAccessible(true);
179 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
180 reflectedDeviceMap.put(deviceId1, device1);
181 reflectedDeviceMap.put(deviceId2, device2);
182
183 //Creating mock events for testing NetconfDeviceOutputEventListener
184 Field field2 = ctrl.getClass().getDeclaredField("downListener");
185 field2.setAccessible(true);
186 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
187
188 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700189 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530190 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700191 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530192 }
193
194 @After
195 public void tearDown() {
196 ctrl.deactivate();
Yuta HIGUCHI09ae3682017-08-14 18:56:54 -0700197 // resetting static variables..
Thomas Vachuska00b5d4f2018-10-30 15:13:20 -0700198 NetconfControllerImpl.netconfConnectTimeout = NETCONF_CONNECT_TIMEOUT_DEFAULT;
199 NetconfControllerImpl.netconfIdleTimeout = NETCONF_IDLE_TIMEOUT_DEFAULT;
200 NetconfControllerImpl.netconfReplyTimeout = NETCONF_REPLY_TIMEOUT_DEFAULT;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530201 }
202
203 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300204 * Test initialization of component configuration.
205 */
206 @Test
207 public void testActivate() {
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);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300212 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000213 assertEquals("Incorrect NetConf connect timeout, should be default",
214 5, ctrl.netconfConnectTimeout);
215 assertEquals("Incorrect NetConf reply timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700216 5, ctrl.netconfReplyTimeout);
217 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300218
219 /**
220 * Test modification of component configuration.
221 */
222 @Test
223 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000224 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700225 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300226 assertEquals("Incorrect NetConf session timeout, should be default",
227 5, ctrl.netconfReplyTimeout);
228 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000229 assertEquals("Incorrect NetConf connect timeout, should be default",
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700230 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300231 assertEquals("Incorrect NetConf session timeout",
232 1, ctrl.netconfReplyTimeout);
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700233 assertEquals(NetconfSshClientLib.APACHE_MINA.toString(), ctrl.sshLibrary.toString());
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300234 }
235
236 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530237 * Test to add DeviceListeners,
238 * and also to check whether the netconfDeviceListeners set is
239 * updating or not which was present in NetconfControllerImpl class.
240 */
241 @Test
242 public void testAddRemoveDeviceListener() {
243 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
244 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
245 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
246
247 ctrl.addDeviceListener(deviceListener1);
248 ctrl.addDeviceListener(deviceListener2);
249 ctrl.addDeviceListener(deviceListener3);
250 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
251 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700252 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530253
254 ctrl.removeDeviceListener(deviceListener1);
255 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
256 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
257 }
258
259 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800260 public void testGetNetconfDevices() {
261 Set<DeviceId> devices = new HashSet<>();
262 devices.add(deviceId1);
263 devices.add(deviceId2);
264 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
265 }
266
267 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530268 public void testGetNetconfDevice() {
269 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
270 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
271
272 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
273 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
274 }
275
276 @Test
277 public void testGetNetconfDeviceWithIPPort() {
278 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
279 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
280
281 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
282 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
283 }
284
285 /**
286 * Check for bad device connection. In this case the device map shouldn't get modified.
287 */
288 @Test(expected = NetconfException.class)
289 public void testConnectBadDevice() throws Exception {
290 reflectedDeviceMap.clear();
291 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700292 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530293 } finally {
294 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
295 }
296 }
297
298 /**
Sean Condon54d82432017-07-26 22:27:25 +0100299 * Check for connection by netconfDeviceConfig.
300 */
301 @Test
302 public void testConnectDeviceNetConfig10() throws Exception {
303 NetconfDevice fetchedDevice10 = ctrl.connectDevice(deviceConfig10Id);
304 assertEquals("Incorrect device fetched - ip",
305 fetchedDevice10.getDeviceInfo().ip().toString(), DEVICE_10_IP);
306 assertEquals("Incorrect device fetched - port",
307 fetchedDevice10.getDeviceInfo().port(), DEVICE_10_PORT);
308 assertEquals("Incorrect device fetched - username",
309 fetchedDevice10.getDeviceInfo().name(), DEVICE_10_USERNAME);
310 assertEquals("Incorrect device fetched - password",
311 fetchedDevice10.getDeviceInfo().password(), DEVICE_10_PASSWORD);
312 assertEquals("Incorrect device fetched - connectTimeout",
313 fetchedDevice10.getDeviceInfo().getConnectTimeoutSec().getAsInt(),
314 DEVICE_10_CONNECT_TIMEOUT);
315 assertEquals("Incorrect device fetched - replyTimeout",
316 fetchedDevice10.getDeviceInfo().getReplyTimeoutSec().getAsInt(),
317 DEVICE_10_REPLY_TIMEOUT);
318 assertEquals("Incorrect device fetched - idleTimeout",
319 fetchedDevice10.getDeviceInfo().getIdleTimeoutSec().getAsInt(),
320 DEVICE_10_IDLE_TIMEOUT);
321 assertEquals("Incorrect device fetched - sshClient",
322 fetchedDevice10.getDeviceInfo().sshClientLib().get(),
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700323 NetconfSshClientLib.APACHE_MINA);
Sean Condon54d82432017-07-26 22:27:25 +0100324 }
325
326 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530327 * Check for correct device connection. In this case the device map get modified.
328 */
329 @Test
330 public void testConnectCorrectDevice() throws Exception {
331 reflectedDeviceMap.clear();
Sean Condon54d82432017-07-26 22:27:25 +0100332 NetconfDevice device1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
333 NetconfDevice device2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
David K. Bainbridge56e90232018-12-18 23:25:08 -0800334 assertTrue(String.format("Incorrect device connection from '%s' we get '%s' contains '%s'",
335 deviceInfo1, ctrl.getDevicesMap(), deviceId1),
336 ctrl.getDevicesMap().containsKey(deviceId1));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530337 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
338 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
339 }
340
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700341 /**
342 * Check for correct ipv6 device connection. In this case the device map get modified.
343 */
344 @Test
345 public void testConnectCorrectIpv6Device() throws Exception {
346 reflectedDeviceMap.clear();
347 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
348 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
349 .containsKey(deviceInfoIpV6.getDeviceId()));
350 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
351 }
352
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530353
354 /**
355 * Check for connect devices already added to the map.
356 */
357 @Test
358 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700359 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
360 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530361 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700362 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530363 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700364 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530365 }
366
367 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800368 * Check that disconnectDevice actually disconnects the device and removes it.
369 */
370 @Test
371 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700372 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800373 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
374 }
375
376 /**
377 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530378 */
379 @Test
380 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700381 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530382 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
383 }
384
385 /**
386 * Test to get the connected device map.
387 */
388 @Test
389 public void testGetDevicesMap() {
390 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
391 }
392
393 /**
394 * Test to check whether the DeviceDownEventListener removes the device from the map when session
395 * for a particular device getting closed.
396 */
397 @Test
398 public void testDeviceDownEventListener() throws Exception {
399 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700400 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530401 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
402 assertFalse("Irrelevant Device Event", result1);
403 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
404 reflectedDownListener.event(eventForDeviceInfo1);
405 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700406 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530407 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
408 assertTrue("Irrelevant Device Event", result2);
409 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
410 reflectedDownListener.event(eventForDeviceInfo2);
411 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
412 }
413
414 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530415 * Mock NetconfDeviceImpl class, used for creating test devices.
416 */
417 protected class TestNetconfDevice implements NetconfDevice {
418 private NetconfDeviceInfo netconfDeviceInfo;
419 private boolean deviceState = false;
420 private NetconfSession netconfSession;
421
422 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
423 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700424 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530425 netconfSession = EasyMock.createMock(NetconfSession.class);
426 deviceState = true;
427 } else {
428 throw new NetconfException("Cannot create Connection and Session");
429 }
430 }
431
432 @Override
433 public boolean isActive() {
434 return deviceState;
435 }
436
437 @Override
438 public NetconfSession getSession() {
439 return netconfSession;
440 }
441
442 @Override
443 public void disconnect() {
444 deviceState = false;
445 netconfSession = null;
446 }
447
448 @Override
449 public NetconfDeviceInfo getDeviceInfo() {
450 return netconfDeviceInfo;
451 }
452
453 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300454
455 private class MockComponentContext extends ComponentContextAdapter {
456 @Override
457 public Dictionary getProperties() {
458 return new MockDictionary();
459 }
460 }
461
462 private class MockDictionary extends Dictionary {
463
464 @Override
465 public int size() {
466 return 0;
467 }
468
469 @Override
470 public boolean isEmpty() {
471 return false;
472 }
473
474 @Override
475 public Enumeration keys() {
476 return null;
477 }
478
479 @Override
480 public Enumeration elements() {
481 return null;
482 }
483
484 @Override
485 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000486 if (key.equals("netconfConnectTimeout")) {
487 return "2";
488 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300489 return "1";
Andrea Campanella7bbe7b12017-05-03 16:03:38 -0700490 } else if (key.equals("sshLibrary")) {
Yuta HIGUCHI5233dec2018-05-02 15:22:37 -0700491 return NetconfSshClientLib.APACHE_MINA.toString();
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300492 }
493 return null;
494 }
495
496 @Override
497 public Object put(Object key, Object value) {
498 return null;
499 }
500
501 @Override
502 public Object remove(Object key) {
503 return null;
504 }
505 }
Sean Condon54d82432017-07-26 22:27:25 +0100506
507 private class MockNetworkConfigRegistry extends NetworkConfigRegistryAdapter {
508 NetconfDeviceConfig cfg = null;
509
510 @Override
511 public void registerConfigFactory(ConfigFactory configFactory) {
512 cfgFactories.add(configFactory);
513 }
514
515 @Override
516 public void unregisterConfigFactory(ConfigFactory configFactory) {
517 cfgFactories.remove(configFactory);
518 }
519
520 @Override
521 public void addListener(NetworkConfigListener listener) {
522 netCfgListeners.add(listener);
523 }
524
525 @Override
526 public void removeListener(NetworkConfigListener listener) {
527 netCfgListeners.remove(listener);
528 }
529
530
531 @Override
532 public <S, C extends Config<S>> C getConfig(S subject, Class<C> configClass) {
533 DeviceId did = (DeviceId) subject;
534 if (configClass.equals(NetconfDeviceConfig.class)
535 && did.equals(deviceConfig10Id)) {
536 return (C) deviceConfig10;
537 }
538 return null;
539 }
540
541 }
542
543 private class MockDelegate implements ConfigApplyDelegate {
544 @Override
545 public void onApply(Config configFile) {
546 }
547 }
Andrea Campanellaa2a6c3c2018-12-11 12:56:38 +0100548
549 private class MockmastershipService extends MastershipServiceAdapter {
550 @Override
551 public boolean isLocalMaster(DeviceId deviceId) {
552 return true;
553 }
554 }
gyewan.an91d7e7e2019-01-17 15:12:48 +0900555 private class ClusterCommunicationServiceMock extends ClusterCommunicationServiceAdapter {
556 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530557}