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