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