Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [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 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableSet; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 21 | import com.google.common.util.concurrent.MoreExecutors; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 25 | import org.onlab.junit.TestUtils; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterServiceAdapter; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreServiceAdapter; |
| 29 | import org.onosproject.core.DefaultApplicationId; |
| 30 | import org.onosproject.event.Event; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 33 | import org.onosproject.net.packet.PacketServiceAdapter; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 34 | import org.onosproject.openstacknetworking.api.OpenstackNetworkEvent; |
| 35 | import org.onosproject.openstacknetworking.api.OpenstackNetworkListener; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 36 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 37 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 38 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
| 39 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 40 | import org.onosproject.store.service.TestStorageService; |
| 41 | import org.openstack4j.model.network.Network; |
| 42 | import org.openstack4j.model.network.Port; |
| 43 | import org.openstack4j.model.network.Subnet; |
| 44 | import org.openstack4j.openstack.networking.domain.NeutronNetwork; |
| 45 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
| 46 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
| 47 | |
| 48 | import java.util.List; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 49 | import java.util.Map; |
| 50 | import java.util.Objects; |
| 51 | import java.util.Set; |
| 52 | import java.util.stream.Collectors; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 53 | |
| 54 | import static org.junit.Assert.assertEquals; |
| 55 | import static org.junit.Assert.assertTrue; |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 56 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_CREATED; |
| 57 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_REMOVED; |
| 58 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_NETWORK_UPDATED; |
| 59 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_CREATED; |
| 60 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_REMOVED; |
| 61 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_PORT_UPDATED; |
| 62 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_SUBNET_CREATED; |
| 63 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_SUBNET_REMOVED; |
| 64 | import static org.onosproject.openstacknetworking.api.OpenstackNetworkEvent.Type.OPENSTACK_SUBNET_UPDATED; |
| 65 | import static org.onosproject.openstacknode.api.NodeState.COMPLETE; |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Unit tests for OpenStack network manager. |
| 69 | */ |
| 70 | public class OpenstackNetworkManagerTest { |
| 71 | |
| 72 | private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test"); |
| 73 | |
| 74 | private static final String UNKNOWN_ID = "unknown_id"; |
| 75 | private static final String UPDATED_NAME = "updated_name"; |
| 76 | |
| 77 | private static final String NETWORK_ID = "network_id"; |
| 78 | private static final String NETWORK_NAME = "network_name"; |
| 79 | private static final Network NETWORK = NeutronNetwork.builder() |
| 80 | .name(NETWORK_NAME) |
| 81 | .build(); |
| 82 | private static final Network NETWORK_COPY = NeutronNetwork.builder() |
| 83 | .name("network") |
| 84 | .build(); |
| 85 | |
| 86 | private static final String SUBNET_ID = "subnet_id"; |
| 87 | private static final Subnet SUBNET = NeutronSubnet.builder() |
| 88 | .networkId(NETWORK_ID) |
| 89 | .cidr("192.168.0.0/24") |
| 90 | .build(); |
| 91 | private static final Subnet SUBNET_COPY = NeutronSubnet.builder() |
| 92 | .networkId(NETWORK_ID) |
| 93 | .cidr("192.168.0.0/24") |
| 94 | .build(); |
| 95 | |
| 96 | private static final String PORT_ID = "port_id"; |
| 97 | private static final Port PORT = NeutronPort.builder() |
| 98 | .networkId(NETWORK_ID) |
| 99 | .fixedIp("192.168.0.1", SUBNET_ID) |
| 100 | .build(); |
| 101 | private static final Port PORT_COPY = NeutronPort.builder() |
| 102 | .networkId(NETWORK_ID) |
| 103 | .fixedIp("192.168.0.1", SUBNET_ID) |
| 104 | .build(); |
| 105 | |
| 106 | private final TestOpenstackNetworkListener testListener = new TestOpenstackNetworkListener(); |
| 107 | |
| 108 | private OpenstackNetworkManager target; |
| 109 | private DistributedOpenstackNetworkStore osNetworkStore; |
| 110 | |
| 111 | @Before |
| 112 | public void setUp() throws Exception { |
| 113 | NETWORK.setId(NETWORK_ID); |
| 114 | NETWORK_COPY.setId(NETWORK_ID); |
| 115 | SUBNET.setId(SUBNET_ID); |
| 116 | SUBNET_COPY.setId(SUBNET_ID); |
| 117 | PORT.setId(PORT_ID); |
| 118 | PORT_COPY.setId(PORT_ID); |
| 119 | |
| 120 | osNetworkStore = new DistributedOpenstackNetworkStore(); |
| 121 | TestUtils.setField(osNetworkStore, "coreService", new TestCoreService()); |
| 122 | TestUtils.setField(osNetworkStore, "storageService", new TestStorageService()); |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 123 | TestUtils.setField(osNetworkStore, "eventExecutor", MoreExecutors.newDirectExecutorService()); |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 124 | osNetworkStore.activate(); |
| 125 | |
| 126 | target = new OpenstackNetworkManager(); |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 127 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 128 | TestUtils.setField(target, "packetService", new PacketServiceAdapter()); |
| 129 | TestUtils.setField(target, "deviceService", new DeviceServiceAdapter()); |
| 130 | TestUtils.setField(target, "storageService", new TestStorageService()); |
| 131 | TestUtils.setField(target, "clusterService", new ClusterServiceAdapter()); |
| 132 | TestUtils.setField(target, "openstacknodeService", new TestOpenstackNodeManager()); |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 133 | target.osNetworkStore = osNetworkStore; |
| 134 | target.addListener(testListener); |
| 135 | target.activate(); |
| 136 | } |
| 137 | |
| 138 | @After |
| 139 | public void tearDown() { |
| 140 | target.removeListener(testListener); |
| 141 | osNetworkStore.deactivate(); |
| 142 | target.deactivate(); |
| 143 | osNetworkStore = null; |
| 144 | target = null; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Tests if getting all networks returns the correct set of networks. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testGetNetworks() { |
| 152 | createBasicNetworks(); |
| 153 | assertEquals("Number of network did not match", 1, target.networks().size()); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Tests if getting a network with ID returns the correct network. |
| 158 | */ |
| 159 | @Test |
| 160 | public void testGetNetworkById() { |
| 161 | createBasicNetworks(); |
| 162 | assertTrue("Network did not match", target.network(NETWORK_ID) != null); |
| 163 | assertTrue("Network did not match", target.network(UNKNOWN_ID) == null); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Tests creating and removing a network, and checks if it triggers proper events. |
| 168 | */ |
| 169 | @Test |
| 170 | public void testCreateAndRemoveNetwork() { |
| 171 | target.createNetwork(NETWORK); |
| 172 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 173 | assertTrue("Network was not created", target.network(NETWORK_ID) != null); |
| 174 | |
| 175 | target.removeNetwork(NETWORK_ID); |
| 176 | assertEquals("Number of networks did not match", 0, target.networks().size()); |
| 177 | assertTrue("Network was not removed", target.network(NETWORK_ID) == null); |
| 178 | |
| 179 | validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_REMOVED); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Tests updating a network, and checks if it triggers proper events. |
| 184 | */ |
| 185 | @Test |
| 186 | public void testCreateAndUpdateNetwork() { |
| 187 | target.createNetwork(NETWORK); |
| 188 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 189 | assertEquals("Network did not match", NETWORK_NAME, target.network(NETWORK_ID).getName()); |
| 190 | |
| 191 | final Network updated = NeutronNetwork.builder() |
| 192 | .from(NETWORK_COPY) |
| 193 | .name(UPDATED_NAME) |
| 194 | .build(); |
| 195 | target.updateNetwork(updated); |
| 196 | |
| 197 | assertEquals("Number of networks did not match", 1, target.networks().size()); |
| 198 | assertEquals("Network did not match", UPDATED_NAME, target.network(NETWORK_ID).getName()); |
| 199 | validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_UPDATED); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Tests if creating a null network fails with an exception. |
| 204 | */ |
| 205 | @Test(expected = NullPointerException.class) |
| 206 | public void testCreateNullNetwork() { |
| 207 | target.createNetwork(null); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Tests if creating a network with null ID fails with an exception. |
| 212 | */ |
| 213 | @Test(expected = IllegalArgumentException.class) |
| 214 | public void testCreateNetworkWithNullId() { |
| 215 | final Network testNet = NeutronNetwork.builder().build(); |
| 216 | target.createNetwork(testNet); |
| 217 | } |
| 218 | |
| 219 | /** |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 220 | * Tests if creating a duplicate network fails with an exception. |
| 221 | */ |
| 222 | @Test(expected = IllegalArgumentException.class) |
| 223 | public void testCreateDuplicateNetwork() { |
| 224 | target.createNetwork(NETWORK); |
| 225 | target.createNetwork(NETWORK); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Tests if removing network with null ID fails with an exception. |
| 230 | */ |
| 231 | @Test(expected = IllegalArgumentException.class) |
| 232 | public void testRemoveNetworkWithNull() { |
| 233 | target.removeNetwork(null); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Tests if updating a network with null name fails with an exception. |
| 238 | */ |
| 239 | @Test(expected = IllegalArgumentException.class) |
| 240 | public void testUpdateNetworkWithNullName() { |
| 241 | final Network updated = NeutronNetwork.builder() |
| 242 | .name(null) |
| 243 | .build(); |
| 244 | updated.setId(NETWORK_ID); |
| 245 | target.updateNetwork(updated); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Tests if updating an unregistered network fails with an exception. |
| 250 | */ |
| 251 | @Test(expected = IllegalArgumentException.class) |
| 252 | public void testUpdateUnregisteredNetwork() { |
| 253 | target.updateNetwork(NETWORK); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Tests if updating a network with null ID fails with an exception. |
| 258 | */ |
| 259 | @Test(expected = IllegalArgumentException.class) |
| 260 | public void testUpdateNetworkWithNullId() { |
| 261 | final Network testNet = NeutronNetwork.builder().build(); |
| 262 | target.updateNetwork(testNet); |
| 263 | } |
| 264 | |
| 265 | |
| 266 | /** |
| 267 | * Tests if getting all subnets returns the correct set of subnets. |
| 268 | */ |
| 269 | @Test |
| 270 | public void testGetSubnets() { |
| 271 | createBasicNetworks(); |
| 272 | assertEquals("Number of subnet did not match", 1, target.subnets().size()); |
| 273 | } |
| 274 | |
| 275 | @Test |
| 276 | public void testGetSubnetsByNetworkId() { |
| 277 | createBasicNetworks(); |
| 278 | assertEquals("Subnet did not match", 1, target.subnets(NETWORK_ID).size()); |
| 279 | assertEquals("Subnet did not match", 0, target.subnets(UNKNOWN_ID).size()); |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Tests if getting a subnet with ID returns the correct subnet. |
| 284 | */ |
| 285 | @Test |
| 286 | public void testGetSubnetById() { |
| 287 | createBasicNetworks(); |
| 288 | assertTrue("Subnet did not match", target.subnet(SUBNET_ID) != null); |
| 289 | assertTrue("Subnet did not match", target.subnet(UNKNOWN_ID) == null); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Tests creating and removing a subnet, and checks if it triggers proper events. |
| 294 | */ |
| 295 | @Test |
| 296 | public void testCreateAndRemoveSubnet() { |
| 297 | target.createSubnet(SUBNET); |
| 298 | assertEquals("Number of subnet did not match", 1, target.subnets().size()); |
| 299 | assertTrue("Subnet was not created", target.subnet(SUBNET_ID) != null); |
| 300 | |
| 301 | target.removeSubnet(SUBNET_ID); |
| 302 | assertEquals("Number of subnet did not match", 0, target.subnets().size()); |
| 303 | assertTrue("Subnet was not removed", target.subnet(SUBNET_ID) == null); |
| 304 | |
| 305 | validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_REMOVED); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Tests updating a subnet, and checks if it triggers proper events. |
| 310 | */ |
| 311 | @Test |
| 312 | public void testCreateAndUpdateSubnet() { |
| 313 | target.createSubnet(SUBNET_COPY); |
| 314 | assertEquals("Number of subnet did not match", 1, target.subnets().size()); |
| 315 | assertEquals("Subnet did not match", null, target.subnet(SUBNET_ID).getName()); |
| 316 | |
| 317 | // TODO fix NeutronSubnet.builder().from() in openstack4j |
| 318 | final Subnet updated = NeutronSubnet.builder() |
| 319 | .networkId(NETWORK_ID) |
| 320 | .cidr("192.168.0.0/24") |
| 321 | .name(UPDATED_NAME) |
| 322 | .build(); |
| 323 | updated.setId(SUBNET_ID); |
| 324 | target.updateSubnet(updated); |
| 325 | |
| 326 | assertEquals("Number of subnet did not match", 1, target.subnets().size()); |
| 327 | assertEquals("Subnet did not match", UPDATED_NAME, target.subnet(SUBNET_ID).getName()); |
| 328 | |
| 329 | validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_UPDATED); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Tests if creating a null subnet fails with an exception. |
| 334 | */ |
| 335 | @Test(expected = NullPointerException.class) |
| 336 | public void testCreateNullSubnet() { |
| 337 | target.createSubnet(null); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Tests if creating a subnet with null ID fails with an exception. |
| 342 | */ |
| 343 | @Test(expected = IllegalArgumentException.class) |
| 344 | public void testCreateSubnetWithNullId() { |
| 345 | final Subnet testSubnet = NeutronSubnet.builder() |
| 346 | .networkId(NETWORK_ID) |
| 347 | .cidr("192.168.0.0/24") |
| 348 | .build(); |
| 349 | target.createSubnet(testSubnet); |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Tests if creating subnet with null network ID fails with an exception. |
| 354 | */ |
| 355 | @Test(expected = IllegalArgumentException.class) |
| 356 | public void testCreateSubnetWithNullNetworkId() { |
| 357 | final Subnet testSubnet = NeutronSubnet.builder() |
| 358 | .cidr("192.168.0.0/24") |
| 359 | .build(); |
| 360 | testSubnet.setId(SUBNET_ID); |
| 361 | target.createSubnet(testSubnet); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Tests if creating a subnet with null CIDR fails with an exception. |
| 366 | */ |
| 367 | @Test(expected = IllegalArgumentException.class) |
| 368 | public void testCreateSubnetWithNullCidr() { |
| 369 | final Subnet testSubnet = NeutronSubnet.builder() |
| 370 | .networkId(NETWORK_ID) |
| 371 | .build(); |
| 372 | testSubnet.setId(SUBNET_ID); |
| 373 | target.createSubnet(testSubnet); |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Tests if creating a duplicate subnet fails with an exception. |
| 378 | */ |
| 379 | @Test(expected = IllegalArgumentException.class) |
| 380 | public void testCreateDuplicateSubnet() { |
| 381 | target.createSubnet(SUBNET); |
| 382 | target.createSubnet(SUBNET); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * Tests if updating an unregistered subnet fails with an exception. |
| 387 | */ |
| 388 | @Test(expected = IllegalArgumentException.class) |
| 389 | public void testUpdateUnregisteredSubnet() { |
| 390 | target.updateSubnet(SUBNET); |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Tests if updating a null subnet fails with an exception. |
| 395 | */ |
| 396 | @Test(expected = NullPointerException.class) |
| 397 | public void testUpdateSubnetWithNull() { |
| 398 | target.updateSubnet(null); |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Tests if updating a subnet with null ID fails with an exception. |
| 403 | */ |
| 404 | @Test(expected = IllegalArgumentException.class) |
| 405 | public void testUpdateSubnetWithNullId() { |
| 406 | final Subnet testSubnet = NeutronSubnet.builder() |
| 407 | .networkId(NETWORK_ID) |
| 408 | .cidr("192.168.0.0/24") |
| 409 | .build(); |
| 410 | target.updateSubnet(testSubnet); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Tests if updating a subnet with null network ID fails with an exception. |
| 415 | */ |
| 416 | @Test(expected = IllegalArgumentException.class) |
| 417 | public void testUpdateSubnetWithNullNetworkId() { |
| 418 | final Subnet testSubnet = NeutronSubnet.builder() |
| 419 | .cidr("192.168.0.0/24") |
| 420 | .build(); |
| 421 | testSubnet.setId(SUBNET_ID); |
| 422 | target.updateSubnet(testSubnet); |
| 423 | } |
| 424 | |
| 425 | /** |
| 426 | * Tests if updating a subnet with null CIDR fails with an exception. |
| 427 | */ |
| 428 | @Test(expected = IllegalArgumentException.class) |
| 429 | public void testUpdateSubnetWithNullCidr() { |
| 430 | final Subnet testSubnet = NeutronSubnet.builder() |
| 431 | .networkId(NETWORK_ID) |
| 432 | .build(); |
| 433 | testSubnet.setId(SUBNET_ID); |
| 434 | target.updateSubnet(testSubnet); |
| 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Tests if getting all ports returns correct set of values. |
| 439 | */ |
| 440 | @Test |
| 441 | public void testGetPorts() { |
| 442 | createBasicNetworks(); |
| 443 | assertEquals("Number of port did not match", 1, target.ports().size()); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Tests if getting a port with network ID returns correct set of values. |
| 448 | */ |
| 449 | @Test |
| 450 | public void testGetPortsByNetworkId() { |
| 451 | createBasicNetworks(); |
| 452 | assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size()); |
| 453 | assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size()); |
| 454 | } |
| 455 | |
| 456 | /** |
| 457 | * Tests if getting a port with ID returns correct value. |
| 458 | */ |
| 459 | @Test |
| 460 | public void testGetPortById() { |
| 461 | createBasicNetworks(); |
| 462 | assertTrue("Port did not match", target.port(PORT_ID) != null); |
| 463 | assertTrue("Port did not match", target.port(UNKNOWN_ID) == null); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Tests creating and removing a port, and checks if proper event is triggered. |
| 468 | */ |
| 469 | @Test |
| 470 | public void testCreateAndRemovePort() { |
| 471 | target.createPort(PORT); |
| 472 | assertEquals("Number of port did not match", 1, target.ports().size()); |
| 473 | assertTrue("Port was not created", target.port(PORT_ID) != null); |
| 474 | |
| 475 | target.removePort(PORT_ID); |
| 476 | assertEquals("Number of port did not match", 0, target.ports().size()); |
| 477 | assertTrue("Port was not created", target.port(PORT_ID) == null); |
| 478 | |
| 479 | validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_REMOVED); |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Tests creating and updating a port, and checks if proper event is triggered. |
| 484 | */ |
| 485 | @Test |
| 486 | public void testCreateAndUpdatePort() { |
| 487 | target.createPort(PORT); |
| 488 | assertEquals("Number of port did not match", 1, target.ports().size()); |
| 489 | assertEquals("Port did not match", null, target.port(PORT_ID).getName()); |
| 490 | |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 491 | final Port updated = PORT_COPY.toBuilder() |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 492 | .name(UPDATED_NAME) |
| 493 | .build(); |
| 494 | target.updatePort(updated); |
| 495 | |
| 496 | assertEquals("Number of port did not match", 1, target.ports().size()); |
| 497 | assertEquals("Port did not match", UPDATED_NAME, target.port(PORT_ID).getName()); |
| 498 | |
| 499 | validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_UPDATED); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Tests if creating a null port fails with an exception. |
| 504 | */ |
| 505 | @Test(expected = NullPointerException.class) |
| 506 | public void testCreateNullPort() { |
| 507 | target.createPort(null); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Tests if creating a port with null ID fails with an exception. |
| 512 | */ |
| 513 | @Test(expected = IllegalArgumentException.class) |
| 514 | public void testCreatePortWithNullId() { |
| 515 | final Port testPort = NeutronPort.builder() |
| 516 | .networkId(NETWORK_ID) |
| 517 | .build(); |
| 518 | target.createPort(testPort); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Tests if creating a port with null network ID fails with an exception. |
| 523 | */ |
| 524 | @Test(expected = IllegalArgumentException.class) |
| 525 | public void testCreatePortWithNullNetworkId() { |
| 526 | final Port testPort = NeutronPort.builder().build(); |
| 527 | testPort.setId(PORT_ID); |
| 528 | target.createPort(testPort); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * Tests if creating a duplicate port fails with an exception. |
| 533 | */ |
| 534 | @Test(expected = IllegalArgumentException.class) |
| 535 | public void createDuplicatePort() { |
| 536 | target.createPort(PORT); |
| 537 | target.createPort(PORT); |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Tests if updating an unregistered port fails with an exception. |
| 542 | */ |
| 543 | @Test(expected = IllegalArgumentException.class) |
| 544 | public void testUpdateUnregisteredPort() { |
| 545 | target.updatePort(PORT); |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Tests if updating a null port fails with an exception. |
| 550 | */ |
| 551 | @Test(expected = NullPointerException.class) |
| 552 | public void testUpdateNullPort() { |
| 553 | target.updatePort(null); |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * Tests if updating a port with null ID fails with exception. |
| 558 | */ |
| 559 | @Test(expected = IllegalArgumentException.class) |
| 560 | public void testUpdatePortWithNullId() { |
| 561 | final Port testPort = NeutronPort.builder() |
| 562 | .networkId(NETWORK_ID) |
| 563 | .build(); |
| 564 | target.updatePort(testPort); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Tests if updating a port with null network ID fails with an exception. |
| 569 | */ |
| 570 | @Test(expected = IllegalArgumentException.class) |
| 571 | public void testUpdatePortWithNullNetworkId() { |
| 572 | final Port testPort = NeutronPort.builder().build(); |
| 573 | testPort.setId(PORT_ID); |
| 574 | target.updatePort(testPort); |
| 575 | } |
| 576 | |
| 577 | private void createBasicNetworks() { |
| 578 | target.createNetwork(NETWORK); |
| 579 | target.createSubnet(SUBNET); |
| 580 | target.createPort(PORT); |
| 581 | } |
| 582 | |
| 583 | private static class TestCoreService extends CoreServiceAdapter { |
| 584 | |
| 585 | @Override |
| 586 | public ApplicationId registerApplication(String name) { |
| 587 | return TEST_APP_ID; |
| 588 | } |
| 589 | } |
| 590 | |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 591 | private static class TestOpenstackNodeManager implements OpenstackNodeService, OpenstackNodeAdminService { |
| 592 | Map<String, OpenstackNode> osNodeMap = Maps.newHashMap(); |
| 593 | List<OpenstackNodeListener> listeners = Lists.newArrayList(); |
| 594 | |
| 595 | @Override |
| 596 | public Set<OpenstackNode> nodes() { |
| 597 | return ImmutableSet.copyOf(osNodeMap.values()); |
| 598 | } |
| 599 | |
| 600 | @Override |
| 601 | public Set<OpenstackNode> nodes(OpenstackNode.NodeType type) { |
| 602 | return osNodeMap.values().stream() |
| 603 | .filter(osNode -> osNode.type() == type) |
| 604 | .collect(Collectors.toSet()); |
| 605 | } |
| 606 | |
| 607 | @Override |
| 608 | public Set<OpenstackNode> completeNodes() { |
| 609 | return osNodeMap.values().stream() |
| 610 | .filter(osNode -> osNode.state() == COMPLETE) |
| 611 | .collect(Collectors.toSet()); |
| 612 | } |
| 613 | |
| 614 | @Override |
| 615 | public Set<OpenstackNode> completeNodes(OpenstackNode.NodeType type) { |
| 616 | return osNodeMap.values().stream() |
| 617 | .filter(osNode -> osNode.type() == type && osNode.state() == COMPLETE) |
| 618 | .collect(Collectors.toSet()); |
| 619 | } |
| 620 | |
| 621 | @Override |
| 622 | public OpenstackNode node(String hostname) { |
| 623 | return osNodeMap.get(hostname); |
| 624 | } |
| 625 | |
| 626 | @Override |
| 627 | public OpenstackNode node(DeviceId deviceId) { |
| 628 | return osNodeMap.values().stream() |
| 629 | .filter(osNode -> Objects.equals(osNode.intgBridge(), deviceId) || |
| 630 | Objects.equals(osNode.ovsdb(), deviceId)) |
| 631 | .findFirst().orElse(null); |
| 632 | } |
| 633 | |
| 634 | @Override |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 635 | public void addVfPort(OpenstackNode osNode, String portName) { |
| 636 | } |
| 637 | |
| 638 | @Override |
| 639 | public void removeVfPort(OpenstackNode osNode, String portName) { |
| 640 | } |
| 641 | |
| 642 | @Override |
daniel park | b581710 | 2018-02-15 00:18:51 +0900 | [diff] [blame] | 643 | public void addListener(OpenstackNodeListener listener) { |
| 644 | listeners.add(listener); |
| 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public void removeListener(OpenstackNodeListener listener) { |
| 649 | listeners.remove(listener); |
| 650 | } |
| 651 | |
| 652 | @Override |
| 653 | public void createNode(OpenstackNode osNode) { |
| 654 | osNodeMap.put(osNode.hostname(), osNode); |
| 655 | } |
| 656 | |
| 657 | @Override |
| 658 | public void updateNode(OpenstackNode osNode) { |
| 659 | osNodeMap.put(osNode.hostname(), osNode); |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public OpenstackNode removeNode(String hostname) { |
| 664 | return null; |
| 665 | } |
| 666 | } |
| 667 | |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 668 | private static class TestOpenstackNetworkListener implements OpenstackNetworkListener { |
| 669 | private List<OpenstackNetworkEvent> events = Lists.newArrayList(); |
| 670 | |
| 671 | @Override |
| 672 | public void event(OpenstackNetworkEvent event) { |
| 673 | events.add(event); |
| 674 | } |
| 675 | } |
| 676 | |
| 677 | private void validateEvents(Enum... types) { |
Hyunsun Moon | 32d471f | 2017-04-27 14:29:44 +0900 | [diff] [blame] | 678 | int i = 0; |
| 679 | assertEquals("Number of events did not match", types.length, testListener.events.size()); |
| 680 | for (Event event : testListener.events) { |
| 681 | assertEquals("Incorrect event received", types[i], event.type()); |
| 682 | i++; |
| 683 | } |
| 684 | testListener.events.clear(); |
Hyunsun Moon | c7eb0d0 | 2017-03-27 18:13:00 +0900 | [diff] [blame] | 685 | } |
| 686 | } |