blob: 45304ea1adc6f6e390bf75e9eefa11fd30789866 [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 */
16
17package org.onosproject.netconf.ctl;
18
19import org.easymock.EasyMock;
20import org.junit.After;
21import org.junit.Before;
22import org.junit.Test;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030023import org.onlab.osgi.ComponentContextAdapter;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053024import org.onlab.packet.IpAddress;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030025import org.onosproject.cfg.ComponentConfigAdapter;
26import org.onosproject.cfg.ComponentConfigService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053027import org.onosproject.net.DeviceId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070028import org.onosproject.net.device.DeviceService;
29import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053030import org.onosproject.netconf.NetconfDevice;
31import org.onosproject.netconf.NetconfDeviceFactory;
32import org.onosproject.netconf.NetconfDeviceInfo;
33import org.onosproject.netconf.NetconfDeviceListener;
34import org.onosproject.netconf.NetconfDeviceOutputEvent;
35import org.onosproject.netconf.NetconfDeviceOutputEventListener;
36import org.onosproject.netconf.NetconfException;
37import org.onosproject.netconf.NetconfSession;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030038import org.osgi.service.component.ComponentContext;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053039
40import java.lang.reflect.Field;
Andreas Papazois4752cfa2016-04-25 14:52:12 +030041import java.util.Dictionary;
42import java.util.Enumeration;
Andrea Campanella86294db2016-03-07 11:42:49 -080043import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053044import java.util.Map;
45import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080046import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053047
48import static org.hamcrest.Matchers.*;
49import static org.junit.Assert.*;
50
51/**
52 * Unit tests for the Netconf controller implementation test.
53 */
54public class NetconfControllerImplTest {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070055
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053056 NetconfControllerImpl ctrl;
57
58 //DeviceInfo
59 NetconfDeviceInfo deviceInfo1;
60 NetconfDeviceInfo deviceInfo2;
61 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070062 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053063
64 //Devices & DeviceId
65 NetconfDevice device1;
66 DeviceId deviceId1;
67 NetconfDevice device2;
68 DeviceId deviceId2;
69
70 //Events
71 NetconfDeviceOutputEvent eventForDeviceInfo1;
72 NetconfDeviceOutputEvent eventForDeviceInfo2;
73
74 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
75 private NetconfDeviceOutputEventListener reflectedDownListener;
76
77 //Test Device IP addresses and ports
78 private static final String DEVICE_1_IP = "10.10.10.11";
79 private static final String DEVICE_2_IP = "10.10.10.12";
80 private static final String BAD_DEVICE_IP = "10.10.10.13";
Andrea Campanella7e6200a2016-03-21 09:48:40 -070081 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053082
83 private static final int DEVICE_1_PORT = 11;
84 private static final int DEVICE_2_PORT = 12;
85 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070086 private static final int IPV6_DEVICE_PORT = 14;
87
Andreas Papazois4752cfa2016-04-25 14:52:12 +030088 private static ComponentConfigService cfgService = new ComponentConfigAdapter();
Andrea Campanella7e6200a2016-03-21 09:48:40 -070089 private static DeviceService deviceService = new NetconfDeviceServiceMock();
90 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053091
Andreas Papazois4752cfa2016-04-25 14:52:12 +030092 private final ComponentContext context = new MockComponentContext();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053093
94 @Before
95 public void setUp() throws Exception {
96 ctrl = new NetconfControllerImpl();
97 ctrl.deviceFactory = new TestNetconfDeviceFactory();
Andreas Papazois4752cfa2016-04-25 14:52:12 +030098 ctrl.cfgService = cfgService;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070099 ctrl.deviceService = deviceService;
100 ctrl.deviceKeyService = deviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530101
102 //Creating mock devices
103 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
104 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
105 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700106 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530107
108 device1 = new TestNetconfDevice(deviceInfo1);
109 deviceId1 = deviceInfo1.getDeviceId();
110 device2 = new TestNetconfDevice(deviceInfo2);
111 deviceId2 = deviceInfo2.getDeviceId();
112
113 //Adding to the map for testing get device calls.
114 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
115 field1.setAccessible(true);
116 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
117 reflectedDeviceMap.put(deviceId1, device1);
118 reflectedDeviceMap.put(deviceId2, device2);
119
120 //Creating mock events for testing NetconfDeviceOutputEventListener
121 Field field2 = ctrl.getClass().getDeclaredField("downListener");
122 field2.setAccessible(true);
123 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
124
125 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700126 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530127 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700128 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530129 }
130
131 @After
132 public void tearDown() {
133 ctrl.deactivate();
134 }
135
136 /**
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300137 * Test initialization of component configuration.
138 */
139 @Test
140 public void testActivate() {
141 assertEquals("Incorrect NetConf session timeout, should be default",
142 5, ctrl.netconfReplyTimeout);
143 ctrl.activate(null);
144 assertEquals("Incorrect NetConf session timeout, should be default",
145 5, ctrl.netconfReplyTimeout);
146 }
147
148 /**
149 * Test modification of component configuration.
150 */
151 @Test
152 public void testModified() {
153 assertEquals("Incorrect NetConf session timeout, should be default",
154 5, ctrl.netconfReplyTimeout);
155 ctrl.modified(context);
156 assertEquals("Incorrect NetConf session timeout",
157 1, ctrl.netconfReplyTimeout);
158 }
159
160 /**
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530161 * Test to add DeviceListeners,
162 * and also to check whether the netconfDeviceListeners set is
163 * updating or not which was present in NetconfControllerImpl class.
164 */
165 @Test
166 public void testAddRemoveDeviceListener() {
167 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
168 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
169 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
170
171 ctrl.addDeviceListener(deviceListener1);
172 ctrl.addDeviceListener(deviceListener2);
173 ctrl.addDeviceListener(deviceListener3);
174 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
175 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700176 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530177
178 ctrl.removeDeviceListener(deviceListener1);
179 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
180 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
181 }
182
183 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800184 public void testGetNetconfDevices() {
185 Set<DeviceId> devices = new HashSet<>();
186 devices.add(deviceId1);
187 devices.add(deviceId2);
188 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
189 }
190
191 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530192 public void testGetNetconfDevice() {
193 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
194 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
195
196 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
197 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
198 }
199
200 @Test
201 public void testGetNetconfDeviceWithIPPort() {
202 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
203 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
204
205 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
206 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
207 }
208
209 /**
210 * Check for bad device connection. In this case the device map shouldn't get modified.
211 */
212 @Test(expected = NetconfException.class)
213 public void testConnectBadDevice() throws Exception {
214 reflectedDeviceMap.clear();
215 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700216 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530217 } finally {
218 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
219 }
220 }
221
222 /**
223 * Check for correct device connection. In this case the device map get modified.
224 */
225 @Test
226 public void testConnectCorrectDevice() throws Exception {
227 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700228 ctrl.connectDevice(deviceInfo1.getDeviceId());
229 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530230 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
231 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
232 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
233 }
234
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700235 /**
236 * Check for correct ipv6 device connection. In this case the device map get modified.
237 */
238 @Test
239 public void testConnectCorrectIpv6Device() throws Exception {
240 reflectedDeviceMap.clear();
241 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
242 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
243 .containsKey(deviceInfoIpV6.getDeviceId()));
244 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
245 }
246
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530247
248 /**
249 * Check for connect devices already added to the map.
250 */
251 @Test
252 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700253 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
254 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530255 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700256 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530257 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700258 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530259 }
260
261 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800262 * Check that disconnectDevice actually disconnects the device and removes it.
263 */
264 @Test
265 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700266 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800267 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
268 }
269
270 /**
271 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530272 */
273 @Test
274 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700275 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530276 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
277 }
278
279 /**
280 * Test to get the connected device map.
281 */
282 @Test
283 public void testGetDevicesMap() {
284 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
285 }
286
287 /**
288 * Test to check whether the DeviceDownEventListener removes the device from the map when session
289 * for a particular device getting closed.
290 */
291 @Test
292 public void testDeviceDownEventListener() throws Exception {
293 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700294 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530295 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
296 assertFalse("Irrelevant Device Event", result1);
297 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
298 reflectedDownListener.event(eventForDeviceInfo1);
299 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700300 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530301 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
302 assertTrue("Irrelevant Device Event", result2);
303 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
304 reflectedDownListener.event(eventForDeviceInfo2);
305 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
306 }
307
308 /**
309 * Mock NetconfDeviceFactory class.
310 */
311 private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
312
313 @Override
314 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
315 return new TestNetconfDevice(netconfDeviceInfo);
316 }
317 }
318
319 /**
320 * Mock NetconfDeviceImpl class, used for creating test devices.
321 */
322 protected class TestNetconfDevice implements NetconfDevice {
323 private NetconfDeviceInfo netconfDeviceInfo;
324 private boolean deviceState = false;
325 private NetconfSession netconfSession;
326
327 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
328 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700329 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530330 netconfSession = EasyMock.createMock(NetconfSession.class);
331 deviceState = true;
332 } else {
333 throw new NetconfException("Cannot create Connection and Session");
334 }
335 }
336
337 @Override
338 public boolean isActive() {
339 return deviceState;
340 }
341
342 @Override
343 public NetconfSession getSession() {
344 return netconfSession;
345 }
346
347 @Override
348 public void disconnect() {
349 deviceState = false;
350 netconfSession = null;
351 }
352
353 @Override
354 public NetconfDeviceInfo getDeviceInfo() {
355 return netconfDeviceInfo;
356 }
357
358 }
Andreas Papazois4752cfa2016-04-25 14:52:12 +0300359
360 private class MockComponentContext extends ComponentContextAdapter {
361 @Override
362 public Dictionary getProperties() {
363 return new MockDictionary();
364 }
365 }
366
367 private class MockDictionary extends Dictionary {
368
369 @Override
370 public int size() {
371 return 0;
372 }
373
374 @Override
375 public boolean isEmpty() {
376 return false;
377 }
378
379 @Override
380 public Enumeration keys() {
381 return null;
382 }
383
384 @Override
385 public Enumeration elements() {
386 return null;
387 }
388
389 @Override
390 public Object get(Object key) {
391 if (key.equals("netconfReplyTimeout")) {
392 return "1";
393 }
394 return null;
395 }
396
397 @Override
398 public Object put(Object key, Object value) {
399 return null;
400 }
401
402 @Override
403 public Object remove(Object key) {
404 return null;
405 }
406 }
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530407}