Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -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.onosproject.newoptical; |
| 17 | |
| 18 | import com.google.common.annotations.Beta; |
| 19 | import com.google.common.collect.ImmutableList; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 20 | |
| 21 | import org.apache.commons.lang3.tuple.Pair; |
| 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 27 | import org.apache.felix.scr.annotations.Service; |
| 28 | import org.onlab.util.Bandwidth; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 29 | import org.onlab.util.KryoNamespace; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 30 | import org.onosproject.cluster.ClusterService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.core.CoreService; |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 33 | import org.onosproject.event.ListenerTracker; |
| 34 | import org.onosproject.net.optical.OchPort; |
| 35 | import org.onosproject.net.optical.OduCltPort; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 36 | import org.onosproject.newoptical.api.OpticalConnectivityId; |
| 37 | import org.onosproject.newoptical.api.OpticalPathEvent; |
| 38 | import org.onosproject.newoptical.api.OpticalPathListener; |
| 39 | import org.onosproject.newoptical.api.OpticalPathService; |
| 40 | import org.onosproject.event.AbstractListenerManager; |
| 41 | import org.onosproject.mastership.MastershipService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 42 | import org.onosproject.net.ConnectPoint; |
| 43 | import org.onosproject.net.Device; |
| 44 | import org.onosproject.net.DeviceId; |
| 45 | import org.onosproject.net.Link; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 46 | import org.onosproject.net.Path; |
| 47 | import org.onosproject.net.Port; |
| 48 | import org.onosproject.net.config.NetworkConfigService; |
| 49 | import org.onosproject.net.device.DeviceService; |
| 50 | import org.onosproject.net.intent.Intent; |
| 51 | import org.onosproject.net.intent.IntentEvent; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 52 | import org.onosproject.net.intent.IntentListener; |
| 53 | import org.onosproject.net.intent.IntentService; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 54 | import org.onosproject.net.intent.OpticalCircuitIntent; |
| 55 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 56 | import org.onosproject.net.link.LinkEvent; |
| 57 | import org.onosproject.net.link.LinkListener; |
| 58 | import org.onosproject.net.link.LinkService; |
Sho SHIMIZU | b6c63a3 | 2016-05-26 12:07:19 -0700 | [diff] [blame] | 59 | import org.onosproject.net.config.basics.BandwidthCapacity; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 60 | import org.onosproject.net.resource.ContinuousResource; |
| 61 | import org.onosproject.net.resource.Resource; |
| 62 | import org.onosproject.net.resource.ResourceAllocation; |
| 63 | import org.onosproject.net.resource.ResourceService; |
| 64 | import org.onosproject.net.resource.Resources; |
| 65 | import org.onosproject.net.topology.LinkWeight; |
| 66 | import org.onosproject.net.topology.PathService; |
| 67 | import org.onosproject.net.topology.TopologyEdge; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 68 | import org.onosproject.store.serializers.KryoNamespaces; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 69 | import org.onosproject.store.service.AtomicCounter; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 70 | import org.onosproject.store.service.ConsistentMap; |
| 71 | import org.onosproject.store.service.DistributedSet; |
| 72 | import org.onosproject.store.service.MapEvent; |
| 73 | import org.onosproject.store.service.MapEventListener; |
| 74 | import org.onosproject.store.service.Serializer; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 75 | import org.onosproject.store.service.StorageService; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 76 | import org.onosproject.store.service.Versioned; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 77 | import org.slf4j.Logger; |
| 78 | import org.slf4j.LoggerFactory; |
| 79 | |
| 80 | import java.time.Duration; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 81 | import java.util.ArrayList; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 82 | import java.util.Collections; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 83 | import java.util.HashMap; |
| 84 | import java.util.HashSet; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 85 | import java.util.Iterator; |
| 86 | import java.util.LinkedList; |
| 87 | import java.util.List; |
| 88 | import java.util.Map; |
| 89 | import java.util.Optional; |
| 90 | import java.util.Set; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 91 | import java.util.stream.Collectors; |
| 92 | import java.util.stream.Stream; |
| 93 | |
| 94 | import static com.google.common.base.Preconditions.checkArgument; |
| 95 | import static com.google.common.base.Preconditions.checkNotNull; |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 96 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Main component to configure optical connectivity. |
| 100 | */ |
| 101 | @Beta |
| 102 | @Service |
| 103 | @Component(immediate = true) |
| 104 | public class OpticalPathProvisioner |
| 105 | extends AbstractListenerManager<OpticalPathEvent, OpticalPathListener> |
| 106 | implements OpticalPathService { |
| 107 | protected static final Logger log = LoggerFactory.getLogger(OpticalPathProvisioner.class); |
| 108 | |
| 109 | private static final String OPTICAL_CONNECTIVITY_ID_COUNTER = "optical-connectivity-id"; |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 110 | private static final String LINKPATH_MAP_NAME = "newoptical-linkpath"; |
| 111 | private static final String CONNECTIVITY_MAP_NAME = "newoptical-connectivity"; |
| 112 | private static final String CROSSCONNECTLINK_SET_NAME = "newoptical-crossconnectlink"; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 113 | |
| 114 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 115 | protected IntentService intentService; |
| 116 | |
| 117 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 118 | protected PathService pathService; |
| 119 | |
| 120 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 121 | protected CoreService coreService; |
| 122 | |
| 123 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 124 | protected LinkService linkService; |
| 125 | |
| 126 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 127 | protected MastershipService mastershipService; |
| 128 | |
| 129 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 130 | protected ClusterService clusterService; |
| 131 | |
| 132 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 133 | protected DeviceService deviceService; |
| 134 | |
| 135 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 136 | protected StorageService storageService; |
| 137 | |
| 138 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 139 | protected NetworkConfigService networkConfigService; |
| 140 | |
| 141 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 142 | protected ResourceService resourceService; |
| 143 | |
| 144 | |
| 145 | private ApplicationId appId; |
| 146 | |
| 147 | private AtomicCounter idCounter; |
| 148 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 149 | private ListenerTracker listeners; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 150 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 151 | private InternalStoreListener storeListener = new InternalStoreListener(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 152 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 153 | /** |
| 154 | * Map from packet-layer link expected to be realized by some optical Intent to |
| 155 | * OpticalConnectivity (~=top level intent over multi-layer topology). |
| 156 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 157 | private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 158 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 159 | private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap; |
| 160 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 161 | // FIXME in the long run. This is effectively app's own resource subsystem |
| 162 | /** |
| 163 | * Set of cross connect link currently used. |
| 164 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 165 | private DistributedSet<Link> usedCrossConnectLinkSet; |
| 166 | |
| 167 | private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder() |
| 168 | .register(KryoNamespaces.API) |
| 169 | .register(PacketLinkRealizedByOptical.class) |
| 170 | .register(OpticalConnectivityId.class) |
| 171 | .register(OpticalConnectivity.class); |
| 172 | |
| 173 | private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder() |
| 174 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 0086cf8 | 2016-07-18 22:49:45 -0700 | [diff] [blame] | 175 | .register(PacketLinkRealizedByOptical.class) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 176 | .register(OpticalConnectivityId.class) |
| 177 | .register(OpticalConnectivity.class); |
| 178 | |
| 179 | private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder() |
| 180 | .register(KryoNamespaces.API); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 181 | |
| 182 | @Activate |
| 183 | protected void activate() { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 184 | deviceService = opticalView(deviceService); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 185 | appId = coreService.registerApplication("org.onosproject.newoptical"); |
| 186 | |
Naoki Shiota | 03c29e1 | 2016-05-16 16:58:07 -0700 | [diff] [blame] | 187 | idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 188 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 189 | linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder() |
| 190 | .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build())) |
| 191 | .withName(LINKPATH_MAP_NAME) |
| 192 | .withApplicationId(appId) |
| 193 | .build(); |
| 194 | |
| 195 | connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder() |
| 196 | .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build())) |
| 197 | .withName(CONNECTIVITY_MAP_NAME) |
| 198 | .withApplicationId(appId) |
| 199 | .build(); |
| 200 | |
| 201 | usedCrossConnectLinkSet = storageService.<Link>setBuilder() |
| 202 | .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build())) |
| 203 | .withName(CROSSCONNECTLINK_SET_NAME) |
| 204 | .withApplicationId(appId) |
| 205 | .build() |
| 206 | .asDistributedSet(); |
| 207 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 208 | eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 209 | |
| 210 | listeners = new ListenerTracker(); |
| 211 | listeners.addListener(linkService, new InternalLinkListener()) |
| 212 | .addListener(intentService, new InternalIntentListener()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 213 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 214 | linkPathMap.addListener(storeListener); |
| 215 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 216 | log.info("Started"); |
| 217 | } |
| 218 | |
| 219 | @Deactivate |
| 220 | protected void deactivate() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 221 | linkPathMap.removeListener(storeListener); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 222 | listeners.removeListeners(); |
| 223 | eventDispatcher.removeSink(OpticalPathEvent.class); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 224 | |
| 225 | log.info("Stopped"); |
| 226 | } |
| 227 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 228 | /* |
| 229 | * Request packet-layer connectivity between specified ports, |
| 230 | * over packet-optical multi-layer infrastructure. |
| 231 | * |
| 232 | * Functionality-wise this is effectively submitting Packet-Optical |
| 233 | * multi-layer P2P Intent. |
| 234 | * |
| 235 | * It computes multi-layer path meeting specified constraint, |
| 236 | * and calls setupPath. |
| 237 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 238 | @Override |
| 239 | public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress, |
| 240 | Bandwidth bandwidth, Duration latency) { |
| 241 | checkNotNull(ingress); |
| 242 | checkNotNull(egress); |
| 243 | log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency); |
| 244 | |
| 245 | bandwidth = (bandwidth == null) ? Bandwidth.bps(0) : bandwidth; |
| 246 | |
| 247 | Set<Path> paths = pathService.getPaths(ingress.deviceId(), egress.deviceId(), |
| 248 | new BandwidthLinkWeight(bandwidth)); |
| 249 | if (paths.isEmpty()) { |
| 250 | log.warn("Unable to find multi-layer path."); |
| 251 | return null; |
| 252 | } |
| 253 | |
| 254 | // Search path with available cross connect points |
| 255 | for (Path path : paths) { |
| 256 | OpticalConnectivityId id = setupPath(path, bandwidth, latency); |
| 257 | if (id != null) { |
| 258 | log.info("Assigned OpticalConnectivityId: {}", id); |
| 259 | return id; |
| 260 | } |
| 261 | } |
| 262 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 263 | log.error("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 264 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 265 | return null; |
| 266 | } |
| 267 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 268 | /* |
| 269 | * Given a multi-layer path, |
| 270 | * compute a set of segments which requires |
| 271 | * OpticalConnectivity(~=OpticalConnectivityIntent or OpticalCircuitPath) |
| 272 | * to provide packet-layer connectivity. |
| 273 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 274 | @Override |
| 275 | public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) { |
| 276 | checkNotNull(path); |
| 277 | log.info("setupPath({}, {}, {})", path, bandwidth, latency); |
| 278 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 279 | // map of cross connect points (optical port -> packet port) |
| 280 | Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>(); |
| 281 | |
| 282 | // list of (src, dst) pair of optical ports between which optical path should be installed |
| 283 | List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>(); |
| 284 | |
| 285 | // Scan path to find pairs of connect points between which optical intent is installed |
| 286 | // opticalSrcPort works as a flag parameter to show scanning status |
| 287 | ConnectPoint opticalSrcPort = null; |
| 288 | for (Link link : path.links()) { |
| 289 | if (!isCrossConnectLink(link)) { |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | if (opticalSrcPort != null) { |
| 294 | // opticalSrcPort!=null means src port was already found |
| 295 | // in this case link.src() is optical layer, and link.dst() is packet layer |
| 296 | |
| 297 | // Check if types of src port and dst port matches |
| 298 | Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()), |
| 299 | "Unknown device ID"); |
| 300 | Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()), |
| 301 | "Unknown device ID"); |
| 302 | if (srcDevice.type() != dstDevice.type()) { |
| 303 | log.error("Unsupported mix of cross connect points : {}, {}", |
| 304 | srcDevice.type(), dstDevice.type()); |
| 305 | return null; |
| 306 | } |
| 307 | |
| 308 | // Update cross connect points map |
| 309 | crossConnectPointMap.put(link.src(), link.dst()); |
| 310 | |
| 311 | // Add optical ports pair to list |
| 312 | crossConnectPoints.add(Pair.of(opticalSrcPort, link.src())); |
| 313 | |
| 314 | // Reset flag parameter |
| 315 | opticalSrcPort = null; |
| 316 | } else { |
| 317 | // opticalSrcPort==null means src port was not found yet |
| 318 | // in this case link.src() is packet layer, and link.dst() is optical layer |
| 319 | |
| 320 | // Update cross connect points map |
| 321 | crossConnectPointMap.put(link.dst(), link.src()); |
| 322 | // Set opticalSrcPort to src of link (optical port) |
| 323 | opticalSrcPort = link.dst(); |
| 324 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 327 | // create intents from cross connect points |
| 328 | List<Intent> intents = createIntents(crossConnectPoints); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 329 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 330 | // create set of PacketLinkRealizedByOptical |
| 331 | Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints, |
| 332 | intents, crossConnectPointMap); |
| 333 | |
| 334 | // create OpticalConnectivity object and store information to distributed store |
| 335 | OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 336 | |
| 337 | // store cross connect port usage |
| 338 | path.links().stream().filter(this::isCrossConnectLink) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 339 | .forEach(usedCrossConnectLinkSet::add); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 340 | |
| 341 | // Submit the intents |
| 342 | for (Intent i : intents) { |
| 343 | intentService.submit(i); |
| 344 | log.debug("Submitted an intent: {}", i); |
| 345 | } |
| 346 | |
| 347 | return connectivity.id(); |
| 348 | } |
| 349 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 350 | private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency, |
| 351 | Set<PacketLinkRealizedByOptical> links) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 352 | OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement()); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 353 | OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency, |
| 354 | links, Collections.emptySet()); |
| 355 | |
| 356 | links.forEach(l -> linkPathMap.put(l, connectivity)); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 357 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 358 | // store connectivity information |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 359 | connectivityMap.put(connectivity.id(), connectivity); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 360 | |
| 361 | return connectivity; |
| 362 | } |
| 363 | |
| 364 | @Override |
| 365 | public boolean removeConnectivity(OpticalConnectivityId id) { |
| 366 | log.info("removeConnectivity({})", id); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 367 | Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 368 | |
| 369 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 370 | log.info("OpticalConnectivity with id {} not found.", id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 371 | return false; |
| 372 | } |
| 373 | |
| 374 | // TODO withdraw intent only if all of connectivities that use the optical path are withdrawn |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 375 | connectivity.value().getRealizingLinks().forEach(l -> { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 376 | Intent intent = intentService.getIntent(l.realizingIntentKey()); |
| 377 | intentService.withdraw(intent); |
| 378 | }); |
| 379 | |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | @Override |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 384 | public Optional<List<Link>> getPath(OpticalConnectivityId id) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 385 | Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 386 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 387 | log.info("OpticalConnectivity with id {} not found.", id); |
| 388 | return Optional.empty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 391 | return Optional.of(ImmutableList.copyOf(connectivity.value().links())); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Scans the list of cross connection points and returns a list of optical connectivity intents. |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 396 | * During the process, save information about packet links to given set. |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 397 | * |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 398 | * @param crossConnectPoints list of (src, dst) pair between which optical path will be set up |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 399 | * @return list of optical connectivity intents |
| 400 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 401 | private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 402 | List<Intent> intents = new LinkedList<>(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 403 | Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 404 | |
| 405 | while (itr.hasNext()) { |
| 406 | // checkArgument at start ensures we'll always have pairs of connect points |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 407 | Pair<ConnectPoint, ConnectPoint> next = itr.next(); |
| 408 | ConnectPoint src = next.getLeft(); |
| 409 | ConnectPoint dst = next.getRight(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 410 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 411 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 412 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 413 | |
| 414 | if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 415 | OduCltPort srcOCPort = (OduCltPort) srcPort; |
| 416 | OduCltPort dstOCPort = (OduCltPort) dstPort; |
| 417 | if (!srcOCPort.signalType().equals(dstOCPort.signalType())) { |
| 418 | continue; |
| 419 | } |
| 420 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 421 | // Create OTN circuit |
| 422 | OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder() |
| 423 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 424 | .src(src) |
| 425 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 426 | .signalType(srcOCPort.signalType()) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 427 | .bidirectional(true) |
| 428 | .build(); |
| 429 | intents.add(circuitIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 430 | } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 431 | OchPort srcOchPort = (OchPort) srcPort; |
| 432 | OchPort dstOchPort = (OchPort) dstPort; |
| 433 | if (!srcOchPort.signalType().equals(dstOchPort.signalType())) { |
| 434 | continue; |
| 435 | } |
| 436 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 437 | // Create lightpath |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 438 | OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder() |
| 439 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 440 | .src(src) |
| 441 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 442 | .signalType(srcOchPort.signalType()) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 443 | .bidirectional(true) |
| 444 | .build(); |
| 445 | intents.add(opticalIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 446 | } else { |
| 447 | log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type()); |
| 448 | return Collections.emptyList(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | return intents; |
| 453 | } |
| 454 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 455 | private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints, |
| 456 | List<Intent> intents, |
| 457 | Map<ConnectPoint, ConnectPoint> crossConnectPoints) { |
| 458 | checkArgument(connectPoints.size() == intents.size()); |
| 459 | |
| 460 | Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>(); |
| 461 | |
| 462 | Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator(); |
| 463 | Iterator<Intent> intentItr = intents.iterator(); |
| 464 | while (xcPointsItr.hasNext()) { |
| 465 | Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next(); |
| 466 | Intent intent = intentItr.next(); |
| 467 | |
| 468 | ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft())); |
| 469 | ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight())); |
| 470 | |
| 471 | if (intent instanceof OpticalConnectivityIntent) { |
| 472 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 473 | (OpticalConnectivityIntent) intent)); |
| 474 | } else if (intent instanceof OpticalCircuitIntent) { |
| 475 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 476 | (OpticalCircuitIntent) intent)); |
| 477 | } else { |
| 478 | log.warn("Unexpected intent type: {}", intent.getClass()); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | return pLinks; |
| 483 | } |
| 484 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device. |
| 487 | * |
| 488 | * @param type device type |
| 489 | * @return true if in packet layer, false otherwise |
| 490 | */ |
| 491 | private boolean isPacketLayer(Device.Type type) { |
| 492 | return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * Verifies if given device type is in packet layer, i.e., switch or router device. |
| 497 | * |
| 498 | * @param type device type |
| 499 | * @return true if in packet layer, false otherwise |
| 500 | */ |
| 501 | private boolean isTransportLayer(Device.Type type) { |
| 502 | return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Verifies if given link forms a cross-connection between packet and optical layer. |
| 507 | * |
| 508 | * @param link the link |
| 509 | * @return true if the link is a cross-connect link, false otherwise |
| 510 | */ |
| 511 | private boolean isCrossConnectLink(Link link) { |
| 512 | if (link.type() != Link.Type.OPTICAL) { |
| 513 | return false; |
| 514 | } |
| 515 | |
| 516 | Device.Type src = deviceService.getDevice(link.src().deviceId()).type(); |
| 517 | Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type(); |
| 518 | |
| 519 | return src != dst && |
| 520 | ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src))); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Updates bandwidth resource of given connect point. |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 525 | * |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 526 | * @param cp Connect point |
| 527 | * @param bandwidth New bandwidth |
| 528 | */ |
| 529 | private void updatePortBandwidth(ConnectPoint cp, Bandwidth bandwidth) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 530 | log.debug("update Port {} Bandwidth {}", cp, bandwidth); |
| 531 | BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class); |
| 532 | bwCapacity.capacity(bandwidth).apply(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Updates usage information of bandwidth based on connectivity which is established. |
| 537 | * @param connectivity Optical connectivity |
| 538 | */ |
| 539 | private void updateBandwidthUsage(OpticalConnectivity connectivity) { |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 540 | OpticalConnectivityId connectivityId = connectivity.id(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 541 | |
| 542 | List<Link> links = connectivity.links(); |
| 543 | |
| 544 | List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst())) |
| 545 | .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type())) |
| 546 | .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), |
| 547 | Bandwidth.class).resource(connectivity.bandwidth().bps())) |
| 548 | .collect(Collectors.toList()); |
| 549 | |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 550 | log.debug("allocating bandwidth for {} : {}", connectivityId, resources); |
| 551 | List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 552 | if (allocations.isEmpty()) { |
| 553 | log.warn("Failed to allocate bandwidth {} to {}", |
| 554 | connectivity.bandwidth().bps(), resources); |
| 555 | // TODO any recovery? |
| 556 | } |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 557 | log.debug("Done allocating bandwidth for {}", connectivityId); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Release bandwidth allocated by given connectivity. |
| 562 | * @param connectivity Optical connectivity |
| 563 | */ |
| 564 | private void releaseBandwidthUsage(OpticalConnectivity connectivity) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 565 | if (connectivity.links().isEmpty()) { |
| 566 | return; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 567 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 568 | |
| 569 | // release resource only if this node is the master for link head device |
| 570 | if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) { |
| 571 | OpticalConnectivityId connectivityId = connectivity.id(); |
| 572 | |
| 573 | log.debug("releasing bandwidth allocated to {}", connectivityId); |
| 574 | if (!resourceService.release(connectivityId)) { |
| 575 | log.warn("Failed to release bandwidth allocated to {}", |
| 576 | connectivityId); |
| 577 | // TODO any recovery? |
| 578 | } |
| 579 | log.debug("DONE releasing bandwidth for {}", connectivityId); |
| 580 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | private class BandwidthLinkWeight implements LinkWeight { |
| 584 | private Bandwidth bandwidth = null; |
| 585 | |
| 586 | public BandwidthLinkWeight(Bandwidth bandwidth) { |
| 587 | this.bandwidth = bandwidth; |
| 588 | } |
| 589 | |
| 590 | @Override |
| 591 | public double weight(TopologyEdge edge) { |
| 592 | Link l = edge.link(); |
| 593 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 594 | // Avoid inactive links |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 595 | if (l.state() == Link.State.INACTIVE) { |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 596 | log.trace("{} is not active", l); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 597 | return -1.0; |
| 598 | } |
| 599 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 600 | // Avoid cross connect links with used ports |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 601 | if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) { |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 602 | log.trace("Cross connect {} in use", l); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 603 | return -1.0; |
| 604 | } |
| 605 | |
| 606 | // Check availability of bandwidth |
| 607 | if (bandwidth != null) { |
| 608 | if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) { |
| 609 | return 1.0; |
| 610 | } else { |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 611 | log.trace("Not enought bandwidth on {}", l); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 612 | return -1.0; |
| 613 | } |
| 614 | } else { |
| 615 | // TODO needs to differentiate optical and packet? |
| 616 | if (l.type() == Link.Type.OPTICAL) { |
| 617 | // Transport links |
| 618 | return 1.0; |
| 619 | } else { |
| 620 | // Packet links |
| 621 | return 1.0; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | private boolean hasEnoughBandwidth(ConnectPoint cp) { |
| 627 | if (cp.elementId() instanceof DeviceId) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 628 | Device device = deviceService.getDevice(cp.deviceId()); |
| 629 | Device.Type type = device.type(); |
| 630 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 631 | if (isTransportLayer(type)) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 632 | // Check if the port has enough capacity |
| 633 | Port port = deviceService.getPort(cp.deviceId(), cp.port()); |
| 634 | if (port instanceof OduCltPort || port instanceof OchPort) { |
| 635 | // Port with capacity |
| 636 | return bandwidth.bps() < port.portSpeed() * 1000000.0; |
| 637 | } else { |
| 638 | // Port without valid capacity (OMS port, etc.) |
| 639 | return true; |
| 640 | } |
| 641 | } else { |
| 642 | // Check if enough amount of bandwidth resource remains |
| 643 | ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class) |
| 644 | .resource(bandwidth.bps()); |
Yuta HIGUCHI | 806113f | 2017-03-07 22:46:20 -0800 | [diff] [blame] | 645 | try { |
| 646 | return resourceService.isAvailable(resource); |
| 647 | } catch (Exception e) { |
| 648 | log.error("Resource service failed checking availability of {}", |
| 649 | resource, e); |
| 650 | throw e; |
| 651 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 652 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 653 | } |
| 654 | return false; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | |
| 659 | public class InternalIntentListener implements IntentListener { |
| 660 | @Override |
| 661 | public void event(IntentEvent event) { |
| 662 | switch (event.type()) { |
| 663 | case INSTALLED: |
| 664 | log.info("Intent {} installed.", event.subject()); |
| 665 | updateCrossConnectLink(event.subject()); |
| 666 | break; |
| 667 | case WITHDRAWN: |
| 668 | log.info("Intent {} withdrawn.", event.subject()); |
| 669 | removeCrossConnectLinks(event.subject()); |
| 670 | break; |
| 671 | case FAILED: |
| 672 | log.info("Intent {} failed.", event.subject()); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 673 | // TODO If it was one of it's own optical Intent, |
| 674 | // update link state |
| 675 | // TODO If it was packet P2P Intent, call setupConnectivity |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 676 | break; |
| 677 | default: |
| 678 | break; |
| 679 | } |
| 680 | } |
| 681 | |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 682 | // TODO rename "CrossConnectLink"? |
| 683 | /** |
| 684 | * Update packet-layer link/port state once Intent is installed. |
| 685 | * |
| 686 | * @param intent which reached installed state |
| 687 | */ |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 688 | private void updateCrossConnectLink(Intent intent) { |
| 689 | linkPathMap.entrySet().stream() |
| 690 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 691 | .forEach(e -> { |
| 692 | ConnectPoint packetSrc = e.getKey().src(); |
| 693 | ConnectPoint packetDst = e.getKey().dst(); |
| 694 | Bandwidth bw = e.getKey().bandwidth(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 695 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 696 | // reflect modification only if packetSrc is local_ |
| 697 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 698 | // Updates bandwidth of packet ports |
| 699 | updatePortBandwidth(packetSrc, bw); |
| 700 | updatePortBandwidth(packetDst, bw); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 701 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 702 | // Updates link status in distributed map |
| 703 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 704 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, true)); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 705 | |
| 706 | // TODO inject expected link or durable link |
| 707 | // if packet device cannot advertise packet link |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 708 | } |
| 709 | }); |
| 710 | } |
| 711 | |
| 712 | private void removeCrossConnectLinks(Intent intent) { |
| 713 | ConnectPoint src, dst; |
| 714 | |
| 715 | if (intent instanceof OpticalCircuitIntent) { |
| 716 | OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent; |
| 717 | src = circuit.getSrc(); |
| 718 | dst = circuit.getDst(); |
| 719 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 720 | OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent; |
| 721 | src = conn.getSrc(); |
| 722 | dst = conn.getDst(); |
| 723 | } else { |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | removeXcLinkUsage(src); |
| 728 | removeXcLinkUsage(dst); |
| 729 | |
| 730 | // Set bandwidth of 0 to cross connect ports |
| 731 | Bandwidth bw = Bandwidth.bps(0); |
| 732 | linkPathMap.entrySet().stream() |
| 733 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 734 | .forEach(e -> { |
| 735 | ConnectPoint packetSrc = e.getKey().src(); |
| 736 | ConnectPoint packetDst = e.getKey().dst(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 737 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 738 | // reflect modification only if packetSrc is local_ |
| 739 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 740 | // Updates bandwidth of packet ports |
| 741 | updatePortBandwidth(packetSrc, bw); |
| 742 | updatePortBandwidth(packetDst, bw); |
| 743 | |
| 744 | // Updates link status in distributed map |
| 745 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 746 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, false)); |
Yuta HIGUCHI | e80ec2c | 2017-03-06 14:16:13 -0800 | [diff] [blame] | 747 | |
| 748 | // TODO remove expected link or durable link |
| 749 | // if packet device cannot monitor packet link |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 750 | } |
| 751 | }); |
| 752 | } |
| 753 | |
| 754 | private void removeXcLinkUsage(ConnectPoint cp) { |
| 755 | Optional<Link> link = linkService.getLinks(cp).stream() |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 756 | .filter(usedCrossConnectLinkSet::contains) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 757 | .findAny(); |
| 758 | |
| 759 | if (!link.isPresent()) { |
| 760 | log.warn("Cross connect point {} has no cross connect link.", cp); |
| 761 | return; |
| 762 | } |
| 763 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 764 | usedCrossConnectLinkSet.remove(link.get()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 765 | } |
| 766 | } |
| 767 | |
| 768 | |
| 769 | private class InternalLinkListener implements LinkListener { |
| 770 | |
| 771 | @Override |
| 772 | public void event(LinkEvent event) { |
| 773 | switch (event.type()) { |
| 774 | case LINK_REMOVED: |
| 775 | Link link = event.subject(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 776 | // updates linkPathMap only if src device of link is local |
| 777 | if (!mastershipService.isLocalMaster(link.src().deviceId())) { |
| 778 | return; |
| 779 | } |
| 780 | |
| 781 | // find all packet links that correspond to removed link |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 782 | Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream() |
| 783 | .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src())) |
| 784 | .collect(Collectors.toSet()); |
| 785 | |
| 786 | pLinks.forEach(l -> { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 787 | // remove found packet links from distributed store |
| 788 | linkPathMap.computeIfPresent(l, (plink, conn) -> { |
| 789 | // Notifies listeners if all packet links are gone |
| 790 | if (conn.isAllRealizingLinkNotEstablished()) { |
| 791 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id())); |
| 792 | } |
| 793 | return null; |
| 794 | }); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 795 | }); |
| 796 | default: |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 801 | |
| 802 | private class InternalStoreListener |
| 803 | implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> { |
| 804 | |
| 805 | @Override |
| 806 | public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) { |
| 807 | switch (event.type()) { |
| 808 | case UPDATE: |
| 809 | OpticalConnectivity oldConnectivity = event.oldValue().value(); |
| 810 | OpticalConnectivity newConnectivity = event.newValue().value(); |
| 811 | |
| 812 | if (!oldConnectivity.isAllRealizingLinkEstablished() && |
| 813 | newConnectivity.isAllRealizingLinkEstablished()) { |
| 814 | // Notifies listeners if all links are established |
| 815 | updateBandwidthUsage(newConnectivity); |
| 816 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id())); |
| 817 | } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() && |
| 818 | newConnectivity.isAllRealizingLinkNotEstablished()) { |
| 819 | // Notifies listeners if all links are gone |
| 820 | releaseBandwidthUsage(newConnectivity); |
| 821 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id())); |
| 822 | } |
| 823 | |
| 824 | break; |
| 825 | default: |
| 826 | break; |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 831 | } |
| 832 | |