blob: 931bfd4c3ec778fe617403d5dd1d9b22222d1506 [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;
25import org.onosproject.netconf.NetconfDevice;
26import org.onosproject.netconf.NetconfDeviceFactory;
27import org.onosproject.netconf.NetconfDeviceInfo;
28import org.onosproject.netconf.NetconfDeviceListener;
29import org.onosproject.netconf.NetconfDeviceOutputEvent;
30import org.onosproject.netconf.NetconfDeviceOutputEventListener;
31import org.onosproject.netconf.NetconfException;
32import org.onosproject.netconf.NetconfSession;
33
34import java.lang.reflect.Field;
Andrea Campanella86294db2016-03-07 11:42:49 -080035import java.util.HashSet;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053036import java.util.Map;
37import java.util.Optional;
Andrea Campanella86294db2016-03-07 11:42:49 -080038import java.util.Set;
Thanuj Ravindranath926e3162016-01-26 09:44:08 +053039
40import static org.hamcrest.Matchers.*;
41import static org.junit.Assert.*;
42
43/**
44 * Unit tests for the Netconf controller implementation test.
45 */
46public class NetconfControllerImplTest {
47 NetconfControllerImpl ctrl;
48
49 //DeviceInfo
50 NetconfDeviceInfo deviceInfo1;
51 NetconfDeviceInfo deviceInfo2;
52 NetconfDeviceInfo badDeviceInfo3;
53
54 //Devices & DeviceId
55 NetconfDevice device1;
56 DeviceId deviceId1;
57 NetconfDevice device2;
58 DeviceId deviceId2;
59
60 //Events
61 NetconfDeviceOutputEvent eventForDeviceInfo1;
62 NetconfDeviceOutputEvent eventForDeviceInfo2;
63
64 private Map<DeviceId, NetconfDevice> reflectedDeviceMap;
65 private NetconfDeviceOutputEventListener reflectedDownListener;
66
67 //Test Device IP addresses and ports
68 private static final String DEVICE_1_IP = "10.10.10.11";
69 private static final String DEVICE_2_IP = "10.10.10.12";
70 private static final String BAD_DEVICE_IP = "10.10.10.13";
71
72 private static final int DEVICE_1_PORT = 11;
73 private static final int DEVICE_2_PORT = 12;
74 private static final int BAD_DEVICE_PORT = 13;
75
76
77 @Before
78 public void setUp() throws Exception {
79 ctrl = new NetconfControllerImpl();
80 ctrl.deviceFactory = new TestNetconfDeviceFactory();
81
82 //Creating mock devices
83 deviceInfo1 = new NetconfDeviceInfo("device1", "001", IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
84 deviceInfo2 = new NetconfDeviceInfo("device2", "002", IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
85 badDeviceInfo3 = new NetconfDeviceInfo("device3", "003", IpAddress.valueOf(BAD_DEVICE_IP), BAD_DEVICE_PORT);
86
87 device1 = new TestNetconfDevice(deviceInfo1);
88 deviceId1 = deviceInfo1.getDeviceId();
89 device2 = new TestNetconfDevice(deviceInfo2);
90 deviceId2 = deviceInfo2.getDeviceId();
91
92 //Adding to the map for testing get device calls.
93 Field field1 = ctrl.getClass().getDeclaredField("netconfDeviceMap");
94 field1.setAccessible(true);
95 reflectedDeviceMap = (Map<DeviceId, NetconfDevice>) field1.get(ctrl);
96 reflectedDeviceMap.put(deviceId1, device1);
97 reflectedDeviceMap.put(deviceId2, device2);
98
99 //Creating mock events for testing NetconfDeviceOutputEventListener
100 Field field2 = ctrl.getClass().getDeclaredField("downListener");
101 field2.setAccessible(true);
102 reflectedDownListener = (NetconfDeviceOutputEventListener) field2.get(ctrl);
103
104 eventForDeviceInfo1 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_NOTIFICATION, null,
105 null, Optional.of(1), deviceInfo1);
106 eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null,
107 null, Optional.of(2), deviceInfo2);
108 }
109
110 @After
111 public void tearDown() {
112 ctrl.deactivate();
113 }
114
115 /**
116 * Test to add DeviceListeners,
117 * and also to check whether the netconfDeviceListeners set is
118 * updating or not which was present in NetconfControllerImpl class.
119 */
120 @Test
121 public void testAddRemoveDeviceListener() {
122 NetconfDeviceListener deviceListener1 = EasyMock.createMock(NetconfDeviceListener.class);
123 NetconfDeviceListener deviceListener2 = EasyMock.createMock(NetconfDeviceListener.class);
124 NetconfDeviceListener deviceListener3 = EasyMock.createMock(NetconfDeviceListener.class);
125
126 ctrl.addDeviceListener(deviceListener1);
127 ctrl.addDeviceListener(deviceListener2);
128 ctrl.addDeviceListener(deviceListener3);
129 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(3));
130 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener1,
131 deviceListener2, deviceListener3));
132
133 ctrl.removeDeviceListener(deviceListener1);
134 assertThat("Incorrect number of listeners", ctrl.netconfDeviceListeners, hasSize(2));
135 assertThat("Not matching listeners", ctrl.netconfDeviceListeners, hasItems(deviceListener2, deviceListener3));
136 }
137
138 @Test
Andrea Campanella86294db2016-03-07 11:42:49 -0800139 public void testGetNetconfDevices() {
140 Set<DeviceId> devices = new HashSet<>();
141 devices.add(deviceId1);
142 devices.add(deviceId2);
143 assertTrue("Incorrect devices", ctrl.getNetconfDevices().containsAll(devices));
144 }
145
146 @Test
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530147 public void testGetNetconfDevice() {
148 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(deviceId1);
149 assertThat("Incorrect device fetched", fetchedDevice1, is(device1));
150
151 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(deviceId2);
152 assertThat("Incorrect device fetched", fetchedDevice2, is(device2));
153 }
154
155 @Test
156 public void testGetNetconfDeviceWithIPPort() {
157 NetconfDevice fetchedDevice1 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_1_IP), DEVICE_1_PORT);
158 assertEquals("Incorrect device fetched", fetchedDevice1.getDeviceInfo().ip(), device1.getDeviceInfo().ip());
159
160 NetconfDevice fetchedDevice2 = ctrl.getNetconfDevice(IpAddress.valueOf(DEVICE_2_IP), DEVICE_2_PORT);
161 assertEquals("Incorrect device fetched", fetchedDevice2.getDeviceInfo().ip(), device2.getDeviceInfo().ip());
162 }
163
164 /**
165 * Check for bad device connection. In this case the device map shouldn't get modified.
166 */
167 @Test(expected = NetconfException.class)
168 public void testConnectBadDevice() throws Exception {
169 reflectedDeviceMap.clear();
170 try {
171 ctrl.connectDevice(badDeviceInfo3);
172 } finally {
173 assertEquals("Incorrect device connection", 0, ctrl.getDevicesMap().size());
174 }
175 }
176
177 /**
178 * Check for correct device connection. In this case the device map get modified.
179 */
180 @Test
181 public void testConnectCorrectDevice() throws Exception {
182 reflectedDeviceMap.clear();
183 ctrl.connectDevice(deviceInfo1);
184 ctrl.connectDevice(deviceInfo2);
185 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId1));
186 assertTrue("Incorrect device connection", ctrl.getDevicesMap().containsKey(deviceId2));
187 assertEquals("Incorrect device connection", 2, ctrl.getDevicesMap().size());
188 }
189
190
191 /**
192 * Check for connect devices already added to the map.
193 */
194 @Test
195 public void testConnectAlreadyExistingDevice() throws Exception {
196 NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1);
197 NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2);
198 assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(),
199 deviceInfo1.getDeviceId());
200 assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(),
201 deviceInfo2.getDeviceId());
202 }
203
204 /**
Andrea Campanella86294db2016-03-07 11:42:49 -0800205 * Check that disconnectDevice actually disconnects the device and removes it.
206 */
207 @Test
208 public void testDisconnectDevice() throws Exception {
209 ctrl.disconnectDevice(deviceInfo1);
210 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
211 }
212
213 /**
214 * Checks that disconnectDevice actually disconnects the device and removes it.
Thanuj Ravindranath926e3162016-01-26 09:44:08 +0530215 */
216 @Test
217 public void testRemoveDevice() throws Exception {
218 ctrl.removeDevice(deviceInfo1);
219 assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1));
220 }
221
222 /**
223 * Test to get the connected device map.
224 */
225 @Test
226 public void testGetDevicesMap() {
227 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
228 }
229
230 /**
231 * Test to check whether the DeviceDownEventListener removes the device from the map when session
232 * for a particular device getting closed.
233 */
234 @Test
235 public void testDeviceDownEventListener() throws Exception {
236 reflectedDeviceMap.clear();
237 ctrl.connectDevice(deviceInfo1);
238 boolean result1 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
239 assertFalse("Irrelevant Device Event", result1);
240 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
241 reflectedDownListener.event(eventForDeviceInfo1);
242 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
243 ctrl.connectDevice(deviceInfo2);
244 boolean result2 = reflectedDownListener.isRelevant(eventForDeviceInfo2);
245 assertTrue("Irrelevant Device Event", result2);
246 assertEquals("Incorrect device map size", 2, ctrl.getDevicesMap().size());
247 reflectedDownListener.event(eventForDeviceInfo2);
248 assertEquals("Incorrect device map size", 1, ctrl.getDevicesMap().size());
249 }
250
251 /**
252 * Mock NetconfDeviceFactory class.
253 */
254 private class TestNetconfDeviceFactory implements NetconfDeviceFactory {
255
256 @Override
257 public NetconfDevice createNetconfDevice(NetconfDeviceInfo netconfDeviceInfo) throws NetconfException {
258 return new TestNetconfDevice(netconfDeviceInfo);
259 }
260 }
261
262 /**
263 * Mock NetconfDeviceImpl class, used for creating test devices.
264 */
265 protected class TestNetconfDevice implements NetconfDevice {
266 private NetconfDeviceInfo netconfDeviceInfo;
267 private boolean deviceState = false;
268 private NetconfSession netconfSession;
269
270 public TestNetconfDevice(NetconfDeviceInfo deviceInfo) throws NetconfException {
271 netconfDeviceInfo = deviceInfo;
272 if (netconfDeviceInfo.ip() != badDeviceInfo3.ip()) {
273 netconfSession = EasyMock.createMock(NetconfSession.class);
274 deviceState = true;
275 } else {
276 throw new NetconfException("Cannot create Connection and Session");
277 }
278 }
279
280 @Override
281 public boolean isActive() {
282 return deviceState;
283 }
284
285 @Override
286 public NetconfSession getSession() {
287 return netconfSession;
288 }
289
290 @Override
291 public void disconnect() {
292 deviceState = false;
293 netconfSession = null;
294 }
295
296 @Override
297 public NetconfDeviceInfo getDeviceInfo() {
298 return netconfDeviceInfo;
299 }
300
301 }
302}