blob: 8b325a39919f77b9cf2a9dd2eadce2ab7b40481c [file] [log] [blame]
Jian Lifae7b382018-07-12 22:27:47 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.ImmutableSet;
19import com.google.common.collect.Lists;
20import com.google.common.util.concurrent.MoreExecutors;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.junit.TestUtils;
25import org.onlab.packet.IpAddress;
26import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
28import org.onosproject.core.ApplicationId;
29import org.onosproject.core.CoreServiceAdapter;
30import org.onosproject.core.DefaultApplicationId;
31import org.onosproject.event.Event;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.DefaultHost;
34import org.onosproject.net.DeviceId;
35import org.onosproject.net.Host;
36import org.onosproject.net.HostId;
37import org.onosproject.net.HostLocation;
38import org.onosproject.net.PortNumber;
39import org.onosproject.net.host.HostServiceAdapter;
40import org.onosproject.net.provider.ProviderId;
41import org.onosproject.openstacknetworking.api.InstancePort;
42import org.onosproject.openstacknetworking.api.InstancePortEvent;
43import org.onosproject.openstacknetworking.api.InstancePortListener;
44import org.onosproject.store.service.TestStorageService;
45
46import java.util.List;
47import java.util.Set;
48
49import static org.junit.Assert.assertEquals;
50import static org.junit.Assert.assertNotNull;
51import static org.junit.Assert.assertNull;
Jian Liec5c32b2018-07-13 14:28:58 +090052import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_CREATE_TIME;
Jian Lifae7b382018-07-12 22:27:47 +090053import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE;
54import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE;
Jian Liec5c32b2018-07-13 14:28:58 +090055import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATED;
Jian Lifae7b382018-07-12 22:27:47 +090056import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATING;
57import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_ENDED;
58import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_MIGRATION_STARTED;
59import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_DETECTED;
60import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_UPDATED;
61import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_PORT_VANISHED;
62import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_RESTARTED;
63import static org.onosproject.openstacknetworking.api.InstancePortEvent.Type.OPENSTACK_INSTANCE_TERMINATED;
Jian Lifae7b382018-07-12 22:27:47 +090064
65/**
66 * Unit tests for instance port manager.
67 */
68public class InstancePortManagerTest {
69
70 private static final ApplicationId TEST_APP_ID = new DefaultApplicationId(1, "test");
71
72 private static final String ANNOTATION_NETWORK_ID = "networkId";
73 private static final String ANNOTATION_PORT_ID = "portId";
74
75 private static final IpAddress IP_ADDRESS_1 = IpAddress.valueOf("1.2.3.4");
76 private static final IpAddress IP_ADDRESS_2 = IpAddress.valueOf("5.6.7.8");
77
78 private static final MacAddress MAC_ADDRESS_1 = MacAddress.valueOf("11:22:33:44:55:66");
79 private static final MacAddress MAC_ADDRESS_2 = MacAddress.valueOf("77:88:99:AA:BB:CC");
80
81 private static final DeviceId DEV_ID_1 = DeviceId.deviceId("of:0000000000000001");
82 private static final DeviceId DEV_ID_2 = DeviceId.deviceId("of:0000000000000002");
83 private static final PortNumber PORT_NUM_1 = PortNumber.portNumber(1L);
84 private static final PortNumber PORT_NUM_2 = PortNumber.portNumber(2L);
85
86 private static final VlanId VLAN_ID = VlanId.vlanId();
87 private static final ProviderId PROVIDER_ID = ProviderId.NONE;
88 private static final HostId HOST_ID_1 = HostId.hostId("00:00:11:00:00:01/1");
89 private static final HostId HOST_ID_2 = HostId.hostId("00:00:11:00:00:02/1");
90
91 private static final String NETWORK_ID_1 = "net-id-1";
92 private static final String NETWORK_ID_2 = "net-id-2";
93 private static final String PORT_ID_1 = "port-id-1";
94 private static final String PORT_ID_2 = "port-id-2";
95 private static final String UNKNOWN_ID = "port-id-x";
96
97 private static final long TIME_1 = 1L;
98 private static final long TIME_2 = 2L;
99
100 private InstancePort instancePort1;
101 private InstancePort instancePort2;
102
103 private InstancePortManager target;
104 private DistributedInstancePortStore store;
105
106 private final TestInstancePortListener testInstancePortListener = new TestInstancePortListener();
107
108 /**
109 * Initial setup for this unit test.
110 */
111 @Before
112 public void setUp() {
113
114 store = new DistributedInstancePortStore();
115 TestUtils.setField(store, "coreService", new TestCoreService());
116 TestUtils.setField(store, "storageService", new TestStorageService());
117 TestUtils.setField(store, "eventExecutor", MoreExecutors.newDirectExecutorService());
118 store.activate();
119
120 target = new InstancePortManager();
121 TestUtils.setField(target, "coreService", new TestCoreService());
122 TestUtils.setField(target, "hostService", new TestHostService());
123 target.instancePortStore = store;
124 target.addListener(testInstancePortListener);
125 target.activate();
126
127 HostLocation location1 = new HostLocation(DEV_ID_1, PORT_NUM_1, TIME_1);
128 HostLocation location2 = new HostLocation(DEV_ID_2, PORT_NUM_2, TIME_2);
129
130 DefaultAnnotations.Builder annotations1 = DefaultAnnotations.builder()
131 .set(ANNOTATION_NETWORK_ID, NETWORK_ID_1)
132 .set(ANNOTATION_PORT_ID, PORT_ID_1)
133 .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_1));
134
135 DefaultAnnotations.Builder annotations2 = DefaultAnnotations.builder()
136 .set(ANNOTATION_NETWORK_ID, NETWORK_ID_2)
137 .set(ANNOTATION_PORT_ID, PORT_ID_2)
138 .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_2));
139
140 Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_1, MAC_ADDRESS_1,
141 VLAN_ID, location1, ImmutableSet.of(IP_ADDRESS_1),
142 annotations1.build());
143 Host host2 = new DefaultHost(PROVIDER_ID, HOST_ID_2, MAC_ADDRESS_2,
144 VLAN_ID, location2, ImmutableSet.of(IP_ADDRESS_2),
145 annotations2.build());
146
147 instancePort1 = DefaultInstancePort.from(host1, ACTIVE);
148 instancePort2 = DefaultInstancePort.from(host2, INACTIVE);
149 }
150
151 /**
152 * Tears down all of this unit test.
153 */
154 @After
155 public void tearDown() {
156 target.removeListener(testInstancePortListener);
157 store.deactivate();
158 target.deactivate();
159 store = null;
160 target = null;
161 }
162
163 /**
164 * Tests if getting all instance ports returns the correct set of ports.
165 */
166 @Test
167 public void testGetInstancePorts() {
168 createBasicInstancePorts();
169 assertEquals("Number of instance port did not match",
170 2, target.instancePorts().size());
171 }
172
173 /**
174 * Tests if getting an instance port with port ID returns the correct port.
175 */
176 @Test
177 public void testGetInstancePortById() {
178 createBasicInstancePorts();
179 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
180 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_2));
181 assertNull("Instance port did not match", target.instancePort(UNKNOWN_ID));
182 }
183
184
185 /**
186 * Tests if getting an instance port with IP and network ID returns correct port.
187 */
188 @Test
189 public void testGetInstancePortByIpAndNetId() {
190 createBasicInstancePorts();
191 InstancePort port = target.instancePort(IP_ADDRESS_1, NETWORK_ID_1);
192 assertEquals("Instance port did not match", port, instancePort1);
193 }
194
195 /**
196 * Tests if getting an instance port with MAC returns correct port.
197 */
198 @Test
199 public void testGetInstancePortByMac() {
200 createBasicInstancePorts();
201 InstancePort port = target.instancePort(MAC_ADDRESS_1);
202 assertEquals("Instance port did not match", port, instancePort1);
203 }
204
205 /**
206 * Tests if getting instance ports with network ID returns correct ports.
207 */
208 @Test
209 public void testGetInstancePortsByNetId() {
210 createBasicInstancePorts();
211 Set<InstancePort> ports = target.instancePorts(NETWORK_ID_1);
212 assertEquals("Number of instance port did not match",
213 1, ports.size());
214 }
215
216 /**
217 * Tests creating and removing an instance port, and checks if it triggers proper events.
218 */
219 @Test
220 public void testCreateAndRemoveInstancePort() {
221 target.createInstancePort(instancePort1);
222 assertEquals("Number of instance port did not match",
223 1, target.instancePorts().size());
224 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
225
226 target.removeInstancePort(PORT_ID_1);
227 assertEquals("Number of instance port did not match",
228 0, target.instancePorts().size());
229 assertNull("Instance port did not match", target.instancePort(PORT_ID_1));
230
231 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_PORT_VANISHED);
232 }
233
234 /**
235 * Tests updating an instance port, and checks if it triggers proper events.
236 */
237 @Test
238 public void testCreateAndUpdateInstancePort() {
239 target.createInstancePort(instancePort1);
240 assertEquals("Number of instance port did not match",
241 1, target.instancePorts().size());
242 assertEquals("Instance port did not match", PORT_NUM_1,
243 target.instancePort(PORT_ID_1).portNumber());
244
245 HostLocation location = new HostLocation(DEV_ID_2, PORT_NUM_2, TIME_2);
246
247 DefaultAnnotations.Builder annotations = DefaultAnnotations.builder()
248 .set(ANNOTATION_NETWORK_ID, NETWORK_ID_2)
249 .set(ANNOTATION_PORT_ID, PORT_ID_1)
250 .set(ANNOTATION_CREATE_TIME, String.valueOf(TIME_2));
251
252 Host host = new DefaultHost(PROVIDER_ID, HOST_ID_2, MAC_ADDRESS_2,
253 VLAN_ID, location, ImmutableSet.of(IP_ADDRESS_2),
254 annotations.build());
255
256 final InstancePort updated = DefaultInstancePort.from(host, ACTIVE);
257 target.updateInstancePort(updated);
258
259 assertEquals("Number of instance port did not match",
260 1, target.instancePorts().size());
261 assertEquals("Instance port did not match", PORT_NUM_2,
262 target.instancePort(PORT_ID_1).portNumber());
263
264 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_PORT_UPDATED);
265 }
266
267 /**
268 * Tests if creating a null instance port fails with an exception.
269 */
270 @Test(expected = NullPointerException.class)
271 public void testCreateNullInstancePort() {
272 target.createInstancePort(null);
273 }
274
275 /**
276 * Tests if creating a duplicated instance port fails with an exception.
277 */
278 @Test(expected = IllegalArgumentException.class)
279 public void testCreateDuplicateInstancePort() {
280 target.createInstancePort(instancePort1);
281 target.createInstancePort(instancePort1);
282 }
283
284 /**
285 * Tests if removing instance port with null ID fails with an exception.
286 */
287 @Test(expected = IllegalArgumentException.class)
288 public void testRemoveInstancePortWithNull() {
289 target.removeInstancePort(null);
290 }
291
292 /**
293 * Tests if updating an unregistered instance port fails with an exception.
294 */
295 @Test(expected = IllegalArgumentException.class)
296 public void testUpdateUnregisteredInstancePort() {
297 target.updateInstancePort(instancePort1);
298 }
299
300 /**
301 * Tests if it triggers the instance termination event.
302 */
303 @Test
304 public void testTerminateInstance() {
305 InstancePort instancePort = instancePort1;
306 target.createInstancePort(instancePort);
307
308 InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE);
309 target.updateInstancePort(inactiveInstancePort);
310
311 assertEquals("Number of instance port did not match",
312 1, target.instancePorts().size());
313 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
314
Jian Liec5c32b2018-07-13 14:28:58 +0900315 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_TERMINATED);
Jian Lifae7b382018-07-12 22:27:47 +0900316 }
317
318 /**
319 * Tests if it triggers the instance restart event.
320 */
321 @Test
322 public void testRestartInstance() {
323 InstancePort instancePort = instancePort1;
324 InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE);
325
326 target.createInstancePort(inactiveInstancePort);
327 target.updateInstancePort(instancePort);
328
329 assertEquals("Number of instance port did not match",
330 1, target.instancePorts().size());
331 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
332
Jian Liec5c32b2018-07-13 14:28:58 +0900333 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_RESTARTED);
Jian Lifae7b382018-07-12 22:27:47 +0900334 }
335
336 /**
337 * Tests if it triggers the instance migration start event.
338 */
339 @Test
340 public void testMigrateInstanceStart() {
341 InstancePort instancePort = instancePort1;
342 target.createInstancePort(instancePort);
343
Jian Liec5c32b2018-07-13 14:28:58 +0900344 InstancePort migratingPort = instancePort.updateState(MIGRATING);
345 target.updateInstancePort(migratingPort);
Jian Lifae7b382018-07-12 22:27:47 +0900346
347 assertEquals("Number of instance port did not match",
348 1, target.instancePorts().size());
349 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
350
Jian Liec5c32b2018-07-13 14:28:58 +0900351 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_MIGRATION_STARTED);
Jian Lifae7b382018-07-12 22:27:47 +0900352 }
353
354 /**
355 * Tests if it triggers the instance migration end event.
356 */
357 @Test
358 public void testMigrateInstanceEnd() {
359 InstancePort instancePort = instancePort1;
Jian Liec5c32b2018-07-13 14:28:58 +0900360 InstancePort migratingPort = instancePort.updateState(MIGRATING);
361 target.createInstancePort(migratingPort);
Jian Lifae7b382018-07-12 22:27:47 +0900362
Jian Liec5c32b2018-07-13 14:28:58 +0900363 InstancePort migratedPort = instancePort.updateState(MIGRATED);
364 target.updateInstancePort(migratedPort);
Jian Lifae7b382018-07-12 22:27:47 +0900365
366 assertEquals("Number of instance port did not match",
367 1, target.instancePorts().size());
368 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
369
Jian Liec5c32b2018-07-13 14:28:58 +0900370 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_MIGRATION_ENDED);
Jian Lifae7b382018-07-12 22:27:47 +0900371 }
372
373 /**
374 * Tests if it triggers the instance removal event from termination status.
375 */
376 @Test
377 public void testRemoveInstanceFromTermination() {
378 InstancePort instancePort = instancePort1;
379 target.createInstancePort(instancePort);
380
381 InstancePort inactiveInstancePort = instancePort.updateState(INACTIVE);
382 target.updateInstancePort(inactiveInstancePort);
383
384 assertEquals("Number of instance port did not match",
385 1, target.instancePorts().size());
386 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
387
388 target.removeInstancePort(PORT_ID_1);
389
390 assertEquals("Number of instance port did not match",
391 0, target.instancePorts().size());
392 assertNull("Instance port did not match", target.instancePort(PORT_ID_1));
393
Jian Liec5c32b2018-07-13 14:28:58 +0900394 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_TERMINATED,
Jian Lifae7b382018-07-12 22:27:47 +0900395 OPENSTACK_INSTANCE_PORT_VANISHED);
396 }
397
398 /**
399 * Tests if it triggers the instance removal event from migration status.
400 */
401 @Test
402 public void testRemoveInstanceFromMigration() {
403 InstancePort instancePort = instancePort1;
404 target.createInstancePort(instancePort);
405
406 InstancePort inactiveInstancePort = instancePort.updateState(MIGRATING);
407 target.updateInstancePort(inactiveInstancePort);
408
409 assertEquals("Number of instance port did not match",
410 1, target.instancePorts().size());
411 assertNotNull("Instance port did not match", target.instancePort(PORT_ID_1));
412
413 target.removeInstancePort(PORT_ID_1);
414
415 assertEquals("Number of instance port did not match",
416 0, target.instancePorts().size());
417 assertNull("Instance port did not match", target.instancePort(PORT_ID_1));
418
Jian Liec5c32b2018-07-13 14:28:58 +0900419 validateEvents(OPENSTACK_INSTANCE_PORT_DETECTED, OPENSTACK_INSTANCE_MIGRATION_STARTED,
Jian Lifae7b382018-07-12 22:27:47 +0900420 OPENSTACK_INSTANCE_PORT_VANISHED);
421 }
422
423 private void createBasicInstancePorts() {
424 target.createInstancePort(instancePort1);
425 target.createInstancePort(instancePort2);
426 }
427
428 private static class TestCoreService extends CoreServiceAdapter {
429
430 @Override
431 public ApplicationId registerApplication(String name) {
432 return TEST_APP_ID;
433 }
434 }
435
436 private static class TestHostService extends HostServiceAdapter {
437 }
438
439 private static class TestInstancePortListener implements InstancePortListener {
440 private List<InstancePortEvent> events = Lists.newArrayList();
441
442 @Override
443 public void event(InstancePortEvent event) {
444 events.add(event);
445 }
446 }
447
448 private void validateEvents(Enum... types) {
449 int i = 0;
450 assertEquals("Number of events did not match", types.length,
451 testInstancePortListener.events.size());
452 for (Event event : testInstancePortListener.events) {
453 assertEquals("Incorrect event received", types[i], event.type());
454 i++;
455 }
456 testInstancePortListener.events.clear();
457 }
458}