Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.impl; |
| 17 | |
| 18 | import com.google.common.collect.ImmutableSet; |
| 19 | import com.google.common.collect.Maps; |
| 20 | import com.google.common.collect.Sets; |
| 21 | import com.google.common.util.concurrent.MoreExecutors; |
| 22 | import org.junit.After; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.junit.TestUtils; |
| 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
| 28 | import org.onlab.packet.VlanId; |
| 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreServiceAdapter; |
| 31 | import org.onosproject.core.DefaultApplicationId; |
| 32 | import org.onosproject.mastership.MastershipServiceAdapter; |
| 33 | import org.onosproject.net.ConnectPoint; |
| 34 | import org.onosproject.net.DefaultAnnotations; |
| 35 | import org.onosproject.net.DefaultDevice; |
| 36 | import org.onosproject.net.DefaultHost; |
| 37 | import org.onosproject.net.DefaultPort; |
| 38 | import org.onosproject.net.Device; |
| 39 | import org.onosproject.net.DeviceId; |
| 40 | import org.onosproject.net.Host; |
| 41 | import org.onosproject.net.HostId; |
| 42 | import org.onosproject.net.HostLocation; |
| 43 | import org.onosproject.net.PortNumber; |
| 44 | import org.onosproject.net.device.DeviceServiceAdapter; |
| 45 | import org.onosproject.net.host.DefaultHostDescription; |
| 46 | import org.onosproject.net.host.HostDescription; |
| 47 | import org.onosproject.net.host.HostProvider; |
| 48 | import org.onosproject.net.host.HostProviderRegistry; |
| 49 | import org.onosproject.net.host.HostProviderService; |
| 50 | import org.onosproject.net.host.HostServiceAdapter; |
| 51 | import org.onosproject.net.provider.ProviderId; |
| 52 | import org.openstack4j.model.network.Network; |
| 53 | import org.openstack4j.model.network.NetworkType; |
| 54 | import org.openstack4j.model.network.Port; |
| 55 | import org.openstack4j.openstack.networking.domain.NeutronNetwork; |
| 56 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
| 57 | |
| 58 | import java.util.Map; |
| 59 | import java.util.Set; |
| 60 | |
| 61 | import static org.junit.Assert.assertEquals; |
| 62 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID; |
| 63 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID; |
| 64 | |
| 65 | /** |
| 66 | * Unit tests for Openstack Switching Host Provider. |
| 67 | */ |
| 68 | public class OpenstackSwitchingHostProviderTest { |
| 69 | |
| 70 | private static final String PORT_ID = "65c0ee9f-d634-4522-8954-51021b570b0d"; |
| 71 | private static final String NETWORK_ID = "396f12f8-521e-4b91-8e21-2e003500433a"; |
| 72 | private static final String IP_ADDRESS = "10.10.10.2"; |
| 73 | private static final String SUBNET_ID = "d32019d3-bc6e-4319-9c1d-6722fc136a22"; |
| 74 | private static final String MAC_ADDRESS = "00:11:22:33:44:55"; |
| 75 | private static final String MAC_ADDRESS2 = "11:22:33:44:55:66"; |
| 76 | |
| 77 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 78 | private static final DefaultAnnotations ANNOTATIONS = |
| 79 | DefaultAnnotations.builder() |
| 80 | .set(ANNOTATION_NETWORK_ID, NETWORK_ID) |
| 81 | .set(ANNOTATION_PORT_ID, PORT_ID).build(); |
| 82 | |
| 83 | // Host Mac, VLAN |
| 84 | private static final ProviderId PROVIDER_ID = ProviderId.NONE; |
| 85 | private static final MacAddress HOST_MAC = MacAddress.valueOf(MAC_ADDRESS); |
| 86 | private static final MacAddress HOST_MAC2 = MacAddress.valueOf(MAC_ADDRESS2); |
| 87 | |
| 88 | private static final VlanId HOST_VLAN_UNTAGGED = VlanId.NONE; |
| 89 | private static final HostId HOST_ID_UNTAGGED = HostId.hostId(HOST_MAC, HOST_VLAN_UNTAGGED); |
| 90 | |
| 91 | private static final String SEGMENT_ID = "1"; |
| 92 | |
| 93 | // Host IP |
| 94 | private static final IpAddress HOST_IP11 = IpAddress.valueOf("10.0.1.1"); |
| 95 | |
| 96 | // Device |
| 97 | private static final DeviceId DEV_ID1 = DeviceId.deviceId("of:0000000000000001"); |
| 98 | private static final DeviceId DEV_ID2 = DeviceId.deviceId("of:0000000000000002"); |
| 99 | |
| 100 | private static final Device DEV1 = |
| 101 | new DefaultDevice(PID, DEV_ID1, Device.Type.SWITCH, "", "", "", "", null); |
| 102 | private static final Device DEV2 = |
| 103 | new DefaultDevice(PID, DEV_ID2, Device.Type.SWITCH, "", "", "", "", null); |
| 104 | |
| 105 | // Port |
| 106 | private static final PortNumber P1 = PortNumber.portNumber(1); |
| 107 | private static final PortNumber P2 = PortNumber.portNumber(2); |
| 108 | |
| 109 | // Connect Point |
| 110 | private static final ConnectPoint CP11 = new ConnectPoint(DEV_ID1, P1); |
| 111 | private static final HostLocation HOST_LOC11 = new HostLocation(CP11, 0); |
| 112 | private static final ConnectPoint CP12 = new ConnectPoint(DEV_ID1, P2); |
| 113 | private static final HostLocation HOST_LOC12 = new HostLocation(CP12, 0); |
| 114 | private static final ConnectPoint CP21 = new ConnectPoint(DEV_ID2, P1); |
| 115 | |
| 116 | private Map<HostId, Host> hostMap = Maps.newHashMap(); |
| 117 | private Map<ConnectPoint, MacAddress> macMap = Maps.newHashMap(); |
| 118 | |
| 119 | private OpenstackSwitchingHostProvider target; |
| 120 | |
| 121 | private HostDescription resultHostDesc; |
| 122 | private HostId resultHostId; |
| 123 | private HostLocation resultHostLocation; |
| 124 | |
| 125 | /** |
| 126 | * Initial setup for this unit test. |
| 127 | */ |
| 128 | @Before |
| 129 | public void setUp() { |
| 130 | target = new OpenstackSwitchingHostProvider(); |
| 131 | TestUtils.setField(target, "coreService", new TestCoreService()); |
| 132 | TestUtils.setField(target, "deviceService", new TestDeviceService()); |
| 133 | TestUtils.setField(target, "hostService", new TestHostService()); |
| 134 | TestUtils.setField(target, "mastershipService", new TestMastershipService()); |
| 135 | TestUtils.setField(target, "osNodeService", new TestOpenstackNodeService()); |
| 136 | TestUtils.setField(target, "osNetworkService", new TestOpenstackNetworkService()); |
| 137 | TestUtils.setField(target, "hostProviderRegistry", new TestHostProviderRegistry()); |
| 138 | TestUtils.setField(target, "executor", MoreExecutors.newDirectExecutorService()); |
| 139 | |
| 140 | Host host1 = new DefaultHost(PROVIDER_ID, HOST_ID_UNTAGGED, HOST_MAC, HOST_VLAN_UNTAGGED, |
| 141 | Sets.newHashSet(HOST_LOC11), Sets.newHashSet(HOST_IP11), false); |
| 142 | hostMap.put(HOST_ID_UNTAGGED, host1); |
| 143 | |
| 144 | macMap.put(CP11, HOST_MAC); |
| 145 | macMap.put(CP12, HOST_MAC); |
| 146 | macMap.put(CP21, HOST_MAC2); |
| 147 | |
| 148 | target.activate(); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Tears down this unit test. |
| 153 | */ |
| 154 | @After |
| 155 | public void tearDown() { |
| 156 | target.deactivate(); |
| 157 | target = null; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests the process port added method for new addition case. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testProcessPortAddedForNewAddition() { |
| 165 | org.onosproject.net.Port port = new DefaultPort(DEV2, P1, true, ANNOTATIONS); |
| 166 | target.processPortAdded(port); |
| 167 | |
| 168 | HostId hostId = HostId.hostId(HOST_MAC2); |
| 169 | HostDescription hostDesc = new DefaultHostDescription( |
| 170 | HOST_MAC2, |
| 171 | VlanId.NONE, |
| 172 | new HostLocation(CP21, System.currentTimeMillis()), |
| 173 | ImmutableSet.of(), |
| 174 | ANNOTATIONS |
| 175 | |
| 176 | ); |
| 177 | |
| 178 | verifyHostResult(hostId, hostDesc); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Tests the process port added method for updating case. |
| 183 | */ |
| 184 | @Test |
| 185 | public void testProcessPortAddedForUpdate() { |
| 186 | org.onosproject.net.Port port = new DefaultPort(DEV1, P1, true, ANNOTATIONS); |
| 187 | target.processPortAdded(port); |
| 188 | |
| 189 | HostId hostId = HostId.hostId(HOST_MAC); |
| 190 | HostDescription hostDesc = new DefaultHostDescription( |
| 191 | HOST_MAC, |
| 192 | VlanId.NONE, |
| 193 | new HostLocation(CP11, System.currentTimeMillis()), |
| 194 | ImmutableSet.of(), |
| 195 | ANNOTATIONS |
| 196 | ); |
| 197 | |
| 198 | verifyHostResult(hostId, hostDesc); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Tests the process port added method for migration case. |
| 203 | */ |
| 204 | @Test |
| 205 | public void testProcessPortAddedForMigration() { |
| 206 | org.onosproject.net.Port port = new DefaultPort(DEV1, P2, true, ANNOTATIONS); |
| 207 | target.processPortAdded(port); |
| 208 | |
| 209 | HostId hostId = HostId.hostId(HOST_MAC); |
| 210 | |
| 211 | verifyHostLocationResult(hostId, HOST_LOC12); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Mocks the CoreService. |
| 216 | */ |
| 217 | private class TestCoreService extends CoreServiceAdapter { |
| 218 | |
| 219 | @Override |
| 220 | public ApplicationId registerApplication(String name) { |
| 221 | return new DefaultApplicationId(100, "hostProviderTestApp"); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Mocks the DeviceService. |
| 227 | */ |
| 228 | private class TestDeviceService extends DeviceServiceAdapter { |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Mocks the HostService. |
| 233 | */ |
| 234 | private class TestHostService extends HostServiceAdapter { |
| 235 | |
| 236 | @Override |
| 237 | public Host getHost(HostId hostId) { |
| 238 | return hostMap.get(hostId); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Mocks the HostProviderService. |
| 244 | */ |
| 245 | private class TestHostProviderService implements HostProviderService { |
| 246 | |
| 247 | @Override |
| 248 | public void hostDetected(HostId hostId, HostDescription hostDescription, |
| 249 | boolean replaceIps) { |
| 250 | resultHostId = hostId; |
| 251 | resultHostDesc = hostDescription; |
| 252 | } |
| 253 | |
| 254 | @Override |
| 255 | public void hostVanished(HostId hostId) { |
| 256 | |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void removeIpFromHost(HostId hostId, IpAddress ipAddress) { |
| 261 | |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | public void removeLocationFromHost(HostId hostId, HostLocation location) { |
| 266 | |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public void addLocationToHost(HostId hostId, HostLocation location) { |
| 271 | resultHostId = hostId; |
| 272 | resultHostLocation = location; |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public HostProvider provider() { |
| 277 | return null; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Mocks the HostProviderRegistry. |
| 283 | */ |
| 284 | private class TestHostProviderRegistry implements HostProviderRegistry { |
| 285 | |
| 286 | @Override |
| 287 | public HostProviderService register(HostProvider provider) { |
| 288 | return new TestHostProviderService(); |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void unregister(HostProvider provider) { |
| 293 | |
| 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public Set<ProviderId> getProviders() { |
| 298 | return null; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Mocks the MastershipService. |
| 304 | */ |
| 305 | private class TestMastershipService extends MastershipServiceAdapter { |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Mocks the OpenstackNodeService. |
| 310 | */ |
| 311 | private class TestOpenstackNodeService extends OpenstackNodeServiceAdapter { |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Mocks the OpenstackNetworkService. |
| 316 | */ |
| 317 | private class TestOpenstackNetworkService extends OpenstackNetworkServiceAdapter { |
| 318 | |
| 319 | @Override |
| 320 | public Port port(org.onosproject.net.Port port) { |
| 321 | Port osPort = NeutronPort.builder() |
| 322 | .networkId(NETWORK_ID) |
| 323 | .fixedIp(IP_ADDRESS, SUBNET_ID) |
| 324 | .macAddress(macMap.get( |
| 325 | new ConnectPoint(port.element().id(), |
| 326 | port.number())).toString()) |
| 327 | .build(); |
| 328 | osPort.setId(PORT_ID); |
| 329 | |
| 330 | return osPort; |
| 331 | } |
| 332 | |
| 333 | @Override |
| 334 | public Network network(String networkId) { |
| 335 | Network osNetwork = NeutronNetwork.builder() |
| 336 | .networkType(NetworkType.VXLAN) |
| 337 | .segmentId(SEGMENT_ID) |
| 338 | .build(); |
| 339 | osNetwork.setId(NETWORK_ID); |
| 340 | return osNetwork; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * Verifies the HostId and HostDescription. |
| 346 | * |
| 347 | * @param hostId host identifier |
| 348 | * @param hostDesc host description |
| 349 | */ |
| 350 | private void verifyHostResult(HostId hostId, HostDescription hostDesc) { |
| 351 | assertEquals(hostId, resultHostId); |
| 352 | assertEquals(hostDesc.hwAddress(), resultHostDesc.hwAddress()); |
| 353 | assertEquals(hostDesc.annotations().value(NETWORK_ID), |
| 354 | resultHostDesc.annotations().value(NETWORK_ID)); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Verifies the HostId and HostLocation. |
| 359 | * |
| 360 | * @param hostId host identifier |
| 361 | * @param hostLocation host location |
| 362 | */ |
| 363 | private void verifyHostLocationResult(HostId hostId, HostLocation hostLocation) { |
| 364 | assertEquals(hostId, resultHostId); |
| 365 | assertEquals(hostLocation.deviceId(), resultHostLocation.deviceId()); |
| 366 | assertEquals(hostLocation.port(), resultHostLocation.port()); |
| 367 | } |
| 368 | } |