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 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 153 | private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 154 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 155 | private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap; |
| 156 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 157 | // Map of cross connect link and installed path which uses the link |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 158 | private DistributedSet<Link> usedCrossConnectLinkSet; |
| 159 | |
| 160 | private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder() |
| 161 | .register(KryoNamespaces.API) |
| 162 | .register(PacketLinkRealizedByOptical.class) |
| 163 | .register(OpticalConnectivityId.class) |
| 164 | .register(OpticalConnectivity.class); |
| 165 | |
| 166 | private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder() |
| 167 | .register(KryoNamespaces.API) |
Yuta HIGUCHI | 0086cf8 | 2016-07-18 22:49:45 -0700 | [diff] [blame] | 168 | .register(PacketLinkRealizedByOptical.class) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 169 | .register(OpticalConnectivityId.class) |
| 170 | .register(OpticalConnectivity.class); |
| 171 | |
| 172 | private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder() |
| 173 | .register(KryoNamespaces.API); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 174 | |
| 175 | @Activate |
| 176 | protected void activate() { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 177 | deviceService = opticalView(deviceService); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 178 | appId = coreService.registerApplication("org.onosproject.newoptical"); |
| 179 | |
Naoki Shiota | 03c29e1 | 2016-05-16 16:58:07 -0700 | [diff] [blame] | 180 | idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 181 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 182 | linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder() |
| 183 | .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build())) |
| 184 | .withName(LINKPATH_MAP_NAME) |
| 185 | .withApplicationId(appId) |
| 186 | .build(); |
| 187 | |
| 188 | connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder() |
| 189 | .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build())) |
| 190 | .withName(CONNECTIVITY_MAP_NAME) |
| 191 | .withApplicationId(appId) |
| 192 | .build(); |
| 193 | |
| 194 | usedCrossConnectLinkSet = storageService.<Link>setBuilder() |
| 195 | .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build())) |
| 196 | .withName(CROSSCONNECTLINK_SET_NAME) |
| 197 | .withApplicationId(appId) |
| 198 | .build() |
| 199 | .asDistributedSet(); |
| 200 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 201 | eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 202 | |
| 203 | listeners = new ListenerTracker(); |
| 204 | listeners.addListener(linkService, new InternalLinkListener()) |
| 205 | .addListener(intentService, new InternalIntentListener()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 206 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 207 | linkPathMap.addListener(storeListener); |
| 208 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 209 | log.info("Started"); |
| 210 | } |
| 211 | |
| 212 | @Deactivate |
| 213 | protected void deactivate() { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 214 | linkPathMap.removeListener(storeListener); |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 215 | listeners.removeListeners(); |
| 216 | eventDispatcher.removeSink(OpticalPathEvent.class); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 217 | |
| 218 | log.info("Stopped"); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress, |
| 223 | Bandwidth bandwidth, Duration latency) { |
| 224 | checkNotNull(ingress); |
| 225 | checkNotNull(egress); |
| 226 | log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency); |
| 227 | |
| 228 | bandwidth = (bandwidth == null) ? Bandwidth.bps(0) : bandwidth; |
| 229 | |
| 230 | Set<Path> paths = pathService.getPaths(ingress.deviceId(), egress.deviceId(), |
| 231 | new BandwidthLinkWeight(bandwidth)); |
| 232 | if (paths.isEmpty()) { |
| 233 | log.warn("Unable to find multi-layer path."); |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | // Search path with available cross connect points |
| 238 | for (Path path : paths) { |
| 239 | OpticalConnectivityId id = setupPath(path, bandwidth, latency); |
| 240 | if (id != null) { |
| 241 | log.info("Assigned OpticalConnectivityId: {}", id); |
| 242 | return id; |
| 243 | } |
| 244 | } |
| 245 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 246 | log.info("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency); |
| 247 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 248 | return null; |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) { |
| 253 | checkNotNull(path); |
| 254 | log.info("setupPath({}, {}, {})", path, bandwidth, latency); |
| 255 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 256 | // map of cross connect points (optical port -> packet port) |
| 257 | Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>(); |
| 258 | |
| 259 | // list of (src, dst) pair of optical ports between which optical path should be installed |
| 260 | List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>(); |
| 261 | |
| 262 | // Scan path to find pairs of connect points between which optical intent is installed |
| 263 | // opticalSrcPort works as a flag parameter to show scanning status |
| 264 | ConnectPoint opticalSrcPort = null; |
| 265 | for (Link link : path.links()) { |
| 266 | if (!isCrossConnectLink(link)) { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | if (opticalSrcPort != null) { |
| 271 | // opticalSrcPort!=null means src port was already found |
| 272 | // in this case link.src() is optical layer, and link.dst() is packet layer |
| 273 | |
| 274 | // Check if types of src port and dst port matches |
| 275 | Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()), |
| 276 | "Unknown device ID"); |
| 277 | Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()), |
| 278 | "Unknown device ID"); |
| 279 | if (srcDevice.type() != dstDevice.type()) { |
| 280 | log.error("Unsupported mix of cross connect points : {}, {}", |
| 281 | srcDevice.type(), dstDevice.type()); |
| 282 | return null; |
| 283 | } |
| 284 | |
| 285 | // Update cross connect points map |
| 286 | crossConnectPointMap.put(link.src(), link.dst()); |
| 287 | |
| 288 | // Add optical ports pair to list |
| 289 | crossConnectPoints.add(Pair.of(opticalSrcPort, link.src())); |
| 290 | |
| 291 | // Reset flag parameter |
| 292 | opticalSrcPort = null; |
| 293 | } else { |
| 294 | // opticalSrcPort==null means src port was not found yet |
| 295 | // in this case link.src() is packet layer, and link.dst() is optical layer |
| 296 | |
| 297 | // Update cross connect points map |
| 298 | crossConnectPointMap.put(link.dst(), link.src()); |
| 299 | // Set opticalSrcPort to src of link (optical port) |
| 300 | opticalSrcPort = link.dst(); |
| 301 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 304 | // create intents from cross connect points |
| 305 | List<Intent> intents = createIntents(crossConnectPoints); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 306 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 307 | // create set of PacketLinkRealizedByOptical |
| 308 | Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints, |
| 309 | intents, crossConnectPointMap); |
| 310 | |
| 311 | // create OpticalConnectivity object and store information to distributed store |
| 312 | OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 313 | |
| 314 | // store cross connect port usage |
| 315 | path.links().stream().filter(this::isCrossConnectLink) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 316 | .forEach(usedCrossConnectLinkSet::add); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 317 | |
| 318 | // Submit the intents |
| 319 | for (Intent i : intents) { |
| 320 | intentService.submit(i); |
| 321 | log.debug("Submitted an intent: {}", i); |
| 322 | } |
| 323 | |
| 324 | return connectivity.id(); |
| 325 | } |
| 326 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 327 | private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency, |
| 328 | Set<PacketLinkRealizedByOptical> links) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 329 | OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement()); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 330 | OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency, |
| 331 | links, Collections.emptySet()); |
| 332 | |
| 333 | links.forEach(l -> linkPathMap.put(l, connectivity)); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 334 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 335 | // store connectivity information |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 336 | connectivityMap.put(connectivity.id(), connectivity); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 337 | |
| 338 | return connectivity; |
| 339 | } |
| 340 | |
| 341 | @Override |
| 342 | public boolean removeConnectivity(OpticalConnectivityId id) { |
| 343 | log.info("removeConnectivity({})", id); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 344 | Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 345 | |
| 346 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 347 | log.info("OpticalConnectivity with id {} not found.", id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 348 | return false; |
| 349 | } |
| 350 | |
| 351 | // 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] | 352 | connectivity.value().getRealizingLinks().forEach(l -> { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 353 | Intent intent = intentService.getIntent(l.realizingIntentKey()); |
| 354 | intentService.withdraw(intent); |
| 355 | }); |
| 356 | |
| 357 | return true; |
| 358 | } |
| 359 | |
| 360 | @Override |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 361 | public Optional<List<Link>> getPath(OpticalConnectivityId id) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 362 | Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 363 | if (connectivity == null) { |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 364 | log.info("OpticalConnectivity with id {} not found.", id); |
| 365 | return Optional.empty(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 368 | return Optional.of(ImmutableList.copyOf(connectivity.value().links())); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * 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] | 373 | * During the process, save information about packet links to given set. |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 374 | * |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 375 | * @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] | 376 | * @return list of optical connectivity intents |
| 377 | */ |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 378 | private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 379 | List<Intent> intents = new LinkedList<>(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 380 | Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 381 | |
| 382 | while (itr.hasNext()) { |
| 383 | // checkArgument at start ensures we'll always have pairs of connect points |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 384 | Pair<ConnectPoint, ConnectPoint> next = itr.next(); |
| 385 | ConnectPoint src = next.getLeft(); |
| 386 | ConnectPoint dst = next.getRight(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 387 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 388 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 389 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 390 | |
| 391 | if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 392 | OduCltPort srcOCPort = (OduCltPort) srcPort; |
| 393 | OduCltPort dstOCPort = (OduCltPort) dstPort; |
| 394 | if (!srcOCPort.signalType().equals(dstOCPort.signalType())) { |
| 395 | continue; |
| 396 | } |
| 397 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 398 | // Create OTN circuit |
| 399 | OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder() |
| 400 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 401 | .src(src) |
| 402 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 403 | .signalType(srcOCPort.signalType()) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 404 | .bidirectional(true) |
| 405 | .build(); |
| 406 | intents.add(circuitIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 407 | } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 408 | OchPort srcOchPort = (OchPort) srcPort; |
| 409 | OchPort dstOchPort = (OchPort) dstPort; |
| 410 | if (!srcOchPort.signalType().equals(dstOchPort.signalType())) { |
| 411 | continue; |
| 412 | } |
| 413 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 414 | // Create lightpath |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 415 | OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder() |
| 416 | .appId(appId) |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 417 | .src(src) |
| 418 | .dst(dst) |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 419 | .signalType(srcOchPort.signalType()) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 420 | .bidirectional(true) |
| 421 | .build(); |
| 422 | intents.add(opticalIntent); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 423 | } else { |
| 424 | log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type()); |
| 425 | return Collections.emptyList(); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | return intents; |
| 430 | } |
| 431 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 432 | private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints, |
| 433 | List<Intent> intents, |
| 434 | Map<ConnectPoint, ConnectPoint> crossConnectPoints) { |
| 435 | checkArgument(connectPoints.size() == intents.size()); |
| 436 | |
| 437 | Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>(); |
| 438 | |
| 439 | Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator(); |
| 440 | Iterator<Intent> intentItr = intents.iterator(); |
| 441 | while (xcPointsItr.hasNext()) { |
| 442 | Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next(); |
| 443 | Intent intent = intentItr.next(); |
| 444 | |
| 445 | ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft())); |
| 446 | ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight())); |
| 447 | |
| 448 | if (intent instanceof OpticalConnectivityIntent) { |
| 449 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 450 | (OpticalConnectivityIntent) intent)); |
| 451 | } else if (intent instanceof OpticalCircuitIntent) { |
| 452 | pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst, |
| 453 | (OpticalCircuitIntent) intent)); |
| 454 | } else { |
| 455 | log.warn("Unexpected intent type: {}", intent.getClass()); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | return pLinks; |
| 460 | } |
| 461 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 462 | /** |
| 463 | * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device. |
| 464 | * |
| 465 | * @param type device type |
| 466 | * @return true if in packet layer, false otherwise |
| 467 | */ |
| 468 | private boolean isPacketLayer(Device.Type type) { |
| 469 | return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Verifies if given device type is in packet layer, i.e., switch or router device. |
| 474 | * |
| 475 | * @param type device type |
| 476 | * @return true if in packet layer, false otherwise |
| 477 | */ |
| 478 | private boolean isTransportLayer(Device.Type type) { |
| 479 | return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN; |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Verifies if given link forms a cross-connection between packet and optical layer. |
| 484 | * |
| 485 | * @param link the link |
| 486 | * @return true if the link is a cross-connect link, false otherwise |
| 487 | */ |
| 488 | private boolean isCrossConnectLink(Link link) { |
| 489 | if (link.type() != Link.Type.OPTICAL) { |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | Device.Type src = deviceService.getDevice(link.src().deviceId()).type(); |
| 494 | Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type(); |
| 495 | |
| 496 | return src != dst && |
| 497 | ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src))); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Updates bandwidth resource of given connect point. |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 502 | * |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 503 | * @param cp Connect point |
| 504 | * @param bandwidth New bandwidth |
| 505 | */ |
| 506 | private void updatePortBandwidth(ConnectPoint cp, Bandwidth bandwidth) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 507 | log.debug("update Port {} Bandwidth {}", cp, bandwidth); |
| 508 | BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class); |
| 509 | bwCapacity.capacity(bandwidth).apply(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Updates usage information of bandwidth based on connectivity which is established. |
| 514 | * @param connectivity Optical connectivity |
| 515 | */ |
| 516 | private void updateBandwidthUsage(OpticalConnectivity connectivity) { |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 517 | OpticalConnectivityId connectivityId = connectivity.id(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 518 | |
| 519 | List<Link> links = connectivity.links(); |
| 520 | |
| 521 | List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst())) |
| 522 | .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type())) |
| 523 | .map(cp -> Resources.continuous(cp.deviceId(), cp.port(), |
| 524 | Bandwidth.class).resource(connectivity.bandwidth().bps())) |
| 525 | .collect(Collectors.toList()); |
| 526 | |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 527 | log.debug("allocating bandwidth for {} : {}", connectivityId, resources); |
| 528 | List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 529 | if (allocations.isEmpty()) { |
| 530 | log.warn("Failed to allocate bandwidth {} to {}", |
| 531 | connectivity.bandwidth().bps(), resources); |
| 532 | // TODO any recovery? |
| 533 | } |
Naoki Shiota | cb744db | 2016-05-13 19:32:35 -0700 | [diff] [blame] | 534 | log.debug("Done allocating bandwidth for {}", connectivityId); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /** |
| 538 | * Release bandwidth allocated by given connectivity. |
| 539 | * @param connectivity Optical connectivity |
| 540 | */ |
| 541 | private void releaseBandwidthUsage(OpticalConnectivity connectivity) { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 542 | if (connectivity.links().isEmpty()) { |
| 543 | return; |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 544 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 545 | |
| 546 | // release resource only if this node is the master for link head device |
| 547 | if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) { |
| 548 | OpticalConnectivityId connectivityId = connectivity.id(); |
| 549 | |
| 550 | log.debug("releasing bandwidth allocated to {}", connectivityId); |
| 551 | if (!resourceService.release(connectivityId)) { |
| 552 | log.warn("Failed to release bandwidth allocated to {}", |
| 553 | connectivityId); |
| 554 | // TODO any recovery? |
| 555 | } |
| 556 | log.debug("DONE releasing bandwidth for {}", connectivityId); |
| 557 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | private class BandwidthLinkWeight implements LinkWeight { |
| 561 | private Bandwidth bandwidth = null; |
| 562 | |
| 563 | public BandwidthLinkWeight(Bandwidth bandwidth) { |
| 564 | this.bandwidth = bandwidth; |
| 565 | } |
| 566 | |
| 567 | @Override |
| 568 | public double weight(TopologyEdge edge) { |
| 569 | Link l = edge.link(); |
| 570 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 571 | // Avoid inactive links |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 572 | if (l.state() == Link.State.INACTIVE) { |
| 573 | return -1.0; |
| 574 | } |
| 575 | |
Naoki Shiota | 0d2943e | 2016-05-13 18:53:21 -0700 | [diff] [blame] | 576 | // Avoid cross connect links with used ports |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 577 | if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) { |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 578 | return -1.0; |
| 579 | } |
| 580 | |
| 581 | // Check availability of bandwidth |
| 582 | if (bandwidth != null) { |
| 583 | if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) { |
| 584 | return 1.0; |
| 585 | } else { |
| 586 | return -1.0; |
| 587 | } |
| 588 | } else { |
| 589 | // TODO needs to differentiate optical and packet? |
| 590 | if (l.type() == Link.Type.OPTICAL) { |
| 591 | // Transport links |
| 592 | return 1.0; |
| 593 | } else { |
| 594 | // Packet links |
| 595 | return 1.0; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | private boolean hasEnoughBandwidth(ConnectPoint cp) { |
| 601 | if (cp.elementId() instanceof DeviceId) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 602 | Device device = deviceService.getDevice(cp.deviceId()); |
| 603 | Device.Type type = device.type(); |
| 604 | |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 605 | if (isTransportLayer(type)) { |
Naoki Shiota | e1366ad | 2016-05-11 19:24:11 -0700 | [diff] [blame] | 606 | // Check if the port has enough capacity |
| 607 | Port port = deviceService.getPort(cp.deviceId(), cp.port()); |
| 608 | if (port instanceof OduCltPort || port instanceof OchPort) { |
| 609 | // Port with capacity |
| 610 | return bandwidth.bps() < port.portSpeed() * 1000000.0; |
| 611 | } else { |
| 612 | // Port without valid capacity (OMS port, etc.) |
| 613 | return true; |
| 614 | } |
| 615 | } else { |
| 616 | // Check if enough amount of bandwidth resource remains |
| 617 | ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class) |
| 618 | .resource(bandwidth.bps()); |
| 619 | return resourceService.isAvailable(resource); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 620 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 621 | } |
| 622 | return false; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | |
| 627 | public class InternalIntentListener implements IntentListener { |
| 628 | @Override |
| 629 | public void event(IntentEvent event) { |
| 630 | switch (event.type()) { |
| 631 | case INSTALLED: |
| 632 | log.info("Intent {} installed.", event.subject()); |
| 633 | updateCrossConnectLink(event.subject()); |
| 634 | break; |
| 635 | case WITHDRAWN: |
| 636 | log.info("Intent {} withdrawn.", event.subject()); |
| 637 | removeCrossConnectLinks(event.subject()); |
| 638 | break; |
| 639 | case FAILED: |
| 640 | log.info("Intent {} failed.", event.subject()); |
| 641 | break; |
| 642 | default: |
| 643 | break; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | private void updateCrossConnectLink(Intent intent) { |
| 648 | linkPathMap.entrySet().stream() |
| 649 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 650 | .forEach(e -> { |
| 651 | ConnectPoint packetSrc = e.getKey().src(); |
| 652 | ConnectPoint packetDst = e.getKey().dst(); |
| 653 | Bandwidth bw = e.getKey().bandwidth(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 654 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 655 | // reflect modification only if packetSrc is local_ |
| 656 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 657 | // Updates bandwidth of packet ports |
| 658 | updatePortBandwidth(packetSrc, bw); |
| 659 | updatePortBandwidth(packetDst, bw); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 660 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 661 | // Updates link status in distributed map |
| 662 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 663 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, true)); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 664 | } |
| 665 | }); |
| 666 | } |
| 667 | |
| 668 | private void removeCrossConnectLinks(Intent intent) { |
| 669 | ConnectPoint src, dst; |
| 670 | |
| 671 | if (intent instanceof OpticalCircuitIntent) { |
| 672 | OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent; |
| 673 | src = circuit.getSrc(); |
| 674 | dst = circuit.getDst(); |
| 675 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 676 | OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent; |
| 677 | src = conn.getSrc(); |
| 678 | dst = conn.getDst(); |
| 679 | } else { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | removeXcLinkUsage(src); |
| 684 | removeXcLinkUsage(dst); |
| 685 | |
| 686 | // Set bandwidth of 0 to cross connect ports |
| 687 | Bandwidth bw = Bandwidth.bps(0); |
| 688 | linkPathMap.entrySet().stream() |
| 689 | .filter(e -> e.getKey().realizingIntentKey().equals(intent.key())) |
| 690 | .forEach(e -> { |
| 691 | ConnectPoint packetSrc = e.getKey().src(); |
| 692 | ConnectPoint packetDst = e.getKey().dst(); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 693 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 694 | // reflect modification only if packetSrc is local_ |
| 695 | if (mastershipService.isLocalMaster(packetSrc.deviceId())) { |
| 696 | // Updates bandwidth of packet ports |
| 697 | updatePortBandwidth(packetSrc, bw); |
| 698 | updatePortBandwidth(packetDst, bw); |
| 699 | |
| 700 | // Updates link status in distributed map |
| 701 | linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) -> |
| 702 | e.getValue().value().setLinkEstablished(packetSrc, packetDst, false)); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 703 | } |
| 704 | }); |
| 705 | } |
| 706 | |
| 707 | private void removeXcLinkUsage(ConnectPoint cp) { |
| 708 | Optional<Link> link = linkService.getLinks(cp).stream() |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 709 | .filter(usedCrossConnectLinkSet::contains) |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 710 | .findAny(); |
| 711 | |
| 712 | if (!link.isPresent()) { |
| 713 | log.warn("Cross connect point {} has no cross connect link.", cp); |
| 714 | return; |
| 715 | } |
| 716 | |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 717 | usedCrossConnectLinkSet.remove(link.get()); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | |
| 722 | private class InternalLinkListener implements LinkListener { |
| 723 | |
| 724 | @Override |
| 725 | public void event(LinkEvent event) { |
| 726 | switch (event.type()) { |
| 727 | case LINK_REMOVED: |
| 728 | Link link = event.subject(); |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 729 | // updates linkPathMap only if src device of link is local |
| 730 | if (!mastershipService.isLocalMaster(link.src().deviceId())) { |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | // find all packet links that correspond to removed link |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 735 | Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream() |
| 736 | .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src())) |
| 737 | .collect(Collectors.toSet()); |
| 738 | |
| 739 | pLinks.forEach(l -> { |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 740 | // remove found packet links from distributed store |
| 741 | linkPathMap.computeIfPresent(l, (plink, conn) -> { |
| 742 | // Notifies listeners if all packet links are gone |
| 743 | if (conn.isAllRealizingLinkNotEstablished()) { |
| 744 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id())); |
| 745 | } |
| 746 | return null; |
| 747 | }); |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 748 | }); |
| 749 | default: |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | } |
Naoki Shiota | 7c3111b | 2016-06-09 16:12:11 -0700 | [diff] [blame] | 754 | |
| 755 | private class InternalStoreListener |
| 756 | implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> { |
| 757 | |
| 758 | @Override |
| 759 | public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) { |
| 760 | switch (event.type()) { |
| 761 | case UPDATE: |
| 762 | OpticalConnectivity oldConnectivity = event.oldValue().value(); |
| 763 | OpticalConnectivity newConnectivity = event.newValue().value(); |
| 764 | |
| 765 | if (!oldConnectivity.isAllRealizingLinkEstablished() && |
| 766 | newConnectivity.isAllRealizingLinkEstablished()) { |
| 767 | // Notifies listeners if all links are established |
| 768 | updateBandwidthUsage(newConnectivity); |
| 769 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id())); |
| 770 | } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() && |
| 771 | newConnectivity.isAllRealizingLinkNotEstablished()) { |
| 772 | // Notifies listeners if all links are gone |
| 773 | releaseBandwidthUsage(newConnectivity); |
| 774 | post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id())); |
| 775 | } |
| 776 | |
| 777 | break; |
| 778 | default: |
| 779 | break; |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | } |
Naoki Shiota | 5a05606 | 2016-05-05 18:43:59 -0700 | [diff] [blame] | 784 | } |
| 785 | |