Jian Li | ecae438 | 2018-06-28 17:41:12 +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.base.Strings; |
| 19 | import com.google.common.collect.ImmutableSet; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 20 | import com.google.common.collect.Sets; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 26 | import org.apache.felix.scr.annotations.Service; |
| 27 | import org.onlab.packet.IpAddress; |
| 28 | import org.onlab.packet.MacAddress; |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 29 | import org.onosproject.cluster.ClusterService; |
| 30 | import org.onosproject.cluster.LeadershipService; |
| 31 | import org.onosproject.cluster.NodeId; |
| 32 | import org.onosproject.core.ApplicationId; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 33 | import org.onosproject.core.CoreService; |
| 34 | import org.onosproject.event.ListenerRegistry; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 35 | import org.onosproject.net.DeviceId; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 36 | import org.onosproject.net.Host; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 37 | import org.onosproject.net.HostLocation; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 38 | import org.onosproject.net.PortNumber; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 39 | import org.onosproject.net.host.HostEvent; |
| 40 | import org.onosproject.net.host.HostListener; |
| 41 | import org.onosproject.net.host.HostService; |
| 42 | import org.onosproject.openstacknetworking.api.Constants; |
| 43 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 44 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
| 45 | import org.onosproject.openstacknetworking.api.InstancePortEvent; |
| 46 | import org.onosproject.openstacknetworking.api.InstancePortListener; |
| 47 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 48 | import org.onosproject.openstacknetworking.api.InstancePortStore; |
| 49 | import org.onosproject.openstacknetworking.api.InstancePortStoreDelegate; |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 50 | import org.onosproject.openstacknetworking.api.OpenstackRouterService; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 51 | import org.slf4j.Logger; |
| 52 | |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 53 | import java.util.Objects; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 54 | import java.util.Set; |
| 55 | import java.util.stream.Collectors; |
| 56 | |
| 57 | import static com.google.common.base.Preconditions.checkArgument; |
| 58 | import static com.google.common.base.Preconditions.checkNotNull; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 59 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID; |
| 60 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 61 | import static org.onosproject.openstacknetworking.api.InstancePort.State.ACTIVE; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 62 | import static org.onosproject.openstacknetworking.api.InstancePort.State.INACTIVE; |
| 63 | import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATED; |
| 64 | import static org.onosproject.openstacknetworking.api.InstancePort.State.MIGRATING; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 65 | import static org.slf4j.LoggerFactory.getLogger; |
| 66 | |
| 67 | /** |
| 68 | * Provides implementation of administering and interfacing instance ports. |
| 69 | * It also provides instance port events for the hosts mapped to OpenStack VM interface. |
| 70 | */ |
| 71 | @Service |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 72 | @Component(immediate = true) |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 73 | public class InstancePortManager |
| 74 | extends ListenerRegistry<InstancePortEvent, InstancePortListener> |
| 75 | implements InstancePortService, InstancePortAdminService { |
| 76 | |
| 77 | protected final Logger log = getLogger(getClass()); |
| 78 | |
| 79 | private static final String MSG_INSTANCE_PORT = "Instance port %s %s"; |
| 80 | private static final String MSG_CREATED = "created"; |
| 81 | private static final String MSG_UPDATED = "updated"; |
| 82 | private static final String MSG_REMOVED = "removed"; |
| 83 | |
| 84 | private static final String ERR_NULL_INSTANCE_PORT = "Instance port cannot be null"; |
| 85 | private static final String ERR_NULL_INSTANCE_PORT_ID = "Instance port ID cannot be null"; |
| 86 | private static final String ERR_NULL_MAC_ADDRESS = "MAC address cannot be null"; |
| 87 | private static final String ERR_NULL_IP_ADDRESS = "IP address cannot be null"; |
| 88 | private static final String ERR_NULL_NETWORK_ID = "Network ID cannot be null"; |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 89 | private static final String ERR_NULL_DEVICE_ID = "Device ID cannot be null"; |
| 90 | private static final String ERR_NULL_PORT_NUMBER = "Port number cannot be null"; |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 91 | |
| 92 | private static final String ERR_IN_USE = " still in use"; |
| 93 | |
| 94 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 95 | protected CoreService coreService; |
| 96 | |
| 97 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 98 | protected InstancePortStore instancePortStore; |
| 99 | |
| 100 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 101 | protected LeadershipService leadershipService; |
| 102 | |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected ClusterService clusterService; |
| 105 | |
| 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 107 | protected HostService hostService; |
| 108 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 109 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 110 | protected OpenstackRouterService routerService; |
| 111 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 112 | private final InstancePortStoreDelegate |
| 113 | delegate = new InternalInstancePortStoreDelegate(); |
| 114 | private final InternalHostListener |
| 115 | hostListener = new InternalHostListener(); |
| 116 | |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 117 | private ApplicationId appId; |
| 118 | private NodeId localNodeId; |
| 119 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 120 | @Activate |
| 121 | protected void activate() { |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 122 | appId = coreService.registerApplication(Constants.OPENSTACK_NETWORKING_APP_ID); |
| 123 | localNodeId = clusterService.getLocalNode().id(); |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 124 | instancePortStore.setDelegate(delegate); |
| 125 | hostService.addListener(hostListener); |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 126 | leadershipService.runForLeadership(appId.name()); |
| 127 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 128 | log.info("Started"); |
| 129 | } |
| 130 | |
| 131 | @Deactivate |
| 132 | protected void deactivate() { |
| 133 | hostService.removeListener(hostListener); |
| 134 | instancePortStore.unsetDelegate(delegate); |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 135 | leadershipService.withdraw(appId.name()); |
| 136 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 137 | log.info("Stopped"); |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | public void createInstancePort(InstancePort instancePort) { |
| 142 | checkNotNull(instancePort, ERR_NULL_INSTANCE_PORT); |
| 143 | checkArgument(!Strings.isNullOrEmpty(instancePort.portId()), |
| 144 | ERR_NULL_INSTANCE_PORT_ID); |
| 145 | |
| 146 | instancePortStore.createInstancePort(instancePort); |
| 147 | log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(), |
| 148 | MSG_CREATED)); |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void updateInstancePort(InstancePort instancePort) { |
| 153 | checkNotNull(instancePort, ERR_NULL_INSTANCE_PORT); |
| 154 | checkArgument(!Strings.isNullOrEmpty(instancePort.portId()), |
| 155 | ERR_NULL_INSTANCE_PORT_ID); |
| 156 | |
Jian Li | da03ce9 | 2018-07-24 21:41:53 +0900 | [diff] [blame] | 157 | // in case OpenStack removes the port prior to OVS, we will not update |
| 158 | // the instance port as it does not exist in the store |
| 159 | if (instancePortStore.instancePort(instancePort.portId()) == null) { |
| 160 | log.warn("Unable to update instance port {}, as it does not exist", instancePort.portId()); |
| 161 | return; |
| 162 | } |
| 163 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 164 | instancePortStore.updateInstancePort(instancePort); |
| 165 | log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(), MSG_UPDATED)); |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public void removeInstancePort(String portId) { |
| 170 | checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_INSTANCE_PORT_ID); |
| 171 | |
| 172 | synchronized (this) { |
| 173 | if (isInstancePortInUse(portId)) { |
| 174 | final String error = |
| 175 | String.format(MSG_INSTANCE_PORT, portId, ERR_IN_USE); |
| 176 | throw new IllegalStateException(error); |
| 177 | } |
| 178 | InstancePort instancePort = instancePortStore.removeInstancePort(portId); |
| 179 | if (instancePort != null) { |
| 180 | log.info(String.format(MSG_INSTANCE_PORT, instancePort.portId(), MSG_REMOVED)); |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void clear() { |
| 187 | instancePortStore.clear(); |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public InstancePort instancePort(MacAddress macAddress) { |
| 192 | checkNotNull(macAddress, ERR_NULL_MAC_ADDRESS); |
| 193 | |
| 194 | return instancePortStore.instancePorts().stream() |
| 195 | .filter(port -> port.macAddress().equals(macAddress)) |
| 196 | .findFirst().orElse(null); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public InstancePort instancePort(IpAddress ipAddress, String osNetId) { |
| 201 | checkNotNull(ipAddress, ERR_NULL_IP_ADDRESS); |
| 202 | checkNotNull(osNetId, ERR_NULL_NETWORK_ID); |
| 203 | |
| 204 | return instancePortStore.instancePorts().stream() |
| 205 | .filter(port -> port.networkId().equals(osNetId)) |
| 206 | .filter(port -> port.ipAddress().equals(ipAddress)) |
| 207 | .findFirst().orElse(null); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public InstancePort instancePort(String portId) { |
| 212 | checkArgument(!Strings.isNullOrEmpty(portId), ERR_NULL_INSTANCE_PORT_ID); |
| 213 | |
| 214 | return instancePortStore.instancePort(portId); |
| 215 | } |
| 216 | |
| 217 | @Override |
Jian Li | 6343020 | 2018-08-30 16:24:09 +0900 | [diff] [blame] | 218 | public InstancePort instancePort(DeviceId deviceId, PortNumber portNumber) { |
| 219 | checkNotNull(deviceId, ERR_NULL_DEVICE_ID); |
| 220 | checkNotNull(portNumber, ERR_NULL_PORT_NUMBER); |
| 221 | |
| 222 | return instancePortStore.instancePorts().stream() |
| 223 | .filter(port -> port.deviceId().equals(deviceId)) |
| 224 | .filter(port -> port.portNumber().equals(portNumber)) |
| 225 | .findFirst().orElse(null); |
| 226 | } |
| 227 | |
| 228 | @Override |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 229 | public Set<InstancePort> instancePorts() { |
| 230 | Set<InstancePort> ports = instancePortStore.instancePorts(); |
| 231 | |
| 232 | return ImmutableSet.copyOf(ports); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Set<InstancePort> instancePorts(String osNetId) { |
| 237 | checkNotNull(osNetId, ERR_NULL_NETWORK_ID); |
| 238 | |
| 239 | Set<InstancePort> ports = instancePortStore.instancePorts().stream() |
| 240 | .filter(port -> port.networkId().equals(osNetId)) |
| 241 | .collect(Collectors.toSet()); |
| 242 | |
| 243 | return ImmutableSet.copyOf(ports); |
| 244 | } |
| 245 | |
Jian Li | c38e903 | 2018-08-09 17:08:38 +0900 | [diff] [blame] | 246 | @Override |
| 247 | public IpAddress floatingIp(String osPortId) { |
| 248 | checkNotNull(osPortId, ERR_NULL_INSTANCE_PORT_ID); |
| 249 | |
| 250 | return routerService.floatingIps().stream() |
| 251 | .filter(fip -> osPortId.equals(fip.getPortId())) |
| 252 | .filter(fip -> fip.getFloatingIpAddress() != null) |
| 253 | .map(fip -> IpAddress.valueOf(fip.getFloatingIpAddress())) |
| 254 | .findFirst().orElse(null); |
| 255 | } |
| 256 | |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 257 | private boolean isInstancePortInUse(String portId) { |
| 258 | // TODO add checking logic |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | private class InternalInstancePortStoreDelegate implements InstancePortStoreDelegate { |
| 263 | |
| 264 | @Override |
| 265 | public void notify(InstancePortEvent event) { |
| 266 | if (event != null) { |
| 267 | log.trace("send instance port event {}", event); |
| 268 | process(event); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * An internal listener that listens host event generated by HostLocationTracker |
| 275 | * in DistributedHostStore. The role of this listener is to convert host event |
| 276 | * to instance port event and post to the subscribers that have interested on |
| 277 | * this type of event. |
| 278 | */ |
| 279 | private class InternalHostListener implements HostListener { |
| 280 | |
| 281 | @Override |
| 282 | public boolean isRelevant(HostEvent event) { |
| 283 | Host host = event.subject(); |
| 284 | if (!isValidHost(host)) { |
| 285 | log.debug("Invalid host detected, ignore it {}", host); |
| 286 | return false; |
| 287 | } |
Jian Li | 078cd20 | 2018-07-23 18:58:20 +0900 | [diff] [blame] | 288 | |
| 289 | // do not allow to proceed without leadership |
| 290 | NodeId leader = leadershipService.getLeader(appId.name()); |
| 291 | return Objects.equals(localNodeId, leader); |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void event(HostEvent event) { |
| 296 | InstancePort instPort = DefaultInstancePort.from(event.subject(), ACTIVE); |
| 297 | |
| 298 | switch (event.type()) { |
| 299 | case HOST_UPDATED: |
| 300 | updateInstancePort(instPort); |
| 301 | break; |
| 302 | case HOST_ADDED: |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 303 | InstancePort existingPort = instancePort(instPort.portId()); |
| 304 | if (existingPort == null) { |
| 305 | // first time to add instance |
| 306 | createInstancePort(instPort); |
| 307 | } else { |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 308 | if (existingPort.state() == INACTIVE) { |
Jian Li | ee8214a | 2018-07-21 20:07:28 +0900 | [diff] [blame] | 309 | |
| 310 | if (instPort.deviceId().equals(existingPort.deviceId())) { |
| 311 | |
| 312 | // VM RESTART case |
| 313 | // if the ID of switch where VM is attached to is |
| 314 | // identical, we can assume that the VM was |
| 315 | // restarted in the same location; |
| 316 | // note that the switch port number where VM is |
| 317 | // attached can be varied per each restart |
| 318 | updateInstancePort(instPort); |
| 319 | } else { |
| 320 | |
| 321 | // VM COLD MIGRATION case |
| 322 | // if the ID of switch where VM is attached to is |
| 323 | // varied, we can assume that the VM was migrated |
| 324 | // to a new location |
| 325 | updateInstancePort(instPort.updateState(MIGRATING)); |
| 326 | InstancePort updated = instPort.updateState(MIGRATED); |
| 327 | updateInstancePort(updated.updatePrevLocation( |
| 328 | existingPort.deviceId(), existingPort.portNumber())); |
| 329 | } |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 330 | } |
| 331 | } |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 332 | break; |
| 333 | case HOST_REMOVED: |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 334 | // we will remove instance port from persistent store, |
Jian Li | ee8214a | 2018-07-21 20:07:28 +0900 | [diff] [blame] | 335 | // only if we receive port removal signal from neutron. |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 336 | // by default, we update the instance port state to INACTIVE |
| 337 | // to indicate the instance is terminated |
| 338 | updateInstancePort(instPort.updateState(INACTIVE)); |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 339 | break; |
| 340 | case HOST_MOVED: |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 341 | Host oldHost = event.prevSubject(); |
| 342 | Host currHost = event.subject(); |
| 343 | |
| 344 | // in the middle of VM migration |
| 345 | if (oldHost.locations().size() < currHost.locations().size()) { |
| 346 | updateInstancePort(instPort.updateState(MIGRATING)); |
| 347 | } |
| 348 | |
| 349 | // finish of VM migration |
| 350 | if (oldHost.locations().size() > currHost.locations().size()) { |
| 351 | Set<HostLocation> diff = |
| 352 | Sets.difference(oldHost.locations(), currHost.locations()); |
| 353 | HostLocation location = diff.stream().findFirst().orElse(null); |
| 354 | |
| 355 | if (location != null) { |
| 356 | InstancePort updated = instPort.updateState(MIGRATED); |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 357 | updateInstancePort(updated.updatePrevLocation( |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 358 | location.deviceId(), location.port())); |
| 359 | } |
| 360 | } |
Jian Li | ecae438 | 2018-06-28 17:41:12 +0900 | [diff] [blame] | 361 | break; |
| 362 | default: |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | private boolean isValidHost(Host host) { |
| 368 | return !host.ipAddresses().isEmpty() && |
| 369 | host.annotations().value(ANNOTATION_NETWORK_ID) != null && |
| 370 | host.annotations().value(ANNOTATION_PORT_ID) != null; |
| 371 | } |
| 372 | } |
| 373 | } |