Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [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.incubator.net.virtual.impl; |
| 18 | |
| 19 | import com.google.common.collect.Iterators; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 24 | import org.onlab.junit.TestTools; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 25 | import org.onlab.junit.TestUtils; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 26 | import org.onlab.osgi.TestServiceDirectory; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 27 | import org.onosproject.common.event.impl.TestEventDispatcher; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.IdGenerator; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.event.EventDeliveryService; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 33 | import org.onosproject.incubator.net.virtual.TenantId; |
| 34 | import org.onosproject.incubator.net.virtual.VirtualDevice; |
| 35 | import org.onosproject.incubator.net.virtual.VirtualNetwork; |
| 36 | import org.onosproject.incubator.net.virtual.VirtualPort; |
| 37 | import org.onosproject.incubator.store.virtual.impl.DistributedVirtualNetworkStore; |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 38 | import org.onosproject.net.ConnectPoint; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 39 | import org.onosproject.net.Device; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 40 | import org.onosproject.net.DeviceId; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 41 | import org.onosproject.net.MastershipRole; |
| 42 | import org.onosproject.net.NetTestTools; |
Claudine Chiu | 579969d | 2017-10-06 14:32:18 -0400 | [diff] [blame] | 43 | import org.onosproject.net.Port; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 44 | import org.onosproject.net.PortNumber; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 45 | import org.onosproject.net.device.DeviceEvent; |
| 46 | import org.onosproject.net.device.DeviceListener; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 47 | import org.onosproject.net.device.DeviceService; |
| 48 | import org.onosproject.store.service.TestStorageService; |
| 49 | |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 50 | import java.util.ArrayList; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 51 | import java.util.Iterator; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 52 | import java.util.List; |
| 53 | import java.util.Set; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 54 | import java.util.concurrent.atomic.AtomicLong; |
| 55 | |
| 56 | import static org.junit.Assert.*; |
| 57 | |
| 58 | /** |
| 59 | * Junit tests for VirtualNetworkDeviceService. |
| 60 | */ |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 61 | public class VirtualNetworkDeviceManagerTest extends VirtualNetworkTestUtil { |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 62 | private final String tenantIdValue1 = "TENANT_ID1"; |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 63 | |
| 64 | private VirtualNetworkManager manager; |
| 65 | private DistributedVirtualNetworkStore virtualNetworkManagerStore; |
| 66 | private CoreService coreService; |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 67 | private TestServiceDirectory testDirectory; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 68 | private TestListener testListener = new TestListener(); |
| 69 | private TestEventDispatcher dispatcher = new TestEventDispatcher(); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 70 | |
| 71 | @Before |
| 72 | public void setUp() throws Exception { |
| 73 | virtualNetworkManagerStore = new DistributedVirtualNetworkStore(); |
| 74 | |
yoonseon | 214963b | 2016-11-21 15:41:07 -0800 | [diff] [blame] | 75 | coreService = new VirtualNetworkDeviceManagerTest.TestCoreService(); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 76 | TestUtils.setField(virtualNetworkManagerStore, "coreService", coreService); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 77 | TestUtils.setField(virtualNetworkManagerStore, "storageService", new TestStorageService()); |
| 78 | virtualNetworkManagerStore.activate(); |
| 79 | |
| 80 | manager = new VirtualNetworkManager(); |
| 81 | manager.store = virtualNetworkManagerStore; |
yoonseon | 322c9c3 | 2016-12-07 16:47:02 -0800 | [diff] [blame] | 82 | manager.coreService = coreService; |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 83 | NetTestTools.injectEventDispatcher(manager, dispatcher); |
yoonseon | c6a6927 | 2017-01-12 18:22:20 -0800 | [diff] [blame] | 84 | |
| 85 | testDirectory = new TestServiceDirectory(); |
| 86 | TestUtils.setField(manager, "serviceDirectory", testDirectory); |
| 87 | |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 88 | manager.activate(); |
| 89 | } |
| 90 | |
| 91 | @After |
| 92 | public void tearDown() { |
| 93 | virtualNetworkManagerStore.deactivate(); |
| 94 | manager.deactivate(); |
| 95 | NetTestTools.injectEventDispatcher(manager, null); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Tests the getDevices(), getAvailableDevices(), getDeviceCount(), getDevice(), and isAvailable() methods. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testGetDevices() { |
| 103 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 104 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 105 | VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 106 | VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 107 | |
| 108 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 109 | |
| 110 | // test the getDevices() method |
| 111 | Iterator<Device> it = deviceService.getDevices().iterator(); |
| 112 | assertEquals("The device set size did not match.", 2, Iterators.size(it)); |
| 113 | |
| 114 | // test the getAvailableDevices() method |
| 115 | Iterator<Device> it2 = deviceService.getAvailableDevices().iterator(); |
| 116 | assertEquals("The device set size did not match.", 2, Iterators.size(it2)); |
| 117 | |
| 118 | // test the getDeviceCount() method |
| 119 | assertEquals("The device set size did not match.", 2, deviceService.getDeviceCount()); |
| 120 | |
| 121 | // test the getDevice() method |
| 122 | assertEquals("The expect device did not match.", device1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 123 | deviceService.getDevice(DID1)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 124 | assertNotEquals("The expect device should not have matched.", device1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 125 | deviceService.getDevice(DID2)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 126 | |
| 127 | // test the isAvailable() method |
| 128 | assertTrue("The expect device availability did not match.", |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 129 | deviceService.isAvailable(DID1)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 130 | assertFalse("The expect device availability did not match.", |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 131 | deviceService.isAvailable(DID3)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Tests querying for a device using a null device identifier. |
| 136 | */ |
| 137 | @Test(expected = NullPointerException.class) |
| 138 | public void testGetDeviceByNullId() { |
| 139 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 140 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 141 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 142 | |
| 143 | // test the getDevice() method with null device id value. |
| 144 | deviceService.getDevice(null); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests querying for a device using a null device type. |
| 149 | */ |
| 150 | @Test(expected = NullPointerException.class) |
| 151 | public void testGetDeviceByNullType() { |
| 152 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 153 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 154 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 155 | |
| 156 | // test the getDevices() method with null type value. |
| 157 | deviceService.getDevices(null); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests the isAvailable method using a null device identifier. |
| 162 | */ |
| 163 | @Test(expected = NullPointerException.class) |
| 164 | public void testIsAvailableByNullId() { |
| 165 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 166 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 167 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 168 | |
| 169 | // test the isAvailable() method with null device id value. |
| 170 | deviceService.isAvailable(null); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Tests querying for a device and available devices by device type. |
| 175 | */ |
| 176 | @Test |
| 177 | public void testGetDeviceType() { |
| 178 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 179 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 180 | manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 181 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 182 | |
| 183 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 184 | |
| 185 | // test the getDevices(Type) method. |
| 186 | Iterator<Device> it = deviceService.getDevices(Device.Type.VIRTUAL).iterator(); |
| 187 | assertEquals("The device set size did not match.", 2, Iterators.size(it)); |
| 188 | Iterator<Device> it2 = deviceService.getDevices(Device.Type.SWITCH).iterator(); |
| 189 | assertEquals("The device set size did not match.", 0, Iterators.size(it2)); |
| 190 | |
| 191 | // test the getAvailableDevices(Type) method. |
| 192 | Iterator<Device> it3 = deviceService.getAvailableDevices(Device.Type.VIRTUAL).iterator(); |
| 193 | assertEquals("The device set size did not match.", 2, Iterators.size(it3)); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Tests querying the role of a device by null device identifier. |
| 198 | */ |
| 199 | @Test(expected = NullPointerException.class) |
| 200 | public void testGetRoleByNullId() { |
| 201 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 202 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 203 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 204 | |
| 205 | // test the getRole() method using a null device identifier |
| 206 | deviceService.getRole(null); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests querying the role of a device by device identifier. |
| 211 | */ |
| 212 | @Test |
| 213 | public void testGetRole() { |
| 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 getRole() method |
| 219 | assertEquals("The expect device role did not match.", MastershipRole.MASTER, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 220 | deviceService.getRole(DID1)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Tests querying the ports of a device by null device identifier. |
| 225 | */ |
| 226 | @Test(expected = NullPointerException.class) |
| 227 | public void testGetPortsByNullId() { |
| 228 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 229 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 230 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 231 | |
| 232 | // test the getPorts() method using a null device identifier |
| 233 | deviceService.getPorts(null); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Tests querying the ports of a device by device identifier. |
| 238 | */ |
| 239 | @Test |
| 240 | public void testGetPorts() { |
| 241 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 242 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 243 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 244 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 245 | |
| 246 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 247 | |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 248 | ConnectPoint cp = new ConnectPoint(virtualDevice.id(), PortNumber.portNumber(1)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 249 | |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 250 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(1), cp); |
| 251 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), cp); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 252 | |
| 253 | // test the getPorts() method |
| 254 | assertEquals("The port set size did not match.", 2, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 255 | deviceService.getPorts(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 256 | assertEquals("The port set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 257 | deviceService.getPorts(DID2).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Tests querying the port of a device by device identifier and port number. |
| 262 | */ |
| 263 | @Test |
| 264 | public void testGetPort() { |
| 265 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 266 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 267 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 268 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 269 | |
| 270 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 271 | |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 272 | ConnectPoint cp = new ConnectPoint(virtualDevice.id(), PortNumber.portNumber(1)); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 273 | |
| 274 | VirtualPort virtualPort1 = manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), |
Yoonseon Han | 6c60389 | 2016-09-01 11:52:21 -0700 | [diff] [blame] | 275 | PortNumber.portNumber(1), cp); |
| 276 | manager.createVirtualPort(virtualNetwork.id(), virtualDevice.id(), PortNumber.portNumber(2), cp); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 277 | |
| 278 | // test the getPort() method |
| 279 | assertEquals("The port did not match as expected.", virtualPort1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 280 | deviceService.getPort(DID1, PortNumber.portNumber(1))); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 281 | assertNotEquals("The port did not match as expected.", virtualPort1, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 282 | deviceService.getPort(DID1, PortNumber.portNumber(3))); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Tests querying the port statistics of a device by null device identifier. |
| 287 | */ |
| 288 | @Test(expected = NullPointerException.class) |
| 289 | public void testGetPortsStatisticsByNullId() { |
| 290 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 291 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 292 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 293 | |
| 294 | // test the getPortStatistics() method using a null device identifier |
| 295 | deviceService.getPortStatistics(null); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Tests querying the port statistics of a device by device identifier. |
| 300 | */ |
| 301 | @Test |
| 302 | public void testGetPortStatistics() { |
| 303 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 304 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 305 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 306 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 307 | |
| 308 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 309 | |
| 310 | // test the getPortStatistics() method |
| 311 | assertEquals("The port statistics set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 312 | deviceService.getPortStatistics(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Tests querying the port delta statistics of a device by null device identifier. |
| 317 | */ |
| 318 | @Test(expected = NullPointerException.class) |
| 319 | public void testGetPortsDeltaStatisticsByNullId() { |
| 320 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 321 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 322 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 323 | |
| 324 | // test the getPortDeltaStatistics() method using a null device identifier |
| 325 | deviceService.getPortDeltaStatistics(null); |
| 326 | } |
| 327 | |
| 328 | /** |
| 329 | * Tests querying the port delta statistics of a device by device identifier. |
| 330 | */ |
| 331 | @Test |
| 332 | public void testGetPortDeltaStatistics() { |
| 333 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 334 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 335 | VirtualDevice virtualDevice = manager.createVirtualDevice(virtualNetwork.id(), DID1); |
| 336 | manager.createVirtualDevice(virtualNetwork.id(), DID2); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 337 | |
| 338 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 339 | |
| 340 | // test the getPortDeltaStatistics() method |
| 341 | assertEquals("The port delta statistics set size did not match.", 0, |
Brian Stanke | 8e9f8d1 | 2016-06-08 14:48:33 -0400 | [diff] [blame] | 342 | deviceService.getPortDeltaStatistics(DID1).size()); |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 346 | * Tests DeviceEvents received during virtual device/port addition and removal. |
| 347 | */ |
| 348 | @Test |
| 349 | public void testDeviceEventsForAddRemovalDeviceAndPorts() throws TestUtils.TestUtilsException { |
| 350 | manager.registerTenantId(TenantId.tenantId(tenantIdValue1)); |
| 351 | VirtualNetwork virtualNetwork = manager.createVirtualNetwork(TenantId.tenantId(tenantIdValue1)); |
| 352 | |
| 353 | // add virtual device before virtual device manager is created |
| 354 | VirtualDevice device1 = manager.createVirtualDevice(virtualNetwork.id(), VDID1); |
| 355 | validateEvents(); // no DeviceEvent expected |
| 356 | |
| 357 | testDirectory.add(EventDeliveryService.class, dispatcher); |
| 358 | DeviceService deviceService = manager.get(virtualNetwork.id(), DeviceService.class); |
| 359 | |
| 360 | // virtual device manager is created; register DeviceEvent listener |
| 361 | deviceService.addListener(testListener); |
| 362 | |
| 363 | // list to keep track of expected event types |
| 364 | List<DeviceEvent.Type> expectedEventTypes = new ArrayList<>(); |
| 365 | |
| 366 | // add virtual device |
| 367 | VirtualDevice device2 = manager.createVirtualDevice(virtualNetwork.id(), VDID2); |
| 368 | expectedEventTypes.add(DeviceEvent.Type.DEVICE_ADDED); |
| 369 | |
| 370 | ConnectPoint cp = new ConnectPoint(PHYDID1, PortNumber.portNumber(1)); |
| 371 | |
| 372 | // add 2 virtual ports |
| 373 | manager.createVirtualPort(virtualNetwork.id(), |
| 374 | device2.id(), PortNumber.portNumber(1), cp); |
| 375 | expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED); |
| 376 | manager.createVirtualPort(virtualNetwork.id(), |
| 377 | device2.id(), PortNumber.portNumber(2), cp); |
| 378 | expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED); |
| 379 | |
| 380 | // verify virtual ports were added |
| 381 | Set<VirtualPort> virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id()); |
| 382 | assertNotNull("The virtual port set should not be null", virtualPorts); |
| 383 | assertEquals("The virtual port set size did not match.", 2, virtualPorts.size()); |
Claudine Chiu | 579969d | 2017-10-06 14:32:18 -0400 | [diff] [blame] | 384 | virtualPorts.forEach(vp -> assertFalse("Initial virtual port state should be disabled", vp.isEnabled())); |
| 385 | |
| 386 | // verify change state of virtual port (disabled -> enabled) |
| 387 | manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(1), true); |
| 388 | Port changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(1)); |
| 389 | assertNotNull("The changed virtual port should not be null", changedPort); |
| 390 | assertEquals("Virtual port state should be enabled", true, changedPort.isEnabled()); |
| 391 | expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED); |
| 392 | |
| 393 | // verify change state of virtual port (disabled -> disabled) |
| 394 | manager.updatePortState(virtualNetwork.id(), device2.id(), PortNumber.portNumber(2), false); |
| 395 | changedPort = deviceService.getPort(device2.id(), PortNumber.portNumber(2)); |
| 396 | assertNotNull("The changed virtual port should not be null", changedPort); |
| 397 | assertEquals("Virtual port state should be disabled", false, changedPort.isEnabled()); |
| 398 | // no VIRTUAL_PORT_UPDATED event is expected - the requested state (disabled) is same as previous state. |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 399 | |
| 400 | // remove 2 virtual ports |
| 401 | for (VirtualPort virtualPort : virtualPorts) { |
| 402 | manager.removeVirtualPort(virtualNetwork.id(), |
| 403 | (DeviceId) virtualPort.element().id(), virtualPort.number()); |
| 404 | expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED); |
| 405 | // attempt to remove the same virtual port again - no DeviceEvent.Type.PORT_REMOVED expected. |
| 406 | manager.removeVirtualPort(virtualNetwork.id(), |
| 407 | (DeviceId) virtualPort.element().id(), virtualPort.number()); |
| 408 | } |
| 409 | |
| 410 | // verify virtual ports were removed |
| 411 | virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id()); |
| 412 | assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty()); |
| 413 | |
| 414 | // Add/remove one virtual port again. |
| 415 | VirtualPort virtualPort = |
| 416 | manager.createVirtualPort(virtualNetwork.id(), device2.id(), |
| 417 | PortNumber.portNumber(1), cp); |
| 418 | expectedEventTypes.add(DeviceEvent.Type.PORT_ADDED); |
| 419 | |
| 420 | ConnectPoint newCp = new ConnectPoint(PHYDID3, PortNumber.portNumber(2)); |
| 421 | manager.bindVirtualPort(virtualNetwork.id(), device2.id(), |
| 422 | PortNumber.portNumber(1), newCp); |
| 423 | expectedEventTypes.add(DeviceEvent.Type.PORT_UPDATED); |
| 424 | |
| 425 | manager.removeVirtualPort(virtualNetwork.id(), |
| 426 | (DeviceId) virtualPort.element().id(), virtualPort.number()); |
| 427 | expectedEventTypes.add(DeviceEvent.Type.PORT_REMOVED); |
| 428 | |
| 429 | // verify no virtual ports remain |
| 430 | virtualPorts = manager.getVirtualPorts(virtualNetwork.id(), device2.id()); |
| 431 | assertTrue("The virtual port set should be empty.", virtualPorts.isEmpty()); |
| 432 | |
| 433 | // remove virtual device |
| 434 | manager.removeVirtualDevice(virtualNetwork.id(), device2.id()); |
| 435 | expectedEventTypes.add(DeviceEvent.Type.DEVICE_REMOVED); |
| 436 | |
| 437 | // Validate that the events were all received in the correct order. |
| 438 | validateEvents((Enum[]) expectedEventTypes.toArray( |
| 439 | new DeviceEvent.Type[expectedEventTypes.size()])); |
| 440 | |
| 441 | // cleanup |
| 442 | deviceService.removeListener(testListener); |
| 443 | } |
| 444 | |
| 445 | /** |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 446 | * Core service test class. |
| 447 | */ |
| 448 | private class TestCoreService extends CoreServiceAdapter { |
| 449 | |
| 450 | @Override |
| 451 | public IdGenerator getIdGenerator(String topic) { |
| 452 | return new IdGenerator() { |
| 453 | private AtomicLong counter = new AtomicLong(0); |
| 454 | |
| 455 | @Override |
| 456 | public long getNewId() { |
| 457 | return counter.getAndIncrement(); |
| 458 | } |
| 459 | }; |
| 460 | } |
| 461 | } |
Claudine Chiu | 465a260 | 2017-03-17 18:33:36 -0400 | [diff] [blame] | 462 | |
| 463 | /** |
| 464 | * Method to validate that the actual versus expected virtual network events were |
| 465 | * received correctly. |
| 466 | * |
| 467 | * @param types expected virtual network events. |
| 468 | */ |
| 469 | private void validateEvents(Enum... types) { |
| 470 | TestTools.assertAfter(100, () -> { |
| 471 | int i = 0; |
| 472 | assertEquals("wrong events received", types.length, testListener.events.size()); |
| 473 | for (Event event : testListener.events) { |
| 474 | assertEquals("incorrect event type", types[i], event.type()); |
| 475 | i++; |
| 476 | } |
| 477 | testListener.events.clear(); |
| 478 | }); |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * Test listener class to receive device events. |
| 483 | */ |
| 484 | private static class TestListener implements DeviceListener { |
| 485 | |
| 486 | private List<DeviceEvent> events = Lists.newArrayList(); |
| 487 | |
| 488 | @Override |
| 489 | public void event(DeviceEvent event) { |
| 490 | events.add(event); |
| 491 | } |
| 492 | } |
Brian Stanke | 8691428 | 2016-05-25 15:36:50 -0400 | [diff] [blame] | 493 | } |