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