blob: 8287e6d7852ef012ffaec0aafbe09b3f3f67c669 [file] [log] [blame]
Thanuj Ravindranath926e3162016-01-26 09:44:08 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
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
18import org.easymock.EasyMock;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030022import org.onlab.osgi.ComponentContextAdapter;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053023import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030024import org.onosproject.cfg.ComponentConfigAdapter;
25import org.onosproject.cfg.ComponentConfigService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053026import org.onosproject.net.DeviceId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070027import org.onosproject.net.device.DeviceService;
28import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053029import org.onosproject.netconf.NetconfDevice;
30import org.onosproject.netconf.NetconfDeviceFactory;
31import org.onosproject.netconf.NetconfDeviceInfo;
32import org.onosproject.netconf.NetconfDeviceListener;
33import org.onosproject.netconf.NetconfDeviceOutputEvent;
34import org.onosproject.netconf.NetconfDeviceOutputEventListener;
35import org.onosproject.netconf.NetconfException;
36import org.onosproject.netconf.NetconfSession;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030037import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053038
39import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030040import java.util.Dictionary;
41import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080042import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053043import java.util.Map;
44import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080045import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053046
47import static org.hamcrest.Matchers.*;
48import static org.junit.Assert.*;
49
50/**
51 * Unit tests for the Netconf controller implementation test.
52 */
53public class NetconfControllerImplTest {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070054
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053055 NetconfControllerImpl ctrl;
56
57 //DeviceInfo
58 NetconfDeviceInfo deviceInfo1;
59 NetconfDeviceInfo deviceInfo2;
60 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070061 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053062
63 //Devices & DeviceId
64 NetconfDevice device1;
65 DeviceId deviceId1;
66 NetconfDevice device2;
67 DeviceId deviceId2;
68
69 //Events
70 NetconfDeviceOutputEvent eventForDeviceInfo1;
71 NetconfDeviceOutputEvent eventForDeviceInfo2;
72
73 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
74 private NetconfDeviceOutputEventListener reflectedDownListener;
75
76 //Test Device IP addresses and ports
77 private static final String DEVICE_1_IP = "10.10.10.11";
78 private static final String DEVICE_2_IP = "10.10.10.12";
79 private static final String BAD_DEVICE_IP = "10.10.10.13";
Andrea Campanella7e6200a2016-03-21 09:48:40 -070080 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053081
82 private static final int DEVICE_1_PORT = 11;
83 private static final int DEVICE_2_PORT = 12;
84 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070085 private static final int IPV6_DEVICE_PORT = 14;
86
Andreas Papazois4752cfa2016-04-25 14:52:12 +030087 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -070088 private static DeviceService deviceService = new NetconfDeviceServiceMock();
89 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053090
Andreas Papazois4752cfa2016-04-25 14:52:12 +030091 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053092
93 @Before
94 public void setUp() throws Exception {
95 ctrl = new NetconfControllerImpl();
96 ctrl.deviceFactory = new TestNetconfDeviceFactory();
Andreas Papazois4752cfa2016-04-25 14:52:12 +030097 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070098 ctrl.deviceService = deviceService;
99 ctrl.deviceKeyService = deviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530100
101 //Creating mock devices
102 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
103 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
104 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700105 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530106
107 device1 = new TestNetconfDevice(deviceInfo1);
108 deviceId1 = deviceInfo1.getDeviceId();
109 device2 = new TestNetconfDevice(deviceInfo2);
110 deviceId2 = deviceInfo2.getDeviceId();
111
112 //Adding to the map for testing get device calls.
113 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
114 field1.setAccessible(true);
115 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
116 reflectedDeviceMap.put(deviceId1, device1);
117 reflectedDeviceMap.put(deviceId2, device2);
118
119 //Creating mock events for testing NetconfDeviceOutputEventListener
120 Field field2 = ctrl.getClass().getDeclaredField("downListener");
121 field2.setAccessible(true);
122 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
123
124 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700125 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530126 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700127 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530128 }
129
130 @After
131 public void tearDown() {
132 ctrl.deactivate();
133 }
134
135 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300136 * Test initialization of component configuration.
137 */
138 @Test
139 public void testActivate() {
Sean Condon334ad692016-12-13 17:56:56 +0000140 assertEquals("Incorrect NetConf connect timeout, should be default",
141 5, ctrl.netconfConnectTimeout);
142 assertEquals("Incorrect NetConf reply timeout, should be default",
143 5, ctrl.netconfReplyTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300144 ctrl.activate(null);
Sean Condon334ad692016-12-13 17:56:56 +0000145 assertEquals("Incorrect NetConf connect timeout, should be default",
146 5, ctrl.netconfConnectTimeout);
147 assertEquals("Incorrect NetConf reply timeout, should be default",
148 5, ctrl.netconfReplyTimeout); }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300149
150 /**
151 * Test modification of component configuration.
152 */
153 @Test
154 public void testModified() {
Sean Condon334ad692016-12-13 17:56:56 +0000155 assertEquals("Incorrect NetConf connect timeout, should be default",
156 5, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300157 assertEquals("Incorrect NetConf session timeout, should be default",
158 5, ctrl.netconfReplyTimeout);
159 ctrl.modified(context);
Sean Condon334ad692016-12-13 17:56:56 +0000160 assertEquals("Incorrect NetConf connect timeout, should be default",
161 2, ctrl.netconfConnectTimeout);
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300162 assertEquals("Incorrect NetConf session timeout",
163 1, ctrl.netconfReplyTimeout);
164 }
165
166 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530167 * Test to add DeviceListeners,
168 * and also to check whether the netconfDeviceListeners set is
169 * updating or not which was present in NetconfControllerImpl class.
170 */
171 @Test
172 public void testAddRemoveDeviceListener() {
173 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
174 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
175 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
176
177 ctrl.addDeviceListener(deviceListener1);
178 ctrl.addDeviceListener(deviceListener2);
179 ctrl.addDeviceListener(deviceListener3);
180 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
181 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700182 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530183
184 ctrl.removeDeviceListener(deviceListener1);
185 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
186 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
187 }
188
189 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800190 public void testGetNetconfDevices() {
191 Set<DeviceId> devices = new HashSet<>();
192 devices.add(deviceId1);
193 devices.add(deviceId2);
194 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
195 }
196
197 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530198 public void testGetNetconfDevice() {
199 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
200 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
201
202 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
203 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
204 }
205
206 @Test
207 public void testGetNetconfDeviceWithIPPort() {
208 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
209 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
210
211 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
212 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
213 }
214
215 /**
216 * Check for bad device connection. In this case the device map shouldn't get modified.
217 */
218 @Test(expected = NetconfException.class)
219 public void testConnectBadDevice() throws Exception {
220 reflectedDeviceMap.clear();
221 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700222 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530223 } finally {
224 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
225 }
226 }
227
228 /**
229 * Check for correct device connection. In this case the device map get modified.
230 */
231 @Test
232 public void testConnectCorrectDevice() throws Exception {
233 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700234 ctrl.connectDevice(deviceInfo1.getDeviceId());
235 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530236 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
237 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
238 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
239 }
240
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700241 /**
242 * Check for correct ipv6 device connection. In this case the device map get modified.
243 */
244 @Test
245 public void testConnectCorrectIpv6Device() throws Exception {
246 reflectedDeviceMap.clear();
247 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
248 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
249 .containsKey(deviceInfoIpV6.getDeviceId()));
250 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
251 }
252
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530253
254 /**
255 * Check for connect devices already added to the map.
256 */
257 @Test
258 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700259 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
260 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530261 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700262 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530263 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700264 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530265 }
266
267 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800268 * Check that disconnectDevice actually disconnects the device and removes it.
269 */
270 @Test
271 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700272 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800273 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
274 }
275
276 /**
277 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530278 */
279 @Test
280 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700281 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530282 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
283 }
284
285 /**
286 * Test to get the connected device map.
287 */
288 @Test
289 public void testGetDevicesMap() {
290 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
291 }
292
293 /**
294 * Test to check whether the DeviceDownEventListener removes the device from the map when session
295 * for a particular device getting closed.
296 */
297 @Test
298 public void testDeviceDownEventListener() throws Exception {
299 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700300 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530301 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
302 assertFalse("Irrelevant Device Event", result1);
303 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
304 reflectedDownListener.event(eventForDeviceInfo1);
305 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700306 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530307 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
308 assertTrue("Irrelevant Device Event", result2);
309 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
310 reflectedDownListener.event(eventForDeviceInfo2);
311 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
312 }
313
314 /**
315 * Mock NetconfDeviceFactory class.
316 */
317 private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
318
319 @Override
320 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
321 return new TestNetconfDevice(netconfDeviceInfo);
322 }
323 }
324
325 /**
326 * Mock NetconfDeviceImpl class, used for creating test devices.
327 */
328 protected class TestNetconfDevice implements NetconfDevice {
329 private NetconfDeviceInfo netconfDeviceInfo;
330 private boolean deviceState = false;
331 private NetconfSession netconfSession;
332
333 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
334 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700335 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530336 netconfSession = EasyMock.createMock(NetconfSession.class);
337 deviceState = true;
338 } else {
339 throw new NetconfException("Cannot create Connection and Session");
340 }
341 }
342
343 @Override
344 public boolean isActive() {
345 return deviceState;
346 }
347
348 @Override
349 public NetconfSession getSession() {
350 return netconfSession;
351 }
352
353 @Override
354 public void disconnect() {
355 deviceState = false;
356 netconfSession = null;
357 }
358
359 @Override
360 public NetconfDeviceInfo getDeviceInfo() {
361 return netconfDeviceInfo;
362 }
363
364 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300365
366 private class MockComponentContext extends ComponentContextAdapter {
367 @Override
368 public Dictionary getProperties() {
369 return new MockDictionary();
370 }
371 }
372
373 private class MockDictionary extends Dictionary {
374
375 @Override
376 public int size() {
377 return 0;
378 }
379
380 @Override
381 public boolean isEmpty() {
382 return false;
383 }
384
385 @Override
386 public Enumeration keys() {
387 return null;
388 }
389
390 @Override
391 public Enumeration elements() {
392 return null;
393 }
394
395 @Override
396 public Object get(Object key) {
Sean Condon334ad692016-12-13 17:56:56 +0000397 if (key.equals("netconfConnectTimeout")) {
398 return "2";
399 } else if (key.equals("netconfReplyTimeout")) {
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300400 return "1";
401 }
402 return null;
403 }
404
405 @Override
406 public Object put(Object key, Object value) {
407 return null;
408 }
409
410 @Override
411 public Object remove(Object key) {
412 return null;
413 }
414 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530415}