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