Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.netconf.ctl; |
| 18 | |
| 19 | import org.easymock.EasyMock; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 25 | import org.onosproject.net.device.DeviceService; |
| 26 | import org.onosproject.net.key.DeviceKeyService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 27 | import org.onosproject.netconf.NetconfDevice; |
| 28 | import org.onosproject.netconf.NetconfDeviceFactory; |
| 29 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 30 | import org.onosproject.netconf.NetconfDeviceListener; |
| 31 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 32 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 33 | import org.onosproject.netconf.NetconfException; |
| 34 | import org.onosproject.netconf.NetconfSession; |
| 35 | |
| 36 | import java.lang.reflect.Field; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 37 | import java.util.HashSet; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | import java.util.Optional; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 40 | import java.util.Set; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 41 | |
| 42 | import static org.hamcrest.Matchers.*; |
| 43 | import static org.junit.Assert.*; |
| 44 | |
| 45 | /** |
| 46 | * Unit tests for the Netconf controller implementation test. |
| 47 | */ |
| 48 | public class NetconfControllerImplTest { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 49 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 50 | NetconfControllerImpl ctrl; |
| 51 | |
| 52 | //DeviceInfo |
| 53 | NetconfDeviceInfo deviceInfo1; |
| 54 | NetconfDeviceInfo deviceInfo2; |
| 55 | NetconfDeviceInfo badDeviceInfo3; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 56 | NetconfDeviceInfo deviceInfoIpV6; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 57 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 75 | private static final String DEVICE_IPV6 = "2001:db8::1"; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 76 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 80 | private static final int IPV6_DEVICE_PORT = 14; |
| 81 | |
| 82 | private static DeviceService deviceService = new NetconfDeviceServiceMock(); |
| 83 | private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock(); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 84 | |
| 85 | |
| 86 | @Before |
| 87 | public void setUp() throws Exception { |
| 88 | ctrl = new NetconfControllerImpl(); |
| 89 | ctrl.deviceFactory = new TestNetconfDeviceFactory(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 90 | ctrl.deviceService = deviceService; |
| 91 | ctrl.deviceKeyService = deviceKeyService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 92 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 97 | deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 98 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 117 | null, Optional.of(1), deviceInfo1); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 118 | eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 119 | null, Optional.of(2), deviceInfo2); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 120 | } |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 143 | deviceListener2, deviceListener3)); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 144 | |
| 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 Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 151 | 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 159 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 183 | ctrl.connectDevice(badDeviceInfo3.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 184 | } 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 195 | ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 196 | ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 197 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 202 | /** |
| 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 214 | |
| 215 | /** |
| 216 | * Check for connect devices already added to the map. |
| 217 | */ |
| 218 | @Test |
| 219 | public void testConnectAlreadyExistingDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 220 | NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 221 | NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 222 | assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 223 | deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 224 | assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 225 | deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /** |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 229 | * Check that disconnectDevice actually disconnects the device and removes it. |
| 230 | */ |
| 231 | @Test |
| 232 | public void testDisconnectDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 233 | ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 234 | assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Checks that disconnectDevice actually disconnects the device and removes it. |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 239 | */ |
| 240 | @Test |
| 241 | public void testRemoveDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 242 | ctrl.removeDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 243 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 261 | ctrl.connectDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 262 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 267 | ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 268 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame^] | 296 | if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) { |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 297 | 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 | } |