Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2016-present 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.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Iterators; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.junit.TestUtils; |
| 24 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.core.CoreServiceAdapter; |
| 27 | import org.onosproject.core.IdGenerator; |
| 28 | import org.onosproject.incubator.net.virtual.TenantId; |
| 29 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 30 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 31 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 32 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
| 33 | import org.onosproject.net.DefaultPort; |
| 34 | import org.onosproject.net.Device; |
| 35 | import org.onosproject.net.DeviceId; |
| 36 | import org.onosproject.net.MastershipRole; |
| 37 | import org.onosproject.net.NetTestTools; |
| 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.PortNumber; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.store.service.TestStorageService; |
| 42 | |
| 43 | import java.util.Iterator; |
| 44 | import java.util.concurrent.atomic.AtomicLong; |
| 45 | |
| 46 | import static org.junit.Assert.*; |
| 47 | |
| 48 | /** |
| 49 | * Junit tests for VirtualNetworkDeviceService. |
| 50 | */ |
| 51 | public class VirtualNetworkDeviceServiceTest { |
| 52 | private final String tenantIdValue1 = "TENANT_ID1"; |
| 53 | private final String deviceIdValue1 = "DEVICE_ID1"; |
| 54 | private final String deviceIdValue2 = "DEVICE_ID2"; |
| 55 | private final String deviceIdValue3 = "DEVICE_ID3"; |
| 56 | |
| 57 | private VirtualNetworkManager manager; |
| 58 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 59 | private CoreService coreService; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
| 63 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 64 | |
| 65 | coreService = new VirtualNetworkDeviceServiceTest.TestCoreService(); |
| 66 | virtualNetworkManagerStore.setCoreService(coreService); |
| 67 | TestUtils.setField(coreService, "coreService", new VirtualNetworkDeviceServiceTest.TestCoreService()); |
| 68 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 69 | virtualNetworkManagerStore.activate(); |
| 70 | |
| 71 | manager = new VirtualNetworkManager(); |
| 72 | manager.store = virtualNetworkManagerStore; |
| 73 | NetTestTools.injectEventDispatcher(manager, new TestEventDispatcher()); |
| 74 | manager.activate(); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() { |
| 79 | virtualNetworkManagerStore.deactivate(); |
| 80 | manager.deactivate(); |
| 81 | NetTestTools.injectEventDispatcher(manager, null); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Tests the getDevices(), getAvailableDevices(), getDeviceCount(), getDevice(), and isAvailable() methods. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testGetDevices() { |
| 89 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 90 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 91 | VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); |
| 92 | VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 93 | |
| 94 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 95 | |
| 96 | // test the getDevices() method |
| 97 | Iterator<Device> it = deviceService.getDevices().iterator(); |
| 98 | assertEquals("The device set size did not match.", 2, Iterators.size(it)); |
| 99 | |
| 100 | // test the getAvailableDevices() method |
| 101 | Iterator<Device> it2 = deviceService.getAvailableDevices().iterator(); |
| 102 | assertEquals("The device set size did not match.", 2, Iterators.size(it2)); |
| 103 | |
| 104 | // test the getDeviceCount() method |
| 105 | assertEquals("The device set size did not match.", 2, deviceService.getDeviceCount()); |
| 106 | |
| 107 | // test the getDevice() method |
| 108 | assertEquals("The expect device did not match.", device1, |
| 109 | deviceService.getDevice(DeviceId.deviceId(deviceIdValue1))); |
| 110 | assertNotEquals("The expect device should not have matched.", device1, |
| 111 | deviceService.getDevice(DeviceId.deviceId(deviceIdValue2))); |
| 112 | |
| 113 | // test the isAvailable() method |
| 114 | assertTrue("The expect device availability did not match.", |
| 115 | deviceService.isAvailable(DeviceId.deviceId(deviceIdValue1))); |
| 116 | assertFalse("The expect device availability did not match.", |
| 117 | deviceService.isAvailable(DeviceId.deviceId(deviceIdValue3))); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Tests querying for a device using a null device identifier. |
| 122 | */ |
| 123 | @Test(expected = NullPointerException.class) |
| 124 | public void testGetDeviceByNullId() { |
| 125 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 126 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 127 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 128 | |
| 129 | // test the getDevice() method with null device id value. |
| 130 | deviceService.getDevice(null); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Tests querying for a device using a null device type. |
| 135 | */ |
| 136 | @Test(expected = NullPointerException.class) |
| 137 | public void testGetDeviceByNullType() { |
| 138 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 139 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 140 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 141 | |
| 142 | // test the getDevices() method with null type value. |
| 143 | deviceService.getDevices(null); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Tests the isAvailable method using a null device identifier. |
| 148 | */ |
| 149 | @Test(expected = NullPointerException.class) |
| 150 | public void testIsAvailableByNullId() { |
| 151 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 152 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 153 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 154 | |
| 155 | // test the isAvailable() method with null device id value. |
| 156 | deviceService.isAvailable(null); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Tests querying for a device and available devices by device type. |
| 161 | */ |
| 162 | @Test |
| 163 | public void testGetDeviceType() { |
| 164 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 165 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 166 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue1)); |
| 167 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 168 | |
| 169 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 170 | |
| 171 | // test the getDevices(Type) method. |
| 172 | Iterator<Device> it = deviceService.getDevices(Device.Type.VIRTUAL).iterator(); |
| 173 | assertEquals("The device set size did not match.", 2, Iterators.size(it)); |
| 174 | Iterator<Device> it2 = deviceService.getDevices(Device.Type.SWITCH).iterator(); |
| 175 | assertEquals("The device set size did not match.", 0, Iterators.size(it2)); |
| 176 | |
| 177 | // test the getAvailableDevices(Type) method. |
| 178 | Iterator<Device> it3 = deviceService.getAvailableDevices(Device.Type.VIRTUAL).iterator(); |
| 179 | assertEquals("The device set size did not match.", 2, Iterators.size(it3)); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Tests querying the role of a device by null device identifier. |
| 184 | */ |
| 185 | @Test(expected = NullPointerException.class) |
| 186 | public void testGetRoleByNullId() { |
| 187 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 188 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 189 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 190 | |
| 191 | // test the getRole() method using a null device identifier |
| 192 | deviceService.getRole(null); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Tests querying the role of a device by device identifier. |
| 197 | */ |
| 198 | @Test |
| 199 | public void testGetRole() { |
| 200 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 201 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 202 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 203 | |
| 204 | // test the getRole() method |
| 205 | assertEquals("The expect device role did not match.", MastershipRole.MASTER, |
| 206 | deviceService.getRole(DeviceId.deviceId(deviceIdValue1))); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests querying the ports of a device by null device identifier. |
| 211 | */ |
| 212 | @Test(expected = NullPointerException.class) |
| 213 | public void testGetPortsByNullId() { |
| 214 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 215 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 216 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 217 | |
| 218 | // test the getPorts() method using a null device identifier |
| 219 | deviceService.getPorts(null); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Tests querying the ports of a device by device identifier. |
| 224 | */ |
| 225 | @Test |
| 226 | public void testGetPorts() { |
| 227 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 228 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 229 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), |
| 230 | DeviceId.deviceId(deviceIdValue1)); |
| 231 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 232 | |
| 233 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 234 | |
| 235 | Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true); |
| 236 | |
| 237 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(1), port); |
| 238 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port); |
| 239 | |
| 240 | // test the getPorts() method |
| 241 | assertEquals("The port set size did not match.", 2, |
| 242 | deviceService.getPorts(DeviceId.deviceId(deviceIdValue1)).size()); |
| 243 | assertEquals("The port set size did not match.", 0, |
| 244 | deviceService.getPorts(DeviceId.deviceId(deviceIdValue2)).size()); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Tests querying the port of a device by device identifier and port number. |
| 249 | */ |
| 250 | @Test |
| 251 | public void testGetPort() { |
| 252 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 253 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 254 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), |
| 255 | DeviceId.deviceId(deviceIdValue1)); |
| 256 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 257 | |
| 258 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 259 | |
| 260 | Port port = new DefaultPort(virtualDevice, PortNumber.portNumber(1), true); |
| 261 | |
| 262 | VirtualPort virtualPort1 = manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), |
| 263 | PortNumber.portNumber(1), port); |
| 264 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), port); |
| 265 | |
| 266 | // test the getPort() method |
| 267 | assertEquals("The port did not match as expected.", virtualPort1, |
| 268 | deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(1))); |
| 269 | assertNotEquals("The port did not match as expected.", virtualPort1, |
| 270 | deviceService.getPort(DeviceId.deviceId(deviceIdValue1), PortNumber.portNumber(3))); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Tests querying the port statistics of a device by null device identifier. |
| 275 | */ |
| 276 | @Test(expected = NullPointerException.class) |
| 277 | public void testGetPortsStatisticsByNullId() { |
| 278 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 279 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 280 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 281 | |
| 282 | // test the getPortStatistics() method using a null device identifier |
| 283 | deviceService.getPortStatistics(null); |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Tests querying the port statistics of a device by device identifier. |
| 288 | */ |
| 289 | @Test |
| 290 | public void testGetPortStatistics() { |
| 291 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 292 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 293 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), |
| 294 | DeviceId.deviceId(deviceIdValue1)); |
| 295 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 296 | |
| 297 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 298 | |
| 299 | // test the getPortStatistics() method |
| 300 | assertEquals("The port statistics set size did not match.", 0, |
| 301 | deviceService.getPortStatistics(DeviceId.deviceId(deviceIdValue1)).size()); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Tests querying the port delta statistics of a device by null device identifier. |
| 306 | */ |
| 307 | @Test(expected = NullPointerException.class) |
| 308 | public void testGetPortsDeltaStatisticsByNullId() { |
| 309 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 310 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 311 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 312 | |
| 313 | // test the getPortDeltaStatistics() method using a null device identifier |
| 314 | deviceService.getPortDeltaStatistics(null); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Tests querying the port delta statistics of a device by device identifier. |
| 319 | */ |
| 320 | @Test |
| 321 | public void testGetPortDeltaStatistics() { |
| 322 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 323 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 324 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), |
| 325 | DeviceId.deviceId(deviceIdValue1)); |
| 326 | manager.createVirtualDevice(virtualNetwork.id(), DeviceId.deviceId(deviceIdValue2)); |
| 327 | |
| 328 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 329 | |
| 330 | // test the getPortDeltaStatistics() method |
| 331 | assertEquals("The port delta statistics set size did not match.", 0, |
| 332 | deviceService.getPortDeltaStatistics(DeviceId.deviceId(deviceIdValue1)).size()); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Core service test class. |
| 337 | */ |
| 338 | private class TestCoreService extends CoreServiceAdapter { |
| 339 | |
| 340 | @Override |
| 341 | public IdGenerator getIdGenerator(String topic) { |
| 342 | return new IdGenerator() { |
| 343 | private AtomicLong counter = new AtomicLong(0); |
| 344 | |
| 345 | @Override |
| 346 | public long getNewId() { |
| 347 | return counter.getAndIncrement(); |
| 348 | } |
| 349 | }; |
| 350 | } |
| 351 | } |
| 352 | } |