blob: 03bb53e1072d8999b6832a767e52a43de977f4a2 [file] [log] [blame]
Thanuj Ravindranath926e3162016-01-26 09:44:08 +05301/*
2 * Copyright 2016 Open Networking Laboratory
3 *
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;
23import org.onlab.packet.IpAddress;
24import org.onosproject.net.DeviceId;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070025import org.onosproject.net.device.DeviceService;
26import org.onosproject.net.key.DeviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053027import org.onosproject.netconf.NetconfDevice;
28import org.onosproject.netconf.NetconfDeviceFactory;
29import org.onosproject.netconf.NetconfDeviceInfo;
30import org.onosproject.netconf.NetconfDeviceListener;
31import org.onosproject.netconf.NetconfDeviceOutputEvent;
32import org.onosproject.netconf.NetconfDeviceOutputEventListener;
33import org.onosproject.netconf.NetconfException;
34import org.onosproject.netconf.NetconfSession;
35
36import java.lang.reflect.Field;
Andrea Campanella86294db2016-03-07 11:42:49 -080037import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053038import java.util.Map;
39import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080040import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053041
42import static org.hamcrest.Matchers.*;
43import static org.junit.Assert.*;
44
45/**
46 * Unit tests for the Netconf controller implementation test.
47 */
48public class NetconfControllerImplTest {
Andrea Campanella7e6200a2016-03-21 09:48:40 -070049
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053050 NetconfControllerImpl ctrl;
51
52 //DeviceInfo
53 NetconfDeviceInfo deviceInfo1;
54 NetconfDeviceInfo deviceInfo2;
55 NetconfDeviceInfo badDeviceInfo3;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070056 NetconfDeviceInfo deviceInfoIpV6;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053057
58 //Devices & DeviceId
59 NetconfDevice device1;
60 DeviceId deviceId1;
61 NetconfDevice device2;
62 DeviceId deviceId2;
63
64 //Events
65 NetconfDeviceOutputEvent eventForDeviceInfo1;
66 NetconfDeviceOutputEvent eventForDeviceInfo2;
67
68 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
69 private NetconfDeviceOutputEventListener reflectedDownListener;
70
71 //Test Device IP addresses and ports
72 private static final String DEVICE_1_IP = "10.10.10.11";
73 private static final String DEVICE_2_IP = "10.10.10.12";
74 private static final String BAD_DEVICE_IP = "10.10.10.13";
Andrea Campanella7e6200a2016-03-21 09:48:40 -070075 private static final String DEVICE_IPV6 = "2001:db8::1";
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053076
77 private static final int DEVICE_1_PORT = 11;
78 private static final int DEVICE_2_PORT = 12;
79 private static final int BAD_DEVICE_PORT = 13;
Andrea Campanella7e6200a2016-03-21 09:48:40 -070080 private static final int IPV6_DEVICE_PORT = 14;
81
82 private static DeviceService deviceService = new NetconfDeviceServiceMock();
83 private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock();
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053084
85
86 @Before
87 public void setUp() throws Exception {
88 ctrl = new NetconfControllerImpl();
89 ctrl.deviceFactory = new TestNetconfDeviceFactory();
Andrea Campanella7e6200a2016-03-21 09:48:40 -070090 ctrl.deviceService = deviceService;
91 ctrl.deviceKeyService = deviceKeyService;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053092
93 //Creating mock devices
94 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
95 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
96 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
Andrea Campanella7e6200a2016-03-21 09:48:40 -070097 deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053098
99 device1 = new TestNetconfDevice(deviceInfo1);
100 deviceId1 = deviceInfo1.getDeviceId();
101 device2 = new TestNetconfDevice(deviceInfo2);
102 deviceId2 = deviceInfo2.getDeviceId();
103
104 //Adding to the map for testing get device calls.
105 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
106 field1.setAccessible(true);
107 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
108 reflectedDeviceMap.put(deviceId1, device1);
109 reflectedDeviceMap.put(deviceId2, device2);
110
111 //Creating mock events for testing NetconfDeviceOutputEventListener
112 Field field2 = ctrl.getClass().getDeclaredField("downListener");
113 field2.setAccessible(true);
114 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
115
116 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700117 null, Optional.of(1), deviceInfo1);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530118 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700119 null, Optional.of(2), deviceInfo2);
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530120 }
121
122 @After
123 public void tearDown() {
124 ctrl.deactivate();
125 }
126
127 /**
128 * Test to add DeviceListeners,
129 * and also to check whether the netconfDeviceListeners set is
130 * updating or not which was present in NetconfControllerImpl class.
131 */
132 @Test
133 public void testAddRemoveDeviceListener() {
134 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
135 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
136 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
137
138 ctrl.addDeviceListener(deviceListener1);
139 ctrl.addDeviceListener(deviceListener2);
140 ctrl.addDeviceListener(deviceListener3);
141 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
142 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700143 deviceListener2, deviceListener3));
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530144
145 ctrl.removeDeviceListener(deviceListener1);
146 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
147 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
148 }
149
150 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800151 public void testGetNetconfDevices() {
152 Set<DeviceId> devices = new HashSet<>();
153 devices.add(deviceId1);
154 devices.add(deviceId2);
155 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
156 }
157
158 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530159 public void testGetNetconfDevice() {
160 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
161 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
162
163 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
164 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
165 }
166
167 @Test
168 public void testGetNetconfDeviceWithIPPort() {
169 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
170 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
171
172 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
173 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
174 }
175
176 /**
177 * Check for bad device connection. In this case the device map shouldn't get modified.
178 */
179 @Test(expected = NetconfException.class)
180 public void testConnectBadDevice() throws Exception {
181 reflectedDeviceMap.clear();
182 try {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700183 ctrl.connectDevice(badDeviceInfo3.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530184 } finally {
185 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
186 }
187 }
188
189 /**
190 * Check for correct device connection. In this case the device map get modified.
191 */
192 @Test
193 public void testConnectCorrectDevice() throws Exception {
194 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700195 ctrl.connectDevice(deviceInfo1.getDeviceId());
196 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530197 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
198 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
199 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
200 }
201
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700202 /**
203 * Check for correct ipv6 device connection. In this case the device map get modified.
204 */
205 @Test
206 public void testConnectCorrectIpv6Device() throws Exception {
207 reflectedDeviceMap.clear();
208 ctrl.connectDevice(deviceInfoIpV6.getDeviceId());
209 assertTrue("Incorrect device connection", ctrl.getDevicesMap()
210 .containsKey(deviceInfoIpV6.getDeviceId()));
211 assertEquals("Incorrect device connection", 1, ctrl.getDevicesMap().size());
212 }
213
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530214
215 /**
216 * Check for connect devices already added to the map.
217 */
218 @Test
219 public void testConnectAlreadyExistingDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700220 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId());
221 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530222 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700223 deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530224 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700225 deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530226 }
227
228 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800229 * Check that disconnectDevice actually disconnects the device and removes it.
230 */
231 @Test
232 public void testDisconnectDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700233 ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true);
Andrea Campanella86294db2016-03-07 11:42:49 -0800234 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
235 }
236
237 /**
238 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530239 */
240 @Test
241 public void testRemoveDevice() throws Exception {
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700242 ctrl.removeDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530243 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
244 }
245
246 /**
247 * Test to get the connected device map.
248 */
249 @Test
250 public void testGetDevicesMap() {
251 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
252 }
253
254 /**
255 * Test to check whether the DeviceDownEventListener removes the device from the map when session
256 * for a particular device getting closed.
257 */
258 @Test
259 public void testDeviceDownEventListener() throws Exception {
260 reflectedDeviceMap.clear();
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700261 ctrl.connectDevice(deviceInfo1.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530262 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
263 assertFalse("Irrelevant Device Event", result1);
264 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
265 reflectedDownListener.event(eventForDeviceInfo1);
266 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700267 ctrl.connectDevice(deviceInfo2.getDeviceId());
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530268 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
269 assertTrue("Irrelevant Device Event", result2);
270 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
271 reflectedDownListener.event(eventForDeviceInfo2);
272 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
273 }
274
275 /**
276 * Mock NetconfDeviceFactory class.
277 */
278 private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
279
280 @Override
281 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
282 return new TestNetconfDevice(netconfDeviceInfo);
283 }
284 }
285
286 /**
287 * Mock NetconfDeviceImpl class, used for creating test devices.
288 */
289 protected class TestNetconfDevice implements NetconfDevice {
290 private NetconfDeviceInfo netconfDeviceInfo;
291 private boolean deviceState = false;
292 private NetconfSession netconfSession;
293
294 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
295 netconfDeviceInfo = deviceInfo;
Andrea Campanella7e6200a2016-03-21 09:48:40 -0700296 if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) {
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530297 netconfSession = EasyMock.createMock(NetconfSession.class);
298 deviceState = true;
299 } else {
300 throw new NetconfException("Cannot create Connection and Session");
301 }
302 }
303
304 @Override
305 public boolean isActive() {
306 return deviceState;
307 }
308
309 @Override
310 public NetconfSession getSession() {
311 return netconfSession;
312 }
313
314 @Override
315 public void disconnect() {
316 deviceState = false;
317 netconfSession = null;
318 }
319
320 @Override
321 public NetconfDeviceInfo getDeviceInfo() {
322 return netconfDeviceInfo;
323 }
324
325 }
326}