Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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 org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.apache.felix.scr.annotations.Service; |
| 25 | import org.onlab.packet.IpAddress; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onlab.packet.VlanId; |
| 28 | import org.onlab.util.Tools; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.mastership.MastershipService; |
| 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.Device; |
| 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.HostId; |
| 36 | import org.onosproject.net.HostLocation; |
| 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.device.DeviceEvent; |
| 39 | import org.onosproject.net.device.DeviceListener; |
| 40 | import org.onosproject.net.device.DeviceService; |
| 41 | import org.onosproject.net.host.DefaultHostDescription; |
| 42 | import org.onosproject.net.host.HostDescription; |
| 43 | import org.onosproject.net.host.HostProvider; |
| 44 | import org.onosproject.net.host.HostProviderRegistry; |
| 45 | import org.onosproject.net.host.HostProviderService; |
| 46 | import org.onosproject.net.host.HostService; |
| 47 | import org.onosproject.net.provider.AbstractProvider; |
| 48 | import org.onosproject.net.provider.ProviderId; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 49 | import org.onosproject.openstacknetworking.api.Constants; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 50 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 51 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 52 | import org.onosproject.openstacknode.api.OpenstackNodeEvent; |
| 53 | import org.onosproject.openstacknode.api.OpenstackNodeListener; |
| 54 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 55 | import org.openstack4j.model.network.Network; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 56 | import org.openstack4j.model.network.NetworkType; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | import org.slf4j.LoggerFactory; |
| 59 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 60 | import java.util.Optional; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 61 | import java.util.Set; |
| 62 | import java.util.concurrent.ExecutorService; |
| 63 | import java.util.concurrent.Executors; |
| 64 | import java.util.stream.Collectors; |
| 65 | |
| 66 | import static org.onlab.util.Tools.groupedThreads; |
| 67 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 68 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_CREATE_TIME; |
| 69 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_NETWORK_ID; |
| 70 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_PORT_ID; |
| 71 | import static org.onosproject.openstacknetworking.api.Constants.ANNOTATION_SEGMENT_ID; |
daniel park | 796c2eb | 2018-03-22 17:01:51 +0900 | [diff] [blame] | 72 | import static org.onosproject.openstacknetworking.api.Constants.OPENSTACK_NETWORKING_APP_ID; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 73 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_PREFIX_VM; |
| 74 | import static org.onosproject.openstacknetworking.api.Constants.PORT_NAME_VHOST_USER_PREFIX_VM; |
Ray Milkey | 9dc5739 | 2018-06-08 08:52:31 -0700 | [diff] [blame] | 75 | import static org.onosproject.openstacknetworking.api.Constants.portNamePrefixMap; |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 76 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.vnicType; |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 77 | import static org.onosproject.openstacknode.api.OpenstackNode.NodeType.CONTROLLER; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 78 | |
| 79 | @Service |
| 80 | @Component(immediate = true) |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 81 | public final class OpenstackSwitchingHostProvider |
| 82 | extends AbstractProvider implements HostProvider { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 83 | |
| 84 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 85 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 86 | private static final String ERR_ADD_HOST = "Failed to add host: "; |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 87 | private static final String SONA_HOST_SCHEME = "sona"; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 90 | protected CoreService coreService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 91 | |
| 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 93 | protected DeviceService deviceService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 94 | |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 96 | protected HostProviderRegistry hostProviderRegistry; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 97 | |
| 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 99 | protected HostService hostService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 100 | |
| 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 102 | protected MastershipService mastershipService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 103 | |
| 104 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 105 | protected OpenstackNetworkService osNetworkService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 106 | |
| 107 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 108 | protected OpenstackNodeService osNodeService; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 109 | |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 110 | private HostProviderService hostProviderService; |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 111 | |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 112 | private final ExecutorService executor = |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 113 | Executors.newSingleThreadExecutor(groupedThreads(this.getClass().getSimpleName(), "device-event")); |
| 114 | private final InternalDeviceListener internalDeviceListener = |
| 115 | new InternalDeviceListener(); |
| 116 | private final InternalOpenstackNodeListener internalNodeListener = |
| 117 | new InternalOpenstackNodeListener(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 118 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 119 | /** |
| 120 | * Creates OpenStack switching host provider. |
| 121 | */ |
| 122 | public OpenstackSwitchingHostProvider() { |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 123 | super(new ProviderId(SONA_HOST_SCHEME, OPENSTACK_NETWORKING_APP_ID)); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | @Activate |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 127 | void activate() { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 128 | coreService.registerApplication(OPENSTACK_NETWORKING_APP_ID); |
| 129 | deviceService.addListener(internalDeviceListener); |
| 130 | osNodeService.addListener(internalNodeListener); |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 131 | hostProviderService = hostProviderRegistry.register(this); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 132 | |
| 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 137 | void deactivate() { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 138 | hostProviderRegistry.unregister(this); |
| 139 | osNodeService.removeListener(internalNodeListener); |
| 140 | deviceService.removeListener(internalDeviceListener); |
| 141 | |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 142 | executor.shutdown(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 143 | |
| 144 | log.info("Stopped"); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void triggerProbe(Host host) { |
| 149 | // no probe is required |
| 150 | } |
| 151 | |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 152 | /** |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 153 | * A helper method which logs the port addition event and performs port |
| 154 | * addition action. |
| 155 | * |
| 156 | * @param event device event |
| 157 | */ |
| 158 | protected void portAddedHelper(DeviceEvent event) { |
| 159 | log.debug("Instance port {} is detected from {}", |
| 160 | event.port().annotations().value(PORT_NAME), |
| 161 | event.subject().id()); |
| 162 | processPortAdded(event.port()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * A helper method which logs the port removal event and performs port |
| 167 | * removal action. |
| 168 | * |
| 169 | * @param event device event |
| 170 | */ |
| 171 | protected void portRemovedHelper(DeviceEvent event) { |
| 172 | log.debug("Instance port {} is removed from {}", |
| 173 | event.port().annotations().value(PORT_NAME), |
| 174 | event.subject().id()); |
| 175 | processPortRemoved(event.port()); |
| 176 | } |
| 177 | |
| 178 | /** |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 179 | * Processes port addition event. |
| 180 | * Once a port addition event is detected, it tries to create a host instance |
| 181 | * with openstack augmented host information such as networkId, portId, |
| 182 | * createTime, segmentId and notifies to host provider. |
| 183 | * |
| 184 | * @param port port object used in ONOS |
| 185 | */ |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 186 | protected void processPortAdded(Port port) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 187 | // TODO check the node state is COMPLETE |
| 188 | org.openstack4j.model.network.Port osPort = osNetworkService.port(port); |
| 189 | if (osPort == null) { |
| 190 | log.warn(ERR_ADD_HOST + "OpenStack port for {} not found", port); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | Network osNet = osNetworkService.network(osPort.getNetworkId()); |
| 195 | if (osNet == null) { |
| 196 | log.warn(ERR_ADD_HOST + "OpenStack network {} not found", |
| 197 | osPort.getNetworkId()); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (osPort.getFixedIps().isEmpty()) { |
| 202 | log.warn(ERR_ADD_HOST + "no fixed IP for port {}", osPort.getId()); |
| 203 | return; |
| 204 | } |
| 205 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 206 | MacAddress mac = MacAddress.valueOf(osPort.getMacAddress()); |
| 207 | HostId hostId = HostId.hostId(mac); |
| 208 | |
| 209 | // typically one openstack port should only be bound to one fix IP address; |
| 210 | // however, openstack4j binds multiple fixed IPs to one port, this might |
| 211 | // be a defect of openstack4j implementation |
| 212 | |
| 213 | // TODO: we need to find a way to bind multiple ports from multiple |
| 214 | // openstack networks into one host sooner or later |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 215 | Set<IpAddress> fixedIps = osPort.getFixedIps().stream() |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 216 | .map(ip -> IpAddress.valueOf(ip.getIpAddress())) |
| 217 | .collect(Collectors.toSet()); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 218 | |
| 219 | // connect point is the combination of switch ID with port number where |
| 220 | // the host is attached to |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 221 | ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number()); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 222 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 223 | long createTime = System.currentTimeMillis(); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 224 | |
Jian Li | ee8214a | 2018-07-21 20:07:28 +0900 | [diff] [blame] | 225 | // we check whether the host already attached to same locations |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 226 | Host host = hostService.getHost(hostId); |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 227 | |
| 228 | // build host annotations to include a set of meta info from neutron |
| 229 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
| 230 | .set(ANNOTATION_NETWORK_ID, osPort.getNetworkId()) |
| 231 | .set(ANNOTATION_PORT_ID, osPort.getId()) |
| 232 | .set(ANNOTATION_CREATE_TIME, String.valueOf(createTime)); |
| 233 | |
Jian Li | ee8214a | 2018-07-21 20:07:28 +0900 | [diff] [blame] | 234 | // FLAT typed network does not require segment ID |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 235 | if (osNet.getNetworkType() != NetworkType.FLAT) { |
| 236 | annotations.set(ANNOTATION_SEGMENT_ID, osNet.getProviderSegID()); |
| 237 | } |
| 238 | |
| 239 | // build host description object |
| 240 | HostDescription hostDesc = new DefaultHostDescription( |
| 241 | mac, |
| 242 | VlanId.NONE, |
| 243 | new HostLocation(connectPoint, createTime), |
| 244 | fixedIps, |
| 245 | annotations.build()); |
| 246 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 247 | if (host != null) { |
| 248 | Set<HostLocation> locations = host.locations().stream() |
| 249 | .filter(l -> l.deviceId().equals(connectPoint.deviceId())) |
| 250 | .filter(l -> l.port().equals(connectPoint.port())) |
| 251 | .collect(Collectors.toSet()); |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 252 | |
| 253 | // newly added location is not in the existing location list, |
| 254 | // therefore, we simply add this into the location list |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 255 | if (locations.size() == 0) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 256 | hostProviderService.addLocationToHost(hostId, |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 257 | new HostLocation(connectPoint, createTime)); |
| 258 | } |
| 259 | |
| 260 | // newly added location is in the existing location list, |
| 261 | // the hostDetected method invocation in turn triggers host Update event |
| 262 | if (locations.size() == 1) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 263 | hostProviderService.hostDetected(hostId, hostDesc, false); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 264 | } |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 265 | } else { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 266 | hostProviderService.hostDetected(hostId, hostDesc, false); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 267 | } |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 268 | } |
| 269 | |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 270 | /** |
| 271 | * Processes port removal event. |
| 272 | * Once a port removal event is detected, it tries to look for a host |
| 273 | * instance through host provider by giving connect point information, |
| 274 | * and vanishes it. |
| 275 | * |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 276 | * @param port ONOS port |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 277 | */ |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 278 | protected void processPortRemoved(Port port) { |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 279 | ConnectPoint connectPoint = new ConnectPoint(port.element().id(), port.number()); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 280 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 281 | Set<Host> hosts = hostService.getConnectedHosts(connectPoint); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 282 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 283 | hosts.forEach(h -> { |
| 284 | Optional<HostLocation> hostLocation = h.locations().stream() |
| 285 | .filter(l -> l.deviceId().equals(port.element().id())) |
| 286 | .filter(l -> l.port().equals(port.number())).findAny(); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 287 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 288 | // if the host contains only one filtered location, we remove the host |
| 289 | if (h.locations().size() == 1) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 290 | hostProviderService.hostVanished(h.id()); |
Jian Li | 24ec59f | 2018-05-23 19:01:25 +0900 | [diff] [blame] | 291 | } |
| 292 | |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 293 | // if the host contains multiple locations, we simply remove the |
| 294 | // host location |
| 295 | if (h.locations().size() > 1 && hostLocation.isPresent()) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 296 | hostProviderService.removeLocationFromHost(h.id(), hostLocation.get()); |
Jian Li | ec5c32b | 2018-07-13 14:28:58 +0900 | [diff] [blame] | 297 | } |
| 298 | }); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 299 | } |
| 300 | |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 301 | /** |
| 302 | * An internal device listener which listens the port events generated from |
| 303 | * OVS integration bridge. |
| 304 | */ |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 305 | private class InternalDeviceListener implements DeviceListener { |
| 306 | |
| 307 | @Override |
| 308 | public boolean isRelevant(DeviceEvent event) { |
| 309 | Device device = event.subject(); |
| 310 | if (!mastershipService.isLocalMaster(device.id())) { |
| 311 | // do not allow to proceed without mastership |
| 312 | return false; |
| 313 | } |
| 314 | Port port = event.port(); |
| 315 | if (port == null) { |
| 316 | return false; |
| 317 | } |
| 318 | String portName = port.annotations().value(PORT_NAME); |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 319 | |
| 320 | return !Strings.isNullOrEmpty(portName) && |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 321 | (portName.startsWith(PORT_NAME_PREFIX_VM) || |
| 322 | isDirectPort(portName) || |
| 323 | portName.startsWith(PORT_NAME_VHOST_USER_PREFIX_VM)); |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | private boolean isDirectPort(String portName) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 327 | return portNamePrefixMap().values().stream().anyMatch(portName::startsWith); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void event(DeviceEvent event) { |
Daniel Park | c4d0640 | 2018-05-28 15:57:37 +0900 | [diff] [blame] | 332 | log.info("Device event occurred with type {}", event.type()); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 333 | switch (event.type()) { |
| 334 | case PORT_UPDATED: |
| 335 | if (!event.port().isEnabled()) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 336 | executor.execute(() -> portRemovedHelper(event)); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 337 | } else if (event.port().isEnabled()) { |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 338 | executor.execute(() -> portAddedHelper(event)); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 339 | } |
| 340 | break; |
| 341 | case PORT_ADDED: |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 342 | executor.execute(() -> portAddedHelper(event)); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 343 | break; |
Hyunsun Moon | b7a9cd2 | 2017-02-24 11:12:53 +0900 | [diff] [blame] | 344 | case PORT_REMOVED: |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 345 | executor.execute(() -> portRemovedHelper(event)); |
Ray Milkey | d6a67c3 | 2018-02-02 10:30:35 -0800 | [diff] [blame] | 346 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 347 | default: |
| 348 | break; |
| 349 | } |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private class InternalOpenstackNodeListener implements OpenstackNodeListener { |
| 354 | |
| 355 | @Override |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 356 | public boolean isRelevant(OpenstackNodeEvent event) { |
Jian Li | 51b844c | 2018-05-31 10:59:03 +0900 | [diff] [blame] | 357 | |
| 358 | if (event.subject().type() == CONTROLLER) { |
| 359 | return false; |
| 360 | } |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 361 | // do not allow to proceed without mastership |
| 362 | Device device = deviceService.getDevice(event.subject().intgBridge()); |
Jian Li | 7ddea78 | 2018-03-30 11:17:42 +0900 | [diff] [blame] | 363 | if (device == null) { |
| 364 | return false; |
| 365 | } |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 366 | return mastershipService.isLocalMaster(device.id()); |
| 367 | } |
| 368 | |
| 369 | @Override |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 370 | public void event(OpenstackNodeEvent event) { |
| 371 | OpenstackNode osNode = event.subject(); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 372 | |
| 373 | switch (event.type()) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 374 | case OPENSTACK_NODE_COMPLETE: |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 375 | log.info("COMPLETE node {} is detected", osNode.hostname()); |
| 376 | executor.execute(() -> processCompleteNode(event.subject())); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 377 | break; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 378 | case OPENSTACK_NODE_INCOMPLETE: |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 379 | log.warn("{} is changed to INCOMPLETE state", osNode); |
| 380 | break; |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 381 | case OPENSTACK_NODE_CREATED: |
| 382 | case OPENSTACK_NODE_UPDATED: |
| 383 | case OPENSTACK_NODE_REMOVED: |
Jian Li | 9d676fb | 2018-03-02 15:25:36 +0900 | [diff] [blame] | 384 | // not reacts to the events other than complete and incomplete states |
| 385 | break; |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 386 | default: |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | private void processCompleteNode(OpenstackNode osNode) { |
Hyunsun Moon | 0d45736 | 2017-06-27 17:19:41 +0900 | [diff] [blame] | 392 | deviceService.getPorts(osNode.intgBridge()).stream() |
Daniel Park | 7e8c4d8 | 2018-08-13 23:47:49 +0900 | [diff] [blame] | 393 | .filter(port -> vnicType(port.annotations().value(PORT_NAME)).equals(Constants.VnicType.NORMAL) || |
| 394 | vnicType(port.annotations().value(PORT_NAME)).equals(Constants.VnicType.DIRECT)) |
| 395 | .filter(Port::isEnabled) |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 396 | .forEach(port -> { |
| 397 | log.debug("Instance port {} is detected from {}", |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 398 | port.annotations().value(PORT_NAME), |
| 399 | osNode.hostname()); |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 400 | processPortAdded(port); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 401 | }); |
| 402 | |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 403 | Tools.stream(hostService.getHosts()) |
| 404 | .filter(host -> deviceService.getPort( |
| 405 | host.location().deviceId(), |
| 406 | host.location().port()) == null) |
| 407 | .forEach(host -> { |
| 408 | log.info("Remove stale host {}", host.id()); |
Jian Li | 07598ff | 2018-07-23 18:34:34 +0900 | [diff] [blame] | 409 | hostProviderService.hostVanished(host.id()); |
Jian Li | 46b7400 | 2018-07-15 18:39:08 +0900 | [diff] [blame] | 410 | }); |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | } |