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