blob: a33f626dd011865d418236ab135d4daeff2403bf [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;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onlab.junit.TestTools;
23import 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());
100 osNetworkStore.activate();
101
102 target = new OpenstackNetworkManager();
103 target.coreService = new TestCoreService();
104 target.osNetworkStore = osNetworkStore;
105 target.addListener(testListener);
106 target.activate();
107 }
108
109 @After
110 public void tearDown() {
111 target.removeListener(testListener);
112 osNetworkStore.deactivate();
113 target.deactivate();
114 osNetworkStore = null;
115 target = null;
116 }
117
118 /**
119 * Tests if getting all networks returns the correct set of networks.
120 */
121 @Test
122 public void testGetNetworks() {
123 createBasicNetworks();
124 assertEquals("Number of network did not match", 1, target.networks().size());
125 }
126
127 /**
128 * Tests if getting a network with ID returns the correct network.
129 */
130 @Test
131 public void testGetNetworkById() {
132 createBasicNetworks();
133 assertTrue("Network did not match", target.network(NETWORK_ID) != null);
134 assertTrue("Network did not match", target.network(UNKNOWN_ID) == null);
135 }
136
137 /**
138 * Tests creating and removing a network, and checks if it triggers proper events.
139 */
140 @Test
141 public void testCreateAndRemoveNetwork() {
142 target.createNetwork(NETWORK);
143 assertEquals("Number of networks did not match", 1, target.networks().size());
144 assertTrue("Network was not created", target.network(NETWORK_ID) != null);
145
146 target.removeNetwork(NETWORK_ID);
147 assertEquals("Number of networks did not match", 0, target.networks().size());
148 assertTrue("Network was not removed", target.network(NETWORK_ID) == null);
149
150 validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_REMOVED);
151 }
152
153 /**
154 * Tests updating a network, and checks if it triggers proper events.
155 */
156 @Test
157 public void testCreateAndUpdateNetwork() {
158 target.createNetwork(NETWORK);
159 assertEquals("Number of networks did not match", 1, target.networks().size());
160 assertEquals("Network did not match", NETWORK_NAME, target.network(NETWORK_ID).getName());
161
162 final Network updated = NeutronNetwork.builder()
163 .from(NETWORK_COPY)
164 .name(UPDATED_NAME)
165 .build();
166 target.updateNetwork(updated);
167
168 assertEquals("Number of networks did not match", 1, target.networks().size());
169 assertEquals("Network did not match", UPDATED_NAME, target.network(NETWORK_ID).getName());
170 validateEvents(OPENSTACK_NETWORK_CREATED, OPENSTACK_NETWORK_UPDATED);
171 }
172
173 /**
174 * Tests if creating a null network fails with an exception.
175 */
176 @Test(expected = NullPointerException.class)
177 public void testCreateNullNetwork() {
178 target.createNetwork(null);
179 }
180
181 /**
182 * Tests if creating a network with null ID fails with an exception.
183 */
184 @Test(expected = IllegalArgumentException.class)
185 public void testCreateNetworkWithNullId() {
186 final Network testNet = NeutronNetwork.builder().build();
187 target.createNetwork(testNet);
188 }
189
190 /**
191 * Tests if creating a network with null name fails with an exception.
192 */
193 @Test(expected = IllegalArgumentException.class)
194 public void testCreateNetworkWithNullName() {
195 final Network testNet = NeutronNetwork.builder().build();
196 testNet.setId(NETWORK_ID);
197 target.createNetwork(testNet);
198 }
199
200 /**
201 * Tests if creating a duplicate network fails with an exception.
202 */
203 @Test(expected = IllegalArgumentException.class)
204 public void testCreateDuplicateNetwork() {
205 target.createNetwork(NETWORK);
206 target.createNetwork(NETWORK);
207 }
208
209 /**
210 * Tests if removing network with null ID fails with an exception.
211 */
212 @Test(expected = IllegalArgumentException.class)
213 public void testRemoveNetworkWithNull() {
214 target.removeNetwork(null);
215 }
216
217 /**
218 * Tests if updating a network with null name fails with an exception.
219 */
220 @Test(expected = IllegalArgumentException.class)
221 public void testUpdateNetworkWithNullName() {
222 final Network updated = NeutronNetwork.builder()
223 .name(null)
224 .build();
225 updated.setId(NETWORK_ID);
226 target.updateNetwork(updated);
227 }
228
229 /**
230 * Tests if updating an unregistered network fails with an exception.
231 */
232 @Test(expected = IllegalArgumentException.class)
233 public void testUpdateUnregisteredNetwork() {
234 target.updateNetwork(NETWORK);
235 }
236
237 /**
238 * Tests if updating a network with null ID fails with an exception.
239 */
240 @Test(expected = IllegalArgumentException.class)
241 public void testUpdateNetworkWithNullId() {
242 final Network testNet = NeutronNetwork.builder().build();
243 target.updateNetwork(testNet);
244 }
245
246
247 /**
248 * Tests if getting all subnets returns the correct set of subnets.
249 */
250 @Test
251 public void testGetSubnets() {
252 createBasicNetworks();
253 assertEquals("Number of subnet did not match", 1, target.subnets().size());
254 }
255
256 @Test
257 public void testGetSubnetsByNetworkId() {
258 createBasicNetworks();
259 assertEquals("Subnet did not match", 1, target.subnets(NETWORK_ID).size());
260 assertEquals("Subnet did not match", 0, target.subnets(UNKNOWN_ID).size());
261 }
262
263 /**
264 * Tests if getting a subnet with ID returns the correct subnet.
265 */
266 @Test
267 public void testGetSubnetById() {
268 createBasicNetworks();
269 assertTrue("Subnet did not match", target.subnet(SUBNET_ID) != null);
270 assertTrue("Subnet did not match", target.subnet(UNKNOWN_ID) == null);
271 }
272
273 /**
274 * Tests creating and removing a subnet, and checks if it triggers proper events.
275 */
276 @Test
277 public void testCreateAndRemoveSubnet() {
278 target.createSubnet(SUBNET);
279 assertEquals("Number of subnet did not match", 1, target.subnets().size());
280 assertTrue("Subnet was not created", target.subnet(SUBNET_ID) != null);
281
282 target.removeSubnet(SUBNET_ID);
283 assertEquals("Number of subnet did not match", 0, target.subnets().size());
284 assertTrue("Subnet was not removed", target.subnet(SUBNET_ID) == null);
285
286 validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_REMOVED);
287 }
288
289 /**
290 * Tests updating a subnet, and checks if it triggers proper events.
291 */
292 @Test
293 public void testCreateAndUpdateSubnet() {
294 target.createSubnet(SUBNET_COPY);
295 assertEquals("Number of subnet did not match", 1, target.subnets().size());
296 assertEquals("Subnet did not match", null, target.subnet(SUBNET_ID).getName());
297
298 // TODO fix NeutronSubnet.builder().from() in openstack4j
299 final Subnet updated = NeutronSubnet.builder()
300 .networkId(NETWORK_ID)
301 .cidr("192.168.0.0/24")
302 .name(UPDATED_NAME)
303 .build();
304 updated.setId(SUBNET_ID);
305 target.updateSubnet(updated);
306
307 assertEquals("Number of subnet did not match", 1, target.subnets().size());
308 assertEquals("Subnet did not match", UPDATED_NAME, target.subnet(SUBNET_ID).getName());
309
310 validateEvents(OPENSTACK_SUBNET_CREATED, OPENSTACK_SUBNET_UPDATED);
311 }
312
313 /**
314 * Tests if creating a null subnet fails with an exception.
315 */
316 @Test(expected = NullPointerException.class)
317 public void testCreateNullSubnet() {
318 target.createSubnet(null);
319 }
320
321 /**
322 * Tests if creating a subnet with null ID fails with an exception.
323 */
324 @Test(expected = IllegalArgumentException.class)
325 public void testCreateSubnetWithNullId() {
326 final Subnet testSubnet = NeutronSubnet.builder()
327 .networkId(NETWORK_ID)
328 .cidr("192.168.0.0/24")
329 .build();
330 target.createSubnet(testSubnet);
331 }
332
333 /**
334 * Tests if creating subnet with null network ID fails with an exception.
335 */
336 @Test(expected = IllegalArgumentException.class)
337 public void testCreateSubnetWithNullNetworkId() {
338 final Subnet testSubnet = NeutronSubnet.builder()
339 .cidr("192.168.0.0/24")
340 .build();
341 testSubnet.setId(SUBNET_ID);
342 target.createSubnet(testSubnet);
343 }
344
345 /**
346 * Tests if creating a subnet with null CIDR fails with an exception.
347 */
348 @Test(expected = IllegalArgumentException.class)
349 public void testCreateSubnetWithNullCidr() {
350 final Subnet testSubnet = NeutronSubnet.builder()
351 .networkId(NETWORK_ID)
352 .build();
353 testSubnet.setId(SUBNET_ID);
354 target.createSubnet(testSubnet);
355 }
356
357 /**
358 * Tests if creating a duplicate subnet fails with an exception.
359 */
360 @Test(expected = IllegalArgumentException.class)
361 public void testCreateDuplicateSubnet() {
362 target.createSubnet(SUBNET);
363 target.createSubnet(SUBNET);
364 }
365
366 /**
367 * Tests if updating an unregistered subnet fails with an exception.
368 */
369 @Test(expected = IllegalArgumentException.class)
370 public void testUpdateUnregisteredSubnet() {
371 target.updateSubnet(SUBNET);
372 }
373
374 /**
375 * Tests if updating a null subnet fails with an exception.
376 */
377 @Test(expected = NullPointerException.class)
378 public void testUpdateSubnetWithNull() {
379 target.updateSubnet(null);
380 }
381
382 /**
383 * Tests if updating a subnet with null ID fails with an exception.
384 */
385 @Test(expected = IllegalArgumentException.class)
386 public void testUpdateSubnetWithNullId() {
387 final Subnet testSubnet = NeutronSubnet.builder()
388 .networkId(NETWORK_ID)
389 .cidr("192.168.0.0/24")
390 .build();
391 target.updateSubnet(testSubnet);
392 }
393
394 /**
395 * Tests if updating a subnet with null network ID fails with an exception.
396 */
397 @Test(expected = IllegalArgumentException.class)
398 public void testUpdateSubnetWithNullNetworkId() {
399 final Subnet testSubnet = NeutronSubnet.builder()
400 .cidr("192.168.0.0/24")
401 .build();
402 testSubnet.setId(SUBNET_ID);
403 target.updateSubnet(testSubnet);
404 }
405
406 /**
407 * Tests if updating a subnet with null CIDR fails with an exception.
408 */
409 @Test(expected = IllegalArgumentException.class)
410 public void testUpdateSubnetWithNullCidr() {
411 final Subnet testSubnet = NeutronSubnet.builder()
412 .networkId(NETWORK_ID)
413 .build();
414 testSubnet.setId(SUBNET_ID);
415 target.updateSubnet(testSubnet);
416 }
417
418 /**
419 * Tests if getting all ports returns correct set of values.
420 */
421 @Test
422 public void testGetPorts() {
423 createBasicNetworks();
424 assertEquals("Number of port did not match", 1, target.ports().size());
425 }
426
427 /**
428 * Tests if getting a port with network ID returns correct set of values.
429 */
430 @Test
431 public void testGetPortsByNetworkId() {
432 createBasicNetworks();
433 assertEquals("Number of port did not match", 1, target.ports(NETWORK_ID).size());
434 assertEquals("Number of port did not match", 0, target.ports(UNKNOWN_ID).size());
435 }
436
437 /**
438 * Tests if getting a port with ID returns correct value.
439 */
440 @Test
441 public void testGetPortById() {
442 createBasicNetworks();
443 assertTrue("Port did not match", target.port(PORT_ID) != null);
444 assertTrue("Port did not match", target.port(UNKNOWN_ID) == null);
445 }
446
447 /**
448 * Tests creating and removing a port, and checks if proper event is triggered.
449 */
450 @Test
451 public void testCreateAndRemovePort() {
452 target.createPort(PORT);
453 assertEquals("Number of port did not match", 1, target.ports().size());
454 assertTrue("Port was not created", target.port(PORT_ID) != null);
455
456 target.removePort(PORT_ID);
457 assertEquals("Number of port did not match", 0, target.ports().size());
458 assertTrue("Port was not created", target.port(PORT_ID) == null);
459
460 validateEvents(OPENSTACK_PORT_CREATED, OPENSTACK_PORT_REMOVED);
461 }
462
463 /**
464 * Tests creating and updating a port, and checks if proper event is triggered.
465 */
466 @Test
467 public void testCreateAndUpdatePort() {
468 target.createPort(PORT);
469 assertEquals("Number of port did not match", 1, target.ports().size());
470 assertEquals("Port did not match", null, target.port(PORT_ID).getName());
471
472 final Port updated = NeutronPort.builder()
473 .from(PORT_COPY)
474 .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) {
583 TestTools.assertAfter(100, () -> {
584 int i = 0;
585 assertEquals("Number of events did not match", types.length, testListener.events.size());
586 for (Event event : testListener.events) {
587 assertEquals("Incorrect event received", types[i], event.type());
588 i++;
589 }
590 testListener.events.clear();
591 });
592 }
593}