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