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