Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 80dff97 | 2017-08-03 22:46:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [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.opencord.cordvtn.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 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; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 25 | import org.onlab.packet.VlanId; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 26 | import org.onosproject.cluster.ClusterService; |
| 27 | import org.onosproject.cluster.LeadershipService; |
| 28 | import org.onosproject.cluster.NodeId; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 29 | import org.onosproject.core.ApplicationId; |
| 30 | import org.onosproject.core.CoreService; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
Jonathan Hart | 3996048 | 2017-11-20 15:50:36 -0800 | [diff] [blame^] | 33 | import org.onosproject.net.Device; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 34 | import org.onosproject.net.Host; |
| 35 | import org.onosproject.net.HostId; |
| 36 | import org.onosproject.net.HostLocation; |
| 37 | import org.onosproject.net.Port; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.host.DefaultHostDescription; |
| 40 | import org.onosproject.net.host.HostDescription; |
| 41 | import org.onosproject.net.host.HostProvider; |
| 42 | import org.onosproject.net.host.HostProviderRegistry; |
| 43 | import org.onosproject.net.host.HostProviderService; |
| 44 | import org.onosproject.net.host.HostService; |
| 45 | import org.onosproject.net.provider.AbstractProvider; |
| 46 | import org.onosproject.net.provider.ProviderId; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 47 | import org.opencord.cordconfig.CordConfigService; |
| 48 | import org.opencord.cordconfig.access.AccessAgentData; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 49 | import org.opencord.cordvtn.api.core.Instance; |
| 50 | import org.opencord.cordvtn.api.core.InstanceService; |
| 51 | import org.opencord.cordvtn.api.core.ServiceNetworkEvent; |
| 52 | import org.opencord.cordvtn.api.core.ServiceNetworkListener; |
| 53 | import org.opencord.cordvtn.api.core.ServiceNetworkService; |
Hyunsun Moon | fd5a24e | 2016-10-19 19:15:48 -0700 | [diff] [blame] | 54 | import org.opencord.cordvtn.api.net.PortId; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 55 | import org.opencord.cordvtn.api.net.ServiceNetwork; |
| 56 | import org.opencord.cordvtn.api.net.ServicePort; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 57 | import org.slf4j.Logger; |
| 58 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 59 | import java.util.Objects; |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 60 | import java.util.Optional; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 61 | import java.util.concurrent.ExecutorService; |
Jonathan Hart | 3996048 | 2017-11-20 15:50:36 -0800 | [diff] [blame^] | 62 | import java.util.stream.StreamSupport; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 63 | |
Hyunsun Moon | 537018f | 2016-07-27 18:51:00 -0700 | [diff] [blame] | 64 | import static java.util.concurrent.Executors.newSingleThreadExecutor; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 65 | import static org.onlab.util.Tools.groupedThreads; |
Hyunsun Moon | 81a1356 | 2016-08-04 13:48:08 -0700 | [diff] [blame] | 66 | import static org.onosproject.net.AnnotationKeys.PORT_NAME; |
Hyunsun Moon | 28b358a | 2016-11-28 13:23:05 -0800 | [diff] [blame] | 67 | import static org.opencord.cordvtn.api.Constants.CORDVTN_APP_ID; |
| 68 | import static org.opencord.cordvtn.api.Constants.NOT_APPLICABLE; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 69 | import static org.opencord.cordvtn.api.net.ServiceNetwork.NetworkType.ACCESS_AGENT; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 70 | import static org.slf4j.LoggerFactory.getLogger; |
| 71 | |
| 72 | /** |
| 73 | * Adds or removes instances to network services. |
| 74 | */ |
| 75 | @Component(immediate = true) |
| 76 | @Service |
| 77 | public class InstanceManager extends AbstractProvider implements HostProvider, |
| 78 | InstanceService { |
| 79 | |
| 80 | protected final Logger log = getLogger(getClass()); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 81 | private static final String ERR_SERVICE_NETWORK = "Failed to get service network for %s"; |
| 82 | private static final String ERR_SERVICE_PORT = "Failed to get service port for %s"; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 83 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 84 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 85 | protected CoreService coreService; |
| 86 | |
| 87 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 88 | protected HostProviderRegistry hostProviderRegistry; |
| 89 | |
| 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 91 | protected DeviceService deviceService; |
| 92 | |
| 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 94 | protected HostService hostService; |
| 95 | |
| 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 97 | protected LeadershipService leadershipService; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 98 | |
| 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 100 | protected ClusterService clusterService; |
| 101 | |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 102 | // TODO get access agent container information from XOS |
| 103 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 104 | protected CordConfigService cordConfig; |
| 105 | |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 106 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 107 | protected ServiceNetworkService snetService; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 108 | |
| 109 | private final ExecutorService eventExecutor = |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 110 | newSingleThreadExecutor(groupedThreads(this.getClass().getSimpleName(), "event-handler")); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 111 | private final ServiceNetworkListener snetListener = new InternalServiceNetworkListener(); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 112 | |
| 113 | private ApplicationId appId; |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 114 | private NodeId localNodeId; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 115 | private HostProviderService hostProvider; |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
| 118 | * Creates an cordvtn host location provider. |
| 119 | */ |
| 120 | public InstanceManager() { |
| 121 | super(new ProviderId("host", CORDVTN_APP_ID)); |
| 122 | } |
| 123 | |
| 124 | @Activate |
| 125 | protected void activate() { |
| 126 | appId = coreService.registerApplication(CORDVTN_APP_ID); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 127 | localNodeId = clusterService.getLocalNode().id(); |
| 128 | leadershipService.runForLeadership(appId.name()); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 129 | |
| 130 | hostProvider = hostProviderRegistry.register(this); |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 131 | snetService.addListener(snetListener); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 132 | |
| 133 | log.info("Started"); |
| 134 | } |
| 135 | |
| 136 | @Deactivate |
| 137 | protected void deactivate() { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 138 | snetService.removeListener(snetListener); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 139 | hostProviderRegistry.unregister(this); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 140 | eventExecutor.shutdown(); |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 141 | leadershipService.withdraw(appId.name()); |
| 142 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 143 | log.info("Stopped"); |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public void triggerProbe(Host host) { |
| 148 | /* |
| 149 | * Note: In CORD deployment, we assume that all hosts are configured. |
| 150 | * Therefore no probe is required. |
| 151 | */ |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public void addInstance(ConnectPoint connectPoint) { |
| 156 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 157 | if (port == null) { |
| 158 | log.debug("No port found from {}", connectPoint); |
| 159 | return; |
| 160 | } |
| 161 | |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 162 | // TODO remove this when XOS provides access agent information |
| 163 | // and handle it the same way wit the other instances |
| 164 | if (isAccessAgent(connectPoint)) { |
| 165 | addAccessAgentInstance(connectPoint); |
| 166 | return; |
| 167 | } |
| 168 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 169 | ServicePort sport = getServicePortByPortName(port.annotations().value(PORT_NAME)); |
| 170 | if (sport == null) { |
| 171 | log.warn(String.format(ERR_SERVICE_PORT, port)); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 172 | return; |
| 173 | } |
| 174 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 175 | ServiceNetwork snet = snetService.serviceNetwork(sport.networkId()); |
| 176 | if (snet == null) { |
| 177 | final String error = String.format(ERR_SERVICE_NETWORK, sport); |
| 178 | throw new IllegalStateException(error); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 181 | // Added CREATE_TIME intentionally to trigger HOST_UPDATED event for the |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 182 | // existing instances. Fix this after adding a method to update/reinstall |
| 183 | // flow rules for the existing service instances. |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 184 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 185 | .set(Instance.NETWORK_TYPE, snet.type().name()) |
| 186 | .set(Instance.NETWORK_ID, snet.id().id()) |
| 187 | .set(Instance.PORT_ID, sport.id().id()) |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 188 | .set(Instance.CREATE_TIME, String.valueOf(System.currentTimeMillis())); |
| 189 | |
| 190 | HostDescription hostDesc = new DefaultHostDescription( |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 191 | sport.mac(), |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 192 | VlanId.NONE, |
| 193 | new HostLocation(connectPoint, System.currentTimeMillis()), |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 194 | Sets.newHashSet(sport.ip()), |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 195 | annotations.build()); |
| 196 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 197 | HostId hostId = HostId.hostId(sport.mac()); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 198 | hostProvider.hostDetected(hostId, hostDesc, false); |
| 199 | } |
| 200 | |
| 201 | @Override |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 202 | public void addInstance(HostId hostId, HostDescription description) { |
| 203 | hostProvider.hostDetected(hostId, description, false); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void removeInstance(ConnectPoint connectPoint) { |
Hyunsun Moon | 851e57f | 2017-02-23 19:38:40 +0900 | [diff] [blame] | 208 | hostService.getConnectedHosts(connectPoint).forEach(host -> { |
| 209 | hostProvider.hostVanished(host.id()); |
| 210 | }); |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | @Override |
Hyunsun Moon | d02a5a7 | 2017-02-05 22:00:05 +0900 | [diff] [blame] | 214 | public void removeInstance(HostId hostId) { |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 215 | hostProvider.hostVanished(hostId); |
| 216 | } |
| 217 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 218 | private ServicePort getServicePortByPortName(String portName) { |
| 219 | Optional<ServicePort> sport = snetService.servicePorts() |
| 220 | .stream() |
Hyunsun Moon | 5510e34 | 2017-02-23 19:41:00 +0900 | [diff] [blame] | 221 | .filter(p -> Objects.equals(p.name(), portName)) |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 222 | .findFirst(); |
| 223 | return sport.isPresent() ? sport.get() : null; |
| 224 | } |
| 225 | |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 226 | // TODO remove this when XOS provides access agent information |
| 227 | private boolean isAccessAgent(ConnectPoint connectPoint) { |
| 228 | Optional<AccessAgentData> agent = cordConfig.getAccessAgent(connectPoint.deviceId()); |
| 229 | if (!agent.isPresent() || !agent.get().getVtnLocation().isPresent()) { |
| 230 | return false; |
| 231 | } |
| 232 | return agent.get().getVtnLocation().get().port().equals(connectPoint.port()); |
| 233 | } |
| 234 | |
| 235 | // TODO remove this when XOS provides access agent information |
| 236 | private void addAccessAgentInstance(ConnectPoint connectPoint) { |
| 237 | AccessAgentData agent = cordConfig.getAccessAgent(connectPoint.deviceId()).get(); |
| 238 | DefaultAnnotations.Builder annotations = DefaultAnnotations.builder() |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 239 | .set(Instance.NETWORK_TYPE, ACCESS_AGENT.name()) |
| 240 | .set(Instance.NETWORK_ID, NOT_APPLICABLE) |
Hyunsun Moon | 1e88fef | 2016-08-04 14:00:35 -0700 | [diff] [blame] | 241 | .set(Instance.PORT_ID, NOT_APPLICABLE) |
| 242 | .set(Instance.CREATE_TIME, String.valueOf(System.currentTimeMillis())); |
| 243 | |
| 244 | HostDescription hostDesc = new DefaultHostDescription( |
| 245 | agent.getAgentMac(), |
| 246 | VlanId.NONE, |
| 247 | new HostLocation(connectPoint, System.currentTimeMillis()), |
| 248 | Sets.newHashSet(), |
| 249 | annotations.build()); |
| 250 | |
| 251 | HostId hostId = HostId.hostId(agent.getAgentMac()); |
| 252 | hostProvider.hostDetected(hostId, hostDesc, false); |
| 253 | } |
| 254 | |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 255 | private Instance getInstance(PortId portId) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 256 | // TODO use instance service instead |
| 257 | ServicePort sport = snetService.servicePort(portId); |
| 258 | if (sport == null) { |
| 259 | final String error = String.format(ERR_SERVICE_PORT, portId); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 260 | throw new IllegalStateException(error); |
| 261 | } |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 262 | Host host = hostService.getHost(HostId.hostId(sport.mac())); |
Hyunsun Moon | 5c14395 | 2016-10-19 18:34:46 -0700 | [diff] [blame] | 263 | if (host == null) { |
| 264 | return null; |
| 265 | } |
| 266 | return Instance.of(host); |
| 267 | } |
| 268 | |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 269 | private class InternalServiceNetworkListener implements ServiceNetworkListener { |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 270 | |
| 271 | @Override |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 272 | public void event(ServiceNetworkEvent event) { |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 273 | eventExecutor.execute(() -> { |
| 274 | NodeId leader = leadershipService.getLeader(appId.name()); |
| 275 | if (!Objects.equals(localNodeId, leader)) { |
| 276 | // do not allow to proceed without leadership |
| 277 | return; |
| 278 | } |
| 279 | handle(event); |
| 280 | }); |
| 281 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 282 | |
Jonathan Hart | 3996048 | 2017-11-20 15:50:36 -0800 | [diff] [blame^] | 283 | private boolean hasPort(Device device, String portName) { |
| 284 | return deviceService.getPorts(device.id()).stream() |
| 285 | .anyMatch(p -> p.annotations().value(PORT_NAME).equals(portName)); |
| 286 | } |
| 287 | |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 288 | private void handle(ServiceNetworkEvent event) { |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 289 | switch (event.type()) { |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 290 | case SERVICE_PORT_CREATED: |
| 291 | case SERVICE_PORT_UPDATED: |
| 292 | log.debug("Processing service port {}", event.servicePort()); |
| 293 | PortId portId = event.servicePort().id(); |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 294 | Instance instance = getInstance(portId); |
Jonathan Hart | 3996048 | 2017-11-20 15:50:36 -0800 | [diff] [blame^] | 295 | if (instance == null) { |
| 296 | String portName = event.servicePort().name(); |
| 297 | |
| 298 | Optional<Device> device = |
| 299 | StreamSupport.stream(deviceService.getAvailableDevices().spliterator(), false) |
| 300 | .filter(d -> hasPort(d, portName)) |
| 301 | .findAny(); |
| 302 | |
| 303 | Optional<Port> port = device.flatMap(d -> deviceService.getPorts(d.id()).stream() |
| 304 | .filter(p -> p.annotations().value(PORT_NAME) != null) |
| 305 | .filter(p -> p.annotations().value(PORT_NAME).equals(portName)) |
| 306 | .findAny()); |
| 307 | |
| 308 | if (device.isPresent() && port.isPresent()) { |
| 309 | addInstance(new ConnectPoint(device.get().id(), port.get().number())); |
| 310 | } |
Hyunsun Moon | 2c3f0ee | 2017-04-06 16:47:21 +0900 | [diff] [blame] | 311 | } |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 312 | break; |
Hyunsun Moon | 187bf53 | 2017-01-19 10:57:40 +0900 | [diff] [blame] | 313 | case SERVICE_PORT_REMOVED: |
| 314 | case SERVICE_NETWORK_CREATED: |
| 315 | case SERVICE_NETWORK_UPDATED: |
| 316 | case SERVICE_NETWORK_REMOVED: |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 317 | default: |
Hyunsun Moon | eaf75e6 | 2016-09-27 16:40:23 -0700 | [diff] [blame] | 318 | // do nothing for the other events |
Hyunsun Moon | 5401aaa | 2016-06-12 17:40:34 -0700 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |