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