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