blob: 7fcc03ee87a5688ae2abfc143bd988bbb2578cc2 [file] [log] [blame]
Jian Li07598ff2018-07-23 18:34:34 +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.Maps;
Jian Li07598ff2018-07-23 18:34:34 +090020import 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.mastership.MastershipServiceAdapter;
32import org.onosproject.net.ConnectPoint;
33import org.onosproject.net.DefaultAnnotations;
34import org.onosproject.net.DefaultDevice;
35import org.onosproject.net.DefaultHost;
36import org.onosproject.net.DefaultPort;
37import org.onosproject.net.Device;
38import org.onosproject.net.DeviceId;
39import org.onosproject.net.Host;
40import org.onosproject.net.HostId;
41import org.onosproject.net.HostLocation;
42import org.onosproject.net.PortNumber;
Daniel Park4eec9652018-08-29 14:32:26 +090043import org.onosproject.net.device.DeviceEvent;
Jian Li07598ff2018-07-23 18:34:34 +090044import org.onosproject.net.device.DeviceServiceAdapter;
45import org.onosproject.net.host.DefaultHostDescription;
46import org.onosproject.net.host.HostDescription;
47import org.onosproject.net.host.HostProvider;
48import org.onosproject.net.host.HostProviderRegistry;
49import org.onosproject.net.host.HostProviderService;
50import org.onosproject.net.host.HostServiceAdapter;
51import org.onosproject.net.provider.ProviderId;
52import org.openstack4j.model.network.Network;
53import org.openstack4j.model.network.NetworkType;
54import org.openstack4j.model.network.Port;
55import org.openstack4j.openstack.networking.domain.NeutronNetwork;
56import org.openstack4j.openstack.networking.domain.NeutronPort;
57
58import java.util.Map;
59import java.util.Set;
60
61import static org.junit.Assert.assertEquals;
Daniel Park4eec9652018-08-29 14:32:26 +090062import static org.junit.Assert.assertNull;
63import static org.junit.Assert.assertTrue;
Jian Li07598ff2018-07-23 18:34:34 +090064import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID;
65import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID;
66
67/**
68 * Unit tests for Openstack Switching Host Provider.
69 */
70public class OpenstackSwitchingHostProviderTest {
71
72 private static final String PORT_ID = "65c0ee9f-d634-4522-8954-51021b570b0d";
Daniel Park4eec9652018-08-29 14:32:26 +090073 private static final String PORT_NAME = "tap123456";
74
Jian Li07598ff2018-07-23 18:34:34 +090075 private static final String NETWORK_ID = "396f12f8-521e-4b91-8e21-2e003500433a";
76 private static final String IP_ADDRESS = "10.10.10.2";
77 private static final String SUBNET_ID = "d32019d3-bc6e-4319-9c1d-6722fc136a22";
78 private static final String MAC_ADDRESS = "00:11:22:33:44:55";
79 private static final String MAC_ADDRESS2 = "11:22:33:44:55:66";
80
81 private static final ProviderId PID = new ProviderId("of", "foo");
82 private static final DefaultAnnotations ANNOTATIONS =
83 DefaultAnnotations.builder()
84 .set(ANNOTATION_NETWORK_ID, NETWORK_ID)
Daniel Park4eec9652018-08-29 14:32:26 +090085 .set(ANNOTATION_PORT_ID, PORT_ID)
86 .set("portName", PORT_NAME).build();
Jian Li07598ff2018-07-23 18:34:34 +090087
88 // Host Mac, VLAN
89 private static final ProviderId PROVIDER_ID = ProviderId.NONE;
90 private static final MacAddress HOST_MAC = MacAddress.valueOf(MAC_ADDRESS);
91 private static final MacAddress HOST_MAC2 = MacAddress.valueOf(MAC_ADDRESS2);
92
93 private static final VlanId HOST_VLAN_UNTAGGED = VlanId.NONE;
94 private static final HostId HOST_ID_UNTAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_UNTAGGED);
95
96 private static final String SEGMENT_ID = "1";
97
98 // Host IP
99 private static final IpAddress HOST_IP11 = IpAddress.valueOf("10.0.1.1");
100
101 // Device
102 private static final DeviceId DEV_ID1 = DeviceId.deviceId("of:0000000000000001");
103 private static final DeviceId DEV_ID2 = DeviceId.deviceId("of:0000000000000002");
104
105 private static final Device DEV1 =
106 new DefaultDevice(PID, DEV_ID1, Device.Type.SWITCH, "", "", "", "", null);
107 private static final Device DEV2 =
108 new DefaultDevice(PID, DEV_ID2, Device.Type.SWITCH, "", "", "", "", null);
109
110 // Port
111 private static final PortNumber P1 = PortNumber.portNumber(1);
112 private static final PortNumber P2 = PortNumber.portNumber(2);
113
114 // Connect Point
115 private static final ConnectPoint CP11 = new ConnectPoint(DEV_ID1, P1);
116 private static final HostLocation HOST_LOC11 = new HostLocation(CP11, 0);
117 private static final ConnectPoint CP12 = new ConnectPoint(DEV_ID1, P2);
118 private static final HostLocation HOST_LOC12 = new HostLocation(CP12, 0);
119 private static final ConnectPoint CP21 = new ConnectPoint(DEV_ID2, P1);
120
121 private Map<HostId, Host> hostMap = Maps.newHashMap();
122 private Map<ConnectPoint, MacAddress> macMap = Maps.newHashMap();
123
124 private OpenstackSwitchingHostProvider target;
125
Jian Li07598ff2018-07-23 18:34:34 +0900126 /**
127 * Initial setup for this unit test.
128 */
129 @Before
130 public void setUp() {
131 target = new OpenstackSwitchingHostProvider();
132 TestUtils.setField(target, "coreService", new TestCoreService());
133 TestUtils.setField(target, "deviceService", new TestDeviceService());
134 TestUtils.setField(target, "hostService", new TestHostService());
135 TestUtils.setField(target, "mastershipService", new TestMastershipService());
136 TestUtils.setField(target, "osNodeService", new TestOpenstackNodeService());
137 TestUtils.setField(target, "osNetworkService", new TestOpenstackNetworkService());
138 TestUtils.setField(target, "hostProviderRegistry", new TestHostProviderRegistry());
139 TestUtils.setField(target, "executor", MoreExecutors.newDirectExecutorService());
140
Jian Li07598ff2018-07-23 18:34:34 +0900141 macMap.put(CP11, HOST_MAC);
142 macMap.put(CP12, HOST_MAC);
143 macMap.put(CP21, HOST_MAC2);
144
145 target.activate();
146 }
147
148 /**
149 * Tears down this unit test.
150 */
151 @After
152 public void tearDown() {
153 target.deactivate();
154 target = null;
155 }
156
157 /**
158 * Tests the process port added method for new addition case.
159 */
160 @Test
161 public void testProcessPortAddedForNewAddition() {
162 org.onosproject.net.Port port = new DefaultPort(DEV2, P1, true, ANNOTATIONS);
Daniel Park4eec9652018-08-29 14:32:26 +0900163 DeviceEvent event = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV2, port);
164
165 target.portAddedHelper(event);
Jian Li07598ff2018-07-23 18:34:34 +0900166
167 HostId hostId = HostId.hostId(HOST_MAC2);
168 HostDescription hostDesc = new DefaultHostDescription(
169 HOST_MAC2,
170 VlanId.NONE,
171 new HostLocation(CP21, System.currentTimeMillis()),
Daniel Park4eec9652018-08-29 14:32:26 +0900172 ImmutableSet.of(HOST_IP11),
Jian Li07598ff2018-07-23 18:34:34 +0900173 ANNOTATIONS
174
175 );
176
177 verifyHostResult(hostId, hostDesc);
178 }
179
180 /**
181 * Tests the process port added method for updating case.
182 */
183 @Test
184 public void testProcessPortAddedForUpdate() {
Daniel Park4eec9652018-08-29 14:32:26 +0900185 org.onosproject.net.Port addedPort = new DefaultPort(DEV1, P1, true, ANNOTATIONS);
186 DeviceEvent addedEvent = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV1, addedPort);
187
188 target.portAddedHelper(addedEvent);
189
190 //org.onosproject.net.Port updatedPort = new DefaultPort(DEV1, P2, true, ANNOTATIONS);
191 //DeviceEvent updatedEvent = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV1, updatedPort);
192
193 target.portAddedHelper(addedEvent);
194
Jian Li07598ff2018-07-23 18:34:34 +0900195
196 HostId hostId = HostId.hostId(HOST_MAC);
197 HostDescription hostDesc = new DefaultHostDescription(
198 HOST_MAC,
199 VlanId.NONE,
200 new HostLocation(CP11, System.currentTimeMillis()),
Daniel Park4eec9652018-08-29 14:32:26 +0900201 ImmutableSet.of(HOST_IP11),
Jian Li07598ff2018-07-23 18:34:34 +0900202 ANNOTATIONS
203 );
204
205 verifyHostResult(hostId, hostDesc);
206 }
207
Daniel Park4eec9652018-08-29 14:32:26 +0900208 @Test
209 public void testProcessPortRemoved() {
210 org.onosproject.net.Port addedPort = new DefaultPort(DEV1, P1, true, ANNOTATIONS);
211 DeviceEvent addedEvent = new DeviceEvent(DeviceEvent.Type.PORT_ADDED, DEV1, addedPort);
212
213 target.portAddedHelper(addedEvent);
214
215 org.onosproject.net.Port removedPort = new DefaultPort(DEV2, P1, true, ANNOTATIONS);
216 DeviceEvent removedEvent = new DeviceEvent(DeviceEvent.Type.PORT_REMOVED, DEV2, removedPort);
217
218 target.portRemovedHelper(removedEvent);
219
220 assertNull(target.hostService.getHost(HostId.hostId(HOST_MAC)));
221 }
222
Jian Li07598ff2018-07-23 18:34:34 +0900223 /**
224 * Tests the process port added method for migration case.
225 */
226 @Test
227 public void testProcessPortAddedForMigration() {
228 org.onosproject.net.Port port = new DefaultPort(DEV1, P2, true, ANNOTATIONS);
229 target.processPortAdded(port);
230
231 HostId hostId = HostId.hostId(HOST_MAC);
232
233 verifyHostLocationResult(hostId, HOST_LOC12);
234 }
235
236 /**
237 * Mocks the CoreService.
238 */
239 private class TestCoreService extends CoreServiceAdapter {
240
241 @Override
242 public ApplicationId registerApplication(String name) {
243 return new DefaultApplicationId(100, "hostProviderTestApp");
244 }
245 }
246
247 /**
248 * Mocks the DeviceService.
249 */
250 private class TestDeviceService extends DeviceServiceAdapter {
251 }
252
253 /**
254 * Mocks the HostService.
255 */
256 private class TestHostService extends HostServiceAdapter {
257
258 @Override
259 public Host getHost(HostId hostId) {
260 return hostMap.get(hostId);
261 }
Daniel Park4eec9652018-08-29 14:32:26 +0900262
263 @Override
264 public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
265 return ImmutableSet.copyOf(hostMap.values());
266 }
Jian Li07598ff2018-07-23 18:34:34 +0900267 }
268
269 /**
270 * Mocks the HostProviderService.
271 */
272 private class TestHostProviderService implements HostProviderService {
273
274 @Override
275 public void hostDetected(HostId hostId, HostDescription hostDescription,
276 boolean replaceIps) {
Daniel Park4eec9652018-08-29 14:32:26 +0900277 Host host = new DefaultHost(PROVIDER_ID,
278 hostId,
279 hostDescription.hwAddress(),
280 hostDescription.vlan(),
281 hostDescription.locations(),
282 ImmutableSet.copyOf(hostDescription.ipAddress()),
283 hostDescription.innerVlan(),
284 hostDescription.tpid(),
285 hostDescription.configured(),
286 hostDescription.annotations());
287
288 hostMap.put(hostId, host);
Jian Li07598ff2018-07-23 18:34:34 +0900289 }
290
291 @Override
292 public void hostVanished(HostId hostId) {
Daniel Park4eec9652018-08-29 14:32:26 +0900293 hostMap.remove(hostId);
Jian Li07598ff2018-07-23 18:34:34 +0900294 }
295
296 @Override
297 public void removeIpFromHost(HostId hostId, IpAddress ipAddress) {
298
299 }
300
301 @Override
302 public void removeLocationFromHost(HostId hostId, HostLocation location) {
303
304 }
305
306 @Override
307 public void addLocationToHost(HostId hostId, HostLocation location) {
Daniel Park4eec9652018-08-29 14:32:26 +0900308 Host oldHost = hostMap.get(hostId);
309
310 Set<HostLocation> newHostlocations = oldHost.locations();
311 newHostlocations.add(location);
312
313 Host newHost = new DefaultHost(oldHost.providerId(),
314 oldHost.id(),
315 oldHost.mac(),
316 oldHost.vlan(),
317 newHostlocations,
318 oldHost.ipAddresses(),
319 oldHost.innerVlan(),
320 oldHost.tpid(),
321 oldHost.configured(),
322 oldHost.annotations());
323
324 hostMap.put(hostId, newHost);
Jian Li07598ff2018-07-23 18:34:34 +0900325 }
326
327 @Override
328 public HostProvider provider() {
329 return null;
330 }
331 }
332
333 /**
334 * Mocks the HostProviderRegistry.
335 */
336 private class TestHostProviderRegistry implements HostProviderRegistry {
337
338 @Override
339 public HostProviderService register(HostProvider provider) {
340 return new TestHostProviderService();
341 }
342
343 @Override
344 public void unregister(HostProvider provider) {
345
346 }
347
348 @Override
349 public Set<ProviderId> getProviders() {
350 return null;
351 }
352 }
353
354 /**
355 * Mocks the MastershipService.
356 */
357 private class TestMastershipService extends MastershipServiceAdapter {
358 }
359
360 /**
361 * Mocks the OpenstackNodeService.
362 */
363 private class TestOpenstackNodeService extends OpenstackNodeServiceAdapter {
364 }
365
366 /**
367 * Mocks the OpenstackNetworkService.
368 */
369 private class TestOpenstackNetworkService extends OpenstackNetworkServiceAdapter {
370
371 @Override
372 public Port port(org.onosproject.net.Port port) {
373 Port osPort = NeutronPort.builder()
374 .networkId(NETWORK_ID)
375 .fixedIp(IP_ADDRESS, SUBNET_ID)
376 .macAddress(macMap.get(
377 new ConnectPoint(port.element().id(),
378 port.number())).toString())
379 .build();
380 osPort.setId(PORT_ID);
381
382 return osPort;
383 }
384
385 @Override
386 public Network network(String networkId) {
387 Network osNetwork = NeutronNetwork.builder()
388 .networkType(NetworkType.VXLAN)
389 .segmentId(SEGMENT_ID)
390 .build();
391 osNetwork.setId(NETWORK_ID);
392 return osNetwork;
393 }
394 }
395
396 /**
397 * Verifies the HostId and HostDescription.
398 *
399 * @param hostId host identifier
400 * @param hostDesc host description
401 */
402 private void verifyHostResult(HostId hostId, HostDescription hostDesc) {
Daniel Park4eec9652018-08-29 14:32:26 +0900403 Host host = hostMap.get(hostId);
404
405 assertEquals(hostId, host.id());
406 assertEquals(hostDesc.hwAddress(), host.mac());
Jian Li07598ff2018-07-23 18:34:34 +0900407 assertEquals(hostDesc.annotations().value(NETWORK_ID),
Daniel Park4eec9652018-08-29 14:32:26 +0900408 host.annotations().value(NETWORK_ID));
Jian Li07598ff2018-07-23 18:34:34 +0900409 }
410
411 /**
412 * Verifies the HostId and HostLocation.
413 *
414 * @param hostId host identifier
415 * @param hostLocation host location
416 */
417 private void verifyHostLocationResult(HostId hostId, HostLocation hostLocation) {
Daniel Park4eec9652018-08-29 14:32:26 +0900418 Host host = hostMap.get(hostId);
419 assertTrue(host.locations().stream().anyMatch(location -> location.equals(hostLocation)));
Jian Li07598ff2018-07-23 18:34:34 +0900420 }
421}