blob: 07f12f1367d48ebe7e34a65a37cca584ba234d98 [file] [log] [blame]
Hyunsun Moonc7eb0d02017-03-27 18:13:00 +09001/*
2 * Copyright 2017-present Open Networking Laboratory
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 */
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 /**
192 * Tests if creating a network with null name fails with an exception.
193 */
194 @Test(expected = IllegalArgumentException.class)
195 public void testCreateNetworkWithNullName() {
196 final Network testNet = NeutronNetwork.builder().build();
197 testNet.setId(NETWORK_ID);
198 target.createNetwork(testNet);
199 }
200
201 /**
202 * Tests if creating a duplicate network fails with an exception.
203 */
204 @Test(expected = IllegalArgumentException.class)
205 public void testCreateDuplicateNetwork() {
206 target.createNetwork(NETWORK);
207 target.createNetwork(NETWORK);
208 }
209
210 /**
211 * Tests if removing network with null ID fails with an exception.
212 */
213 @Test(expected = IllegalArgumentException.class)
214 public void testRemoveNetworkWithNull() {
215 target.removeNetwork(null);
216 }
217
218 /**
219 * Tests if updating a network with null name fails with an exception.
220 */
221 @Test(expected = IllegalArgumentException.class)
222 public void testUpdateNetworkWithNullName() {
223 final Network updated = NeutronNetwork.builder()
224 .name(null)
225 .build();
226 updated.setId(NETWORK_ID);
227 target.updateNetwork(updated);
228 }
229
230 /**
231 * Tests if updating an unregistered network fails with an exception.
232 */
233 @Test(expected = IllegalArgumentException.class)
234 public void testUpdateUnregisteredNetwork() {
235 target.updateNetwork(NETWORK);
236 }
237
238 /**
239 * Tests if updating a network with null ID fails with an exception.
240 */
241 @Test(expected = IllegalArgumentException.class)
242 public void testUpdateNetworkWithNullId() {
243 final Network testNet = NeutronNetwork.builder().build();
244 target.updateNetwork(testNet);
245 }
246
247
248 /**
249 * Tests if getting all subnets returns the correct set of subnets.
250 */
251 @Test
252 public void testGetSubnets() {
253 createBasicNetworks();
254 assertEquals("Number of subnet did not match", 1, target.subnets().size());
255 }
256
257 @Test
258 public void testGetSubnetsByNetworkId() {
259 createBasicNetworks();
260 assertEquals("Subnet did not match", 1, target.subnets(NETWORK_ID).size());
261 assertEquals("Subnet did not match", 0, target.subnets(UNKNOWN_ID).size());
262 }
263
264 /**
265 * Tests if getting a subnet with ID returns the correct subnet.
266 */
267 @Test
268 public void testGetSubnetById() {
269 createBasicNetworks();
270 assertTrue("Subnet did not match", target.subnet(SUBNET_ID) != null);
271 assertTrue("Subnet did not match", target.subnet(UNKNOWN_ID) == null);
272 }
273
274 /**
275 * Tests creating and removing a subnet, and checks if it triggers proper events.
276 */
277 @Test
278 public void testCreateAndRemoveSubnet() {
279 target.createSubnet(SUBNET);
280 assertEquals("Number of subnet did not match", 1, target.subnets().size());
281 assertTrue("Subnet was not created", target.subnet(SUBNET_ID) != null);
282
283 target.removeSubnet(SUBNET_ID);
284 assertEquals("Number of subnet did not match", 0, target.subnets().size());
285 assertTrue("Subnet was not removed", target.subnet(SUBNET_ID) == null);
286
287 validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_REMOVED);
288 }
289
290 /**
291 * Tests updating a subnet, and checks if it triggers proper events.
292 */
293 @Test
294 public void testCreateAndUpdateSubnet() {
295 target.createSubnet(SUBNET_COPY);
296 assertEquals("Number of subnet did not match", 1, target.subnets().size());
297 assertEquals("Subnet did not match", null, target.subnet(SUBNET_ID).getName());
298
299 // TODO fix NeutronSubnet.builder().from() in openstack4j
300 final Subnet updated = NeutronSubnet.builder()
301 .networkId(NETWORK_ID)
302 .cidr("192.168.0.0/24")
303 .name(UPDATED_NAME)
304 .build();
305 updated.setId(SUBNET_ID);
306 target.updateSubnet(updated);
307
308 assertEquals("Number of subnet did not match", 1, target.subnets().size());
309 assertEquals("Subnet did not match", UPDATED_NAME, target.subnet(SUBNET_ID).getName());
310
311 validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_UPDATED);
312 }
313
314 /**
315 * Tests if creating a null subnet fails with an exception.
316 */
317 @Test(expected = NullPointerException.class)
318 public void testCreateNullSubnet() {
319 target.createSubnet(null);
320 }
321
322 /**
323 * Tests if creating a subnet with null ID fails with an exception.
324 */
325 @Test(expected = IllegalArgumentException.class)
326 public void testCreateSubnetWithNullId() {
327 final Subnet testSubnet = NeutronSubnet.builder()
328 .networkId(NETWORK_ID)
329 .cidr("192.168.0.0/24")
330 .build();
331 target.createSubnet(testSubnet);
332 }
333
334 /**
335 * Tests if creating subnet with null network ID fails with an exception.
336 */
337 @Test(expected = IllegalArgumentException.class)
338 public void testCreateSubnetWithNullNetworkId() {
339 final Subnet testSubnet = NeutronSubnet.builder()
340 .cidr("192.168.0.0/24")
341 .build();
342 testSubnet.setId(SUBNET_ID);
343 target.createSubnet(testSubnet);
344 }
345
346 /**
347 * Tests if creating a subnet with null CIDR fails with an exception.
348 */
349 @Test(expected = IllegalArgumentException.class)
350 public void testCreateSubnetWithNullCidr() {
351 final Subnet testSubnet = NeutronSubnet.builder()
352 .networkId(NETWORK_ID)
353 .build();
354 testSubnet.setId(SUBNET_ID);
355 target.createSubnet(testSubnet);
356 }
357
358 /**
359 * Tests if creating a duplicate subnet fails with an exception.
360 */
361 @Test(expected = IllegalArgumentException.class)
362 public void testCreateDuplicateSubnet() {
363 target.createSubnet(SUBNET);
364 target.createSubnet(SUBNET);
365 }
366
367 /**
368 * Tests if updating an unregistered subnet fails with an exception.
369 */
370 @Test(expected = IllegalArgumentException.class)
371 public void testUpdateUnregisteredSubnet() {
372 target.updateSubnet(SUBNET);
373 }
374
375 /**
376 * Tests if updating a null subnet fails with an exception.
377 */
378 @Test(expected = NullPointerException.class)
379 public void testUpdateSubnetWithNull() {
380 target.updateSubnet(null);
381 }
382
383 /**
384 * Tests if updating a subnet with null ID fails with an exception.
385 */
386 @Test(expected = IllegalArgumentException.class)
387 public void testUpdateSubnetWithNullId() {
388 final Subnet testSubnet = NeutronSubnet.builder()
389 .networkId(NETWORK_ID)
390 .cidr("192.168.0.0/24")
391 .build();
392 target.updateSubnet(testSubnet);
393 }
394
395 /**
396 * Tests if updating a subnet with null network ID fails with an exception.
397 */
398 @Test(expected = IllegalArgumentException.class)
399 public void testUpdateSubnetWithNullNetworkId() {
400 final Subnet testSubnet = NeutronSubnet.builder()
401 .cidr("192.168.0.0/24")
402 .build();
403 testSubnet.setId(SUBNET_ID);
404 target.updateSubnet(testSubnet);
405 }
406
407 /**
408 * Tests if updating a subnet with null CIDR fails with an exception.
409 */
410 @Test(expected = IllegalArgumentException.class)
411 public void testUpdateSubnetWithNullCidr() {
412 final Subnet testSubnet = NeutronSubnet.builder()
413 .networkId(NETWORK_ID)
414 .build();
415 testSubnet.setId(SUBNET_ID);
416 target.updateSubnet(testSubnet);
417 }
418
419 /**
420 * Tests if getting all ports returns correct set of values.
421 */
422 @Test
423 public void testGetPorts() {
424 createBasicNetworks();
425 assertEquals("Number of port did not match", 1, target.ports().size());
426 }
427
428 /**
429 * Tests if getting a port with network ID returns correct set of values.
430 */
431 @Test
432 public void testGetPortsByNetworkId() {
433 createBasicNetworks();
434 assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size());
435 assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size());
436 }
437
438 /**
439 * Tests if getting a port with ID returns correct value.
440 */
441 @Test
442 public void testGetPortById() {
443 createBasicNetworks();
444 assertTrue("Port did not match", target.port(PORT_ID) != null);
445 assertTrue("Port did not match", target.port(UNKNOWN_ID) == null);
446 }
447
448 /**
449 * Tests creating and removing a port, and checks if proper event is triggered.
450 */
451 @Test
452 public void testCreateAndRemovePort() {
453 target.createPort(PORT);
454 assertEquals("Number of port did not match", 1, target.ports().size());
455 assertTrue("Port was not created", target.port(PORT_ID) != null);
456
457 target.removePort(PORT_ID);
458 assertEquals("Number of port did not match", 0, target.ports().size());
459 assertTrue("Port was not created", target.port(PORT_ID) == null);
460
461 validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_REMOVED);
462 }
463
464 /**
465 * Tests creating and updating a port, and checks if proper event is triggered.
466 */
467 @Test
468 public void testCreateAndUpdatePort() {
469 target.createPort(PORT);
470 assertEquals("Number of port did not match", 1, target.ports().size());
471 assertEquals("Port did not match", null, target.port(PORT_ID).getName());
472
Hyunsun Moon32d471f2017-04-27 14:29:44 +0900473 final Port updated = PORT_COPY.toBuilder()
Hyunsun Moonc7eb0d02017-03-27 18:13:00 +0900474 .name(UPDATED_NAME)
475 .build();
476 target.updatePort(updated);
477
478 assertEquals("Number of port did not match", 1, target.ports().size());
479 assertEquals("Port did not match", UPDATED_NAME, target.port(PORT_ID).getName());
480
481 validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_UPDATED);
482 }
483
484 /**
485 * Tests if creating a null port fails with an exception.
486 */
487 @Test(expected = NullPointerException.class)
488 public void testCreateNullPort() {
489 target.createPort(null);
490 }
491
492 /**
493 * Tests if creating a port with null ID fails with an exception.
494 */
495 @Test(expected = IllegalArgumentException.class)
496 public void testCreatePortWithNullId() {
497 final Port testPort = NeutronPort.builder()
498 .networkId(NETWORK_ID)
499 .build();
500 target.createPort(testPort);
501 }
502
503 /**
504 * Tests if creating a port with null network ID fails with an exception.
505 */
506 @Test(expected = IllegalArgumentException.class)
507 public void testCreatePortWithNullNetworkId() {
508 final Port testPort = NeutronPort.builder().build();
509 testPort.setId(PORT_ID);
510 target.createPort(testPort);
511 }
512
513 /**
514 * Tests if creating a duplicate port fails with an exception.
515 */
516 @Test(expected = IllegalArgumentException.class)
517 public void createDuplicatePort() {
518 target.createPort(PORT);
519 target.createPort(PORT);
520 }
521
522 /**
523 * Tests if updating an unregistered port fails with an exception.
524 */
525 @Test(expected = IllegalArgumentException.class)
526 public void testUpdateUnregisteredPort() {
527 target.updatePort(PORT);
528 }
529
530 /**
531 * Tests if updating a null port fails with an exception.
532 */
533 @Test(expected = NullPointerException.class)
534 public void testUpdateNullPort() {
535 target.updatePort(null);
536 }
537
538 /**
539 * Tests if updating a port with null ID fails with exception.
540 */
541 @Test(expected = IllegalArgumentException.class)
542 public void testUpdatePortWithNullId() {
543 final Port testPort = NeutronPort.builder()
544 .networkId(NETWORK_ID)
545 .build();
546 target.updatePort(testPort);
547 }
548
549 /**
550 * Tests if updating a port with null network ID fails with an exception.
551 */
552 @Test(expected = IllegalArgumentException.class)
553 public void testUpdatePortWithNullNetworkId() {
554 final Port testPort = NeutronPort.builder().build();
555 testPort.setId(PORT_ID);
556 target.updatePort(testPort);
557 }
558
559 private void createBasicNetworks() {
560 target.createNetwork(NETWORK);
561 target.createSubnet(SUBNET);
562 target.createPort(PORT);
563 }
564
565 private static class TestCoreService extends CoreServiceAdapter {
566
567 @Override
568 public ApplicationId registerApplication(String name) {
569 return TEST_APP_ID;
570 }
571 }
572
573 private static class TestOpenstackNetworkListener implements OpenstackNetworkListener {
574 private List<OpenstackNetworkEvent> events = Lists.newArrayList();
575
576 @Override
577 public void event(OpenstackNetworkEvent event) {
578 events.add(event);
579 }
580 }
581
582 private void validateEvents(Enum... types) {
Hyunsun Moon32d471f2017-04-27 14:29:44 +0900583 int i = 0;
584 assertEquals("Number of events did not match", types.length, testListener.events.size());
585 for (Event event : testListener.events) {
586 assertEquals("Incorrect event received", types[i], event.type());
587 i++;
588 }
589 testListener.events.clear();
Hyunsun Moonc7eb0d02017-03-27 18:13:00 +0900590 }
591}