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