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