Jian Li | fae7b38 | 2018-07-12 22:27:47 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Lists; |
| 20 | import com.google.common.util.concurrent.MoreExecutors; |
| 21 | import org.junit.After; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.junit.TestUtils; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onosproject.core.ApplicationId; |
| 29 | import org.onosproject.core.CoreServiceAdapter; |
| 30 | import org.onosproject.core.DefaultApplicationId; |
| 31 | import org.onosproject.event.Event; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.DefaultHost; |
| 34 | import org.onosproject.net.DeviceId; |
| 35 | import org.onosproject.net.Host; |
| 36 | import org.onosproject.net.HostId; |
| 37 | import org.onosproject.net.HostLocation; |
| 38 | import org.onosproject.net.PortNumber; |
| 39 | import org.onosproject.net.host.HostServiceAdapter; |
| 40 | import org.onosproject.net.provider.ProviderId; |
| 41 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 42 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 43 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
| 44 | import org.onosproject.store.service.TestStorageService; |
| 45 | |
| 46 | import java.util.List; |
| 47 | import java.util.Set; |
| 48 | |
| 49 | import static org.junit.Assert.assertEquals; |
| 50 | import static org.junit.Assert.assertNotNull; |
| 51 | import static org.junit.Assert.assertNull; |
| 52 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
| 53 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
| 54 | import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATING; |
| 55 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_ENDED; |
| 56 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_STARTED; |
| 57 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_DETECTED; |
| 58 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_UPDATED; |
| 59 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_VANISHED; |
| 60 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_RESTARTED; |
| 61 | import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_TERMINATED; |
| 62 | import static org.onosproject.openstacknetworking.impl.HostBasedInstancePort.ANNOTATION_CREATE_TIME; |
| 63 | |
| 64 | /** |
| 65 | * Unit tests for instance port manager. |
| 66 | */ |
| 67 | public class InstancePortManagerTest { |
| 68 | |
| 69 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 70 | |
| 71 | private static final String ANNOTATION_NETWORK_ID = "networkId"; |
| 72 | private static final String ANNOTATION_PORT_ID = "portId"; |
| 73 | |
| 74 | private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("1.2.3.4"); |
| 75 | private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("5.6.7.8"); |
| 76 | |
| 77 | private static final MacAddress MAC_ADDRESS_1 = MacAddress.valueOf("11:22:33:44:55:66"); |
| 78 | private static final MacAddress MAC_ADDRESS_2 = MacAddress.valueOf("77:88:99:AA:BB:CC"); |
| 79 | |
| 80 | private static final DeviceId DEV_ID_1 = DeviceId.deviceId("of:0000000000000001"); |
| 81 | private static final DeviceId DEV_ID_2 = DeviceId.deviceId("of:0000000000000002"); |
| 82 | private static final PortNumber PORT_NUM_1 = PortNumber.portNumber(1L); |
| 83 | private static final PortNumber PORT_NUM_2 = PortNumber.portNumber(2L); |
| 84 | |
| 85 | private static final VlanId VLAN_ID = VlanId.vlanId(); |
| 86 | private static final ProviderId PROVIDER_ID = ProviderId.NONE; |
| 87 | private static final HostId HOST_ID_1 = HostId.hostId("00:00:11:00:00:01/1"); |
| 88 | private static final HostId HOST_ID_2 = HostId.hostId("00:00:11:00:00:02/1"); |
| 89 | |
| 90 | private static final String NETWORK_ID_1 = "net-id-1"; |
| 91 | private static final String NETWORK_ID_2 = "net-id-2"; |
| 92 | private static final String PORT_ID_1 = "port-id-1"; |
| 93 | private static final String PORT_ID_2 = "port-id-2"; |
| 94 | private static final String UNKNOWN_ID = "port-id-x"; |
| 95 | |
| 96 | private static final long TIME_1 = 1L; |
| 97 | private static final long TIME_2 = 2L; |
| 98 | |
| 99 | private InstancePort instancePort1; |
| 100 | private InstancePort instancePort2; |
| 101 | |
| 102 | private InstancePortManager target; |
| 103 | private DistributedInstancePortStore store; |
| 104 | |
| 105 | private final TestInstancePortListener testInstancePortListener = new TestInstancePortListener(); |
| 106 | |
| 107 | /** |
| 108 | * Initial setup for this unit test. |
| 109 | */ |
| 110 | @Before |
| 111 | public void setUp() { |
| 112 | |
| 113 | store = new DistributedInstancePortStore(); |
| 114 | TestUtils.setField(store, "coreService", new TestCoreService()); |
| 115 | TestUtils.setField(store, "storageService", new TestStorageService()); |
| 116 | TestUtils.setField(store, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
| 117 | store.activate(); |
| 118 | |
| 119 | target = new InstancePortManager(); |
| 120 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 121 | TestUtils.setField(target, "hostService", new TestHostService()); |
| 122 | target.instancePortStore = store; |
| 123 | target.addListener(testInstancePortListener); |
| 124 | target.activate(); |
| 125 | |
| 126 | HostLocation location1 = new HostLocation(DEV_ID_1, PORT_NUM_1, TIME_1); |
| 127 | HostLocation location2 = new HostLocation(DEV_ID_2, PORT_NUM_2, TIME_2); |
| 128 | |
| 129 | DefaultAnnotations.Builder annotations1 = DefaultAnnotations.builder() |
| 130 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID_1) |
| 131 | .set(ANNOTATION_PORT_ID, PORT_ID_1) |
| 132 | .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_1)); |
| 133 | |
| 134 | DefaultAnnotations.Builder annotations2 = DefaultAnnotations.builder() |
| 135 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID_2) |
| 136 | .set(ANNOTATION_PORT_ID, PORT_ID_2) |
| 137 | .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_2)); |
| 138 | |
| 139 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_1, MAC_ADDRESS_1, |
| 140 | VLAN_ID, location1, ImmutableSet.of(IP_ADDRESS_1), |
| 141 | annotations1.build()); |
| 142 | Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_2, MAC_ADDRESS_2, |
| 143 | VLAN_ID, location2, ImmutableSet.of(IP_ADDRESS_2), |
| 144 | annotations2.build()); |
| 145 | |
| 146 | instancePort1 = DefaultInstancePort.from(host1, ACTIVE); |
| 147 | instancePort2 = DefaultInstancePort.from(host2, INACTIVE); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Tears down all of this unit test. |
| 152 | */ |
| 153 | @After |
| 154 | public void tearDown() { |
| 155 | target.removeListener(testInstancePortListener); |
| 156 | store.deactivate(); |
| 157 | target.deactivate(); |
| 158 | store = null; |
| 159 | target = null; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Tests if getting all instance ports returns the correct set of ports. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testGetInstancePorts() { |
| 167 | createBasicInstancePorts(); |
| 168 | assertEquals("Number of instance port did not match", |
| 169 | 2, target.instancePorts().size()); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Tests if getting an instance port with port ID returns the correct port. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testGetInstancePortById() { |
| 177 | createBasicInstancePorts(); |
| 178 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 179 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_2)); |
| 180 | assertNull("Instance port did not match", target.instancePort(UNKNOWN_ID)); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | /** |
| 185 | * Tests if getting an instance port with IP and network ID returns correct port. |
| 186 | */ |
| 187 | @Test |
| 188 | public void testGetInstancePortByIpAndNetId() { |
| 189 | createBasicInstancePorts(); |
| 190 | InstancePort port = target.instancePort(IP_ADDRESS_1, NETWORK_ID_1); |
| 191 | assertEquals("Instance port did not match", port, instancePort1); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Tests if getting an instance port with MAC returns correct port. |
| 196 | */ |
| 197 | @Test |
| 198 | public void testGetInstancePortByMac() { |
| 199 | createBasicInstancePorts(); |
| 200 | InstancePort port = target.instancePort(MAC_ADDRESS_1); |
| 201 | assertEquals("Instance port did not match", port, instancePort1); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Tests if getting instance ports with network ID returns correct ports. |
| 206 | */ |
| 207 | @Test |
| 208 | public void testGetInstancePortsByNetId() { |
| 209 | createBasicInstancePorts(); |
| 210 | Set<InstancePort> ports = target.instancePorts(NETWORK_ID_1); |
| 211 | assertEquals("Number of instance port did not match", |
| 212 | 1, ports.size()); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Tests creating and removing an instance port, and checks if it triggers proper events. |
| 217 | */ |
| 218 | @Test |
| 219 | public void testCreateAndRemoveInstancePort() { |
| 220 | target.createInstancePort(instancePort1); |
| 221 | assertEquals("Number of instance port did not match", |
| 222 | 1, target.instancePorts().size()); |
| 223 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 224 | |
| 225 | target.removeInstancePort(PORT_ID_1); |
| 226 | assertEquals("Number of instance port did not match", |
| 227 | 0, target.instancePorts().size()); |
| 228 | assertNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 229 | |
| 230 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_PORT_VANISHED); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Tests updating an instance port, and checks if it triggers proper events. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testCreateAndUpdateInstancePort() { |
| 238 | target.createInstancePort(instancePort1); |
| 239 | assertEquals("Number of instance port did not match", |
| 240 | 1, target.instancePorts().size()); |
| 241 | assertEquals("Instance port did not match", PORT_NUM_1, |
| 242 | target.instancePort(PORT_ID_1).portNumber()); |
| 243 | |
| 244 | HostLocation location = new HostLocation(DEV_ID_2, PORT_NUM_2, TIME_2); |
| 245 | |
| 246 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 247 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID_2) |
| 248 | .set(ANNOTATION_PORT_ID, PORT_ID_1) |
| 249 | .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_2)); |
| 250 | |
| 251 | Host host = new DefaultHost(PROVIDER_ID, HOST_ID_2, MAC_ADDRESS_2, |
| 252 | VLAN_ID, location, ImmutableSet.of(IP_ADDRESS_2), |
| 253 | annotations.build()); |
| 254 | |
| 255 | final InstancePort updated = DefaultInstancePort.from(host, ACTIVE); |
| 256 | target.updateInstancePort(updated); |
| 257 | |
| 258 | assertEquals("Number of instance port did not match", |
| 259 | 1, target.instancePorts().size()); |
| 260 | assertEquals("Instance port did not match", PORT_NUM_2, |
| 261 | target.instancePort(PORT_ID_1).portNumber()); |
| 262 | |
| 263 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_PORT_UPDATED); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Tests if creating a null instance port fails with an exception. |
| 268 | */ |
| 269 | @Test(expected = NullPointerException.class) |
| 270 | public void testCreateNullInstancePort() { |
| 271 | target.createInstancePort(null); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Tests if creating a duplicated instance port fails with an exception. |
| 276 | */ |
| 277 | @Test(expected = IllegalArgumentException.class) |
| 278 | public void testCreateDuplicateInstancePort() { |
| 279 | target.createInstancePort(instancePort1); |
| 280 | target.createInstancePort(instancePort1); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Tests if removing instance port with null ID fails with an exception. |
| 285 | */ |
| 286 | @Test(expected = IllegalArgumentException.class) |
| 287 | public void testRemoveInstancePortWithNull() { |
| 288 | target.removeInstancePort(null); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Tests if updating an unregistered instance port fails with an exception. |
| 293 | */ |
| 294 | @Test(expected = IllegalArgumentException.class) |
| 295 | public void testUpdateUnregisteredInstancePort() { |
| 296 | target.updateInstancePort(instancePort1); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Tests if it triggers the instance termination event. |
| 301 | */ |
| 302 | @Test |
| 303 | public void testTerminateInstance() { |
| 304 | InstancePort instancePort = instancePort1; |
| 305 | target.createInstancePort(instancePort); |
| 306 | |
| 307 | InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE); |
| 308 | target.updateInstancePort(inactiveInstancePort); |
| 309 | |
| 310 | assertEquals("Number of instance port did not match", |
| 311 | 1, target.instancePorts().size()); |
| 312 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 313 | |
| 314 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 315 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_TERMINATED); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Tests if it triggers the instance restart event. |
| 320 | */ |
| 321 | @Test |
| 322 | public void testRestartInstance() { |
| 323 | InstancePort instancePort = instancePort1; |
| 324 | InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE); |
| 325 | |
| 326 | target.createInstancePort(inactiveInstancePort); |
| 327 | target.updateInstancePort(instancePort); |
| 328 | |
| 329 | assertEquals("Number of instance port did not match", |
| 330 | 1, target.instancePorts().size()); |
| 331 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 332 | |
| 333 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 334 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_RESTARTED); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Tests if it triggers the instance migration start event. |
| 339 | */ |
| 340 | @Test |
| 341 | public void testMigrateInstanceStart() { |
| 342 | InstancePort instancePort = instancePort1; |
| 343 | target.createInstancePort(instancePort); |
| 344 | |
| 345 | InstancePort inactiveInstancePort = instancePort.updateState(MIGRATING); |
| 346 | target.updateInstancePort(inactiveInstancePort); |
| 347 | |
| 348 | assertEquals("Number of instance port did not match", |
| 349 | 1, target.instancePorts().size()); |
| 350 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 351 | |
| 352 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 353 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_MIGRATION_STARTED); |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Tests if it triggers the instance migration end event. |
| 358 | */ |
| 359 | @Test |
| 360 | public void testMigrateInstanceEnd() { |
| 361 | InstancePort instancePort = instancePort1; |
| 362 | InstancePort inactiveInstancePort = instancePort.updateState(MIGRATING); |
| 363 | |
| 364 | target.createInstancePort(inactiveInstancePort); |
| 365 | target.updateInstancePort(instancePort); |
| 366 | |
| 367 | assertEquals("Number of instance port did not match", |
| 368 | 1, target.instancePorts().size()); |
| 369 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 370 | |
| 371 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 372 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_MIGRATION_ENDED); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * Tests if it triggers the instance removal event from termination status. |
| 377 | */ |
| 378 | @Test |
| 379 | public void testRemoveInstanceFromTermination() { |
| 380 | InstancePort instancePort = instancePort1; |
| 381 | target.createInstancePort(instancePort); |
| 382 | |
| 383 | InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE); |
| 384 | target.updateInstancePort(inactiveInstancePort); |
| 385 | |
| 386 | assertEquals("Number of instance port did not match", |
| 387 | 1, target.instancePorts().size()); |
| 388 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 389 | |
| 390 | target.removeInstancePort(PORT_ID_1); |
| 391 | |
| 392 | assertEquals("Number of instance port did not match", |
| 393 | 0, target.instancePorts().size()); |
| 394 | assertNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 395 | |
| 396 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 397 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_TERMINATED, |
| 398 | OPENSTACK_INSTANCE_PORT_VANISHED); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Tests if it triggers the instance removal event from migration status. |
| 403 | */ |
| 404 | @Test |
| 405 | public void testRemoveInstanceFromMigration() { |
| 406 | InstancePort instancePort = instancePort1; |
| 407 | target.createInstancePort(instancePort); |
| 408 | |
| 409 | InstancePort inactiveInstancePort = instancePort.updateState(MIGRATING); |
| 410 | target.updateInstancePort(inactiveInstancePort); |
| 411 | |
| 412 | assertEquals("Number of instance port did not match", |
| 413 | 1, target.instancePorts().size()); |
| 414 | assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 415 | |
| 416 | target.removeInstancePort(PORT_ID_1); |
| 417 | |
| 418 | assertEquals("Number of instance port did not match", |
| 419 | 0, target.instancePorts().size()); |
| 420 | assertNull("Instance port did not match", target.instancePort(PORT_ID_1)); |
| 421 | |
| 422 | validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, |
| 423 | OPENSTACK_INSTANCE_PORT_UPDATED, OPENSTACK_INSTANCE_MIGRATION_STARTED, |
| 424 | OPENSTACK_INSTANCE_PORT_VANISHED); |
| 425 | } |
| 426 | |
| 427 | private void createBasicInstancePorts() { |
| 428 | target.createInstancePort(instancePort1); |
| 429 | target.createInstancePort(instancePort2); |
| 430 | } |
| 431 | |
| 432 | private static class TestCoreService extends CoreServiceAdapter { |
| 433 | |
| 434 | @Override |
| 435 | public ApplicationId registerApplication(String name) { |
| 436 | return TEST_APP_ID; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | private static class TestHostService extends HostServiceAdapter { |
| 441 | } |
| 442 | |
| 443 | private static class TestInstancePortListener implements InstancePortListener { |
| 444 | private List<InstancePortEvent> events = Lists.newArrayList(); |
| 445 | |
| 446 | @Override |
| 447 | public void event(InstancePortEvent event) { |
| 448 | events.add(event); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | private void validateEvents(Enum... types) { |
| 453 | int i = 0; |
| 454 | assertEquals("Number of events did not match", types.length, |
| 455 | testInstancePortListener.events.size()); |
| 456 | for (Event event : testInstancePortListener.events) { |
| 457 | assertEquals("Incorrect event received", types[i], event.type()); |
| 458 | i++; |
| 459 | } |
| 460 | testInstancePortListener.events.clear(); |
| 461 | } |
| 462 | } |