Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 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; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 23 | import org.onlab.osgi.ComponentContextAdapter; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 24 | import org.onlab.packet.IpAddress; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 25 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 26 | import org.onosproject.cfg.ComponentConfigService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceService; |
| 29 | import org.onosproject.net.key.DeviceKeyService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 30 | import org.onosproject.netconf.NetconfDevice; |
| 31 | import org.onosproject.netconf.NetconfDeviceFactory; |
| 32 | import org.onosproject.netconf.NetconfDeviceInfo; |
| 33 | import org.onosproject.netconf.NetconfDeviceListener; |
| 34 | import org.onosproject.netconf.NetconfDeviceOutputEvent; |
| 35 | import org.onosproject.netconf.NetconfDeviceOutputEventListener; |
| 36 | import org.onosproject.netconf.NetconfException; |
| 37 | import org.onosproject.netconf.NetconfSession; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 38 | import org.osgi.service.component.ComponentContext; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 39 | |
| 40 | import java.lang.reflect.Field; |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 41 | import java.util.Dictionary; |
| 42 | import java.util.Enumeration; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 43 | import java.util.HashSet; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 44 | import java.util.Map; |
| 45 | import java.util.Optional; |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 46 | import java.util.Set; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 47 | |
| 48 | import static org.hamcrest.Matchers.*; |
| 49 | import static org.junit.Assert.*; |
| 50 | |
| 51 | /** |
| 52 | * Unit tests for the Netconf controller implementation test. |
| 53 | */ |
| 54 | public class NetconfControllerImplTest { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 55 | |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 56 | NetconfControllerImpl ctrl; |
| 57 | |
| 58 | //DeviceInfo |
| 59 | NetconfDeviceInfo deviceInfo1; |
| 60 | NetconfDeviceInfo deviceInfo2; |
| 61 | NetconfDeviceInfo badDeviceInfo3; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 62 | NetconfDeviceInfo deviceInfoIpV6; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 63 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 81 | private static final String DEVICE_IPV6 = "2001:db8::1"; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 82 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 86 | private static final int IPV6_DEVICE_PORT = 14; |
| 87 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 88 | private static ComponentConfigService cfgService = new ComponentConfigAdapter(); |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 89 | private static DeviceService deviceService = new NetconfDeviceServiceMock(); |
| 90 | private static DeviceKeyService deviceKeyService = new NetconfDeviceKeyServiceMock(); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 91 | |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 92 | private final ComponentContext context = new MockComponentContext(); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 93 | |
| 94 | @Before |
| 95 | public void setUp() throws Exception { |
| 96 | ctrl = new NetconfControllerImpl(); |
| 97 | ctrl.deviceFactory = new TestNetconfDeviceFactory(); |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 98 | ctrl.cfgService = cfgService; |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 99 | ctrl.deviceService = deviceService; |
| 100 | ctrl.deviceKeyService = deviceKeyService; |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 101 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 106 | deviceInfoIpV6 = new NetconfDeviceInfo("deviceIpv6", "004", IpAddress.valueOf(DEVICE_IPV6), IPV6_DEVICE_PORT); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 107 | |
| 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 126 | null, Optional.of(1), deviceInfo1); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 127 | eventForDeviceInfo2 = new NetconfDeviceOutputEvent(NetconfDeviceOutputEvent.Type.DEVICE_UNREGISTERED, null, |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 128 | null, Optional.of(2), deviceInfo2); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | @After |
| 132 | public void tearDown() { |
| 133 | ctrl.deactivate(); |
| 134 | } |
| 135 | |
| 136 | /** |
Andreas Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 137 | * 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 161 | * 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 176 | deviceListener2, deviceListener3)); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 177 | |
| 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 Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 184 | 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 192 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 216 | ctrl.connectDevice(badDeviceInfo3.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 217 | } 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 228 | ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 229 | ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 230 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 235 | /** |
| 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 247 | |
| 248 | /** |
| 249 | * Check for connect devices already added to the map. |
| 250 | */ |
| 251 | @Test |
| 252 | public void testConnectAlreadyExistingDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 253 | NetconfDevice alreadyExistingDevice1 = ctrl.connectDevice(deviceInfo1.getDeviceId()); |
| 254 | NetconfDevice alreadyExistingDevice2 = ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 255 | assertEquals("Incorrect device connection", alreadyExistingDevice1.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 256 | deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 257 | assertEquals("Incorrect device connection", alreadyExistingDevice2.getDeviceInfo().getDeviceId(), |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 258 | deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 262 | * Check that disconnectDevice actually disconnects the device and removes it. |
| 263 | */ |
| 264 | @Test |
| 265 | public void testDisconnectDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 266 | ctrl.disconnectDevice(deviceInfo1.getDeviceId(), true); |
Andrea Campanella | 86294db | 2016-03-07 11:42:49 -0800 | [diff] [blame] | 267 | assertFalse("Incorrect device removal", ctrl.getDevicesMap().containsKey(deviceId1)); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Checks that disconnectDevice actually disconnects the device and removes it. |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 272 | */ |
| 273 | @Test |
| 274 | public void testRemoveDevice() throws Exception { |
Andrea Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 275 | ctrl.removeDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 276 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 294 | ctrl.connectDevice(deviceInfo1.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 295 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 300 | ctrl.connectDevice(deviceInfo2.getDeviceId()); |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 301 | 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 Campanella | 7e6200a | 2016-03-21 09:48:40 -0700 | [diff] [blame] | 329 | if (!badDeviceInfo3.getDeviceId().equals(deviceInfo.getDeviceId())) { |
Thanuj Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 330 | 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 Papazois | 4752cfa | 2016-04-25 14:52:12 +0300 | [diff] [blame] | 359 | |
| 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 Ravindranath | 926e316 | 2016-01-26 09:44:08 +0530 | [diff] [blame] | 407 | } |