blob: 8dcbeff62502216bb5080d6971d5f879b73981a3 [file] [log] [blame]
Naoki Shiota5a056062016-05-05 18:43:59 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Naoki Shiota5a056062016-05-05 18:43:59 -07003 *
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 */
16package org.onosproject.newoptical;
17
18import com.google.common.annotations.Beta;
19import com.google.common.collect.ImmutableList;
Naoki Shiota5a056062016-05-05 18:43:59 -070020import org.apache.commons.lang3.tuple.Pair;
21import org.apache.felix.scr.annotations.Activate;
22import org.apache.felix.scr.annotations.Component;
23import org.apache.felix.scr.annotations.Deactivate;
24import org.apache.felix.scr.annotations.Reference;
25import org.apache.felix.scr.annotations.ReferenceCardinality;
26import org.apache.felix.scr.annotations.Service;
27import org.onlab.util.Bandwidth;
Yuta HIGUCHIf167bf92017-03-08 13:19:04 -080028import org.onlab.util.GuavaCollectors;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070029import org.onlab.util.KryoNamespace;
Naoki Shiota5a056062016-05-05 18:43:59 -070030import org.onosproject.cluster.ClusterService;
Naoki Shiota5a056062016-05-05 18:43:59 -070031import org.onosproject.core.ApplicationId;
32import org.onosproject.core.CoreService;
Naoki Shiota0d2943e2016-05-13 18:53:21 -070033import org.onosproject.event.ListenerTracker;
34import org.onosproject.net.optical.OchPort;
35import org.onosproject.net.optical.OduCltPort;
Naoki Shiota5a056062016-05-05 18:43:59 -070036import org.onosproject.newoptical.api.OpticalConnectivityId;
37import org.onosproject.newoptical.api.OpticalPathEvent;
38import org.onosproject.newoptical.api.OpticalPathListener;
39import org.onosproject.newoptical.api.OpticalPathService;
40import org.onosproject.event.AbstractListenerManager;
41import org.onosproject.mastership.MastershipService;
Naoki Shiota5a056062016-05-05 18:43:59 -070042import org.onosproject.net.ConnectPoint;
43import org.onosproject.net.Device;
44import org.onosproject.net.DeviceId;
45import org.onosproject.net.Link;
Naoki Shiota5a056062016-05-05 18:43:59 -070046import org.onosproject.net.Path;
47import org.onosproject.net.Port;
48import org.onosproject.net.config.NetworkConfigService;
49import org.onosproject.net.device.DeviceService;
50import org.onosproject.net.intent.Intent;
51import org.onosproject.net.intent.IntentEvent;
Naoki Shiota5a056062016-05-05 18:43:59 -070052import org.onosproject.net.intent.IntentListener;
53import org.onosproject.net.intent.IntentService;
Yuta HIGUCHI8ac05092017-03-15 00:41:22 -070054import org.onosproject.net.intent.Key;
Naoki Shiota5a056062016-05-05 18:43:59 -070055import org.onosproject.net.intent.OpticalCircuitIntent;
56import org.onosproject.net.intent.OpticalConnectivityIntent;
Naoki Shiota5a056062016-05-05 18:43:59 -070057import org.onosproject.net.link.LinkEvent;
58import org.onosproject.net.link.LinkListener;
59import org.onosproject.net.link.LinkService;
Sho SHIMIZUb6c63a32016-05-26 12:07:19 -070060import org.onosproject.net.config.basics.BandwidthCapacity;
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -080061import org.onosproject.net.config.basics.BasicLinkConfig;
Naoki Shiota5a056062016-05-05 18:43:59 -070062import org.onosproject.net.resource.ContinuousResource;
63import org.onosproject.net.resource.Resource;
64import org.onosproject.net.resource.ResourceAllocation;
65import org.onosproject.net.resource.ResourceService;
66import org.onosproject.net.resource.Resources;
67import org.onosproject.net.topology.LinkWeight;
68import org.onosproject.net.topology.PathService;
69import org.onosproject.net.topology.TopologyEdge;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070070import org.onosproject.store.serializers.KryoNamespaces;
Naoki Shiota5a056062016-05-05 18:43:59 -070071import org.onosproject.store.service.AtomicCounter;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070072import org.onosproject.store.service.ConsistentMap;
73import org.onosproject.store.service.DistributedSet;
74import org.onosproject.store.service.MapEvent;
75import org.onosproject.store.service.MapEventListener;
76import org.onosproject.store.service.Serializer;
Naoki Shiota5a056062016-05-05 18:43:59 -070077import org.onosproject.store.service.StorageService;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070078import org.onosproject.store.service.Versioned;
Naoki Shiota5a056062016-05-05 18:43:59 -070079import org.slf4j.Logger;
80import org.slf4j.LoggerFactory;
81
82import java.time.Duration;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070083import java.util.ArrayList;
Yuta HIGUCHIf167bf92017-03-08 13:19:04 -080084import java.util.Collection;
Naoki Shiota5a056062016-05-05 18:43:59 -070085import java.util.Collections;
Naoki Shiota7c3111b2016-06-09 16:12:11 -070086import java.util.HashMap;
87import java.util.HashSet;
Naoki Shiota5a056062016-05-05 18:43:59 -070088import java.util.Iterator;
89import java.util.LinkedList;
90import java.util.List;
91import java.util.Map;
Yuta HIGUCHI8ac05092017-03-15 00:41:22 -070092import java.util.Map.Entry;
Naoki Shiota5a056062016-05-05 18:43:59 -070093import java.util.Optional;
94import java.util.Set;
Naoki Shiota5a056062016-05-05 18:43:59 -070095import java.util.stream.Collectors;
96import java.util.stream.Stream;
97
98import static com.google.common.base.Preconditions.checkArgument;
99import static com.google.common.base.Preconditions.checkNotNull;
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800100import static org.onosproject.net.LinkKey.linkKey;
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700101import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Naoki Shiota5a056062016-05-05 18:43:59 -0700102
103/**
104 * Main component to configure optical connectivity.
105 */
106@Beta
107@Service
108@Component(immediate = true)
109public class OpticalPathProvisioner
110 extends AbstractListenerManager<OpticalPathEvent, OpticalPathListener>
111 implements OpticalPathService {
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800112
Naoki Shiota5a056062016-05-05 18:43:59 -0700113 protected static final Logger log = LoggerFactory.getLogger(OpticalPathProvisioner.class);
114
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800115 /**
116 * Bandwidth representing no bandwidth requirement specified.
117 */
118 private static final Bandwidth NO_BW_REQUIREMENT = Bandwidth.bps(0);
119
Naoki Shiota5a056062016-05-05 18:43:59 -0700120 private static final String OPTICAL_CONNECTIVITY_ID_COUNTER = "optical-connectivity-id";
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700121 private static final String LINKPATH_MAP_NAME = "newoptical-linkpath";
122 private static final String CONNECTIVITY_MAP_NAME = "newoptical-connectivity";
123 private static final String CROSSCONNECTLINK_SET_NAME = "newoptical-crossconnectlink";
Naoki Shiota5a056062016-05-05 18:43:59 -0700124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
126 protected IntentService intentService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
129 protected PathService pathService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
132 protected CoreService coreService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
135 protected LinkService linkService;
136
137 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
138 protected MastershipService mastershipService;
139
140 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
141 protected ClusterService clusterService;
142
143 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
144 protected DeviceService deviceService;
145
146 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
147 protected StorageService storageService;
148
149 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
150 protected NetworkConfigService networkConfigService;
151
152 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
153 protected ResourceService resourceService;
154
155
156 private ApplicationId appId;
157
158 private AtomicCounter idCounter;
159
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700160 private ListenerTracker listeners;
Naoki Shiota5a056062016-05-05 18:43:59 -0700161
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700162 private InternalStoreListener storeListener = new InternalStoreListener();
Naoki Shiota5a056062016-05-05 18:43:59 -0700163
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800164 /**
165 * Map from packet-layer link expected to be realized by some optical Intent to
166 * OpticalConnectivity (~=top level intent over multi-layer topology).
167 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700168 private ConsistentMap<PacketLinkRealizedByOptical, OpticalConnectivity> linkPathMap;
Naoki Shiota5a056062016-05-05 18:43:59 -0700169
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700170 private ConsistentMap<OpticalConnectivityId, OpticalConnectivity> connectivityMap;
171
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800172 // FIXME in the long run. This is effectively app's own resource subsystem
173 /**
174 * Set of cross connect link currently used.
175 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700176 private DistributedSet<Link> usedCrossConnectLinkSet;
177
178 private static final KryoNamespace.Builder LINKPATH_SERIALIZER = KryoNamespace.newBuilder()
179 .register(KryoNamespaces.API)
180 .register(PacketLinkRealizedByOptical.class)
181 .register(OpticalConnectivityId.class)
182 .register(OpticalConnectivity.class);
183
184 private static final KryoNamespace.Builder CONNECTIVITY_SERIALIZER = KryoNamespace.newBuilder()
185 .register(KryoNamespaces.API)
Yuta HIGUCHI0086cf82016-07-18 22:49:45 -0700186 .register(PacketLinkRealizedByOptical.class)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700187 .register(OpticalConnectivityId.class)
188 .register(OpticalConnectivity.class);
189
190 private static final KryoNamespace.Builder CROSSCONNECTLINKS_SERIALIZER = KryoNamespace.newBuilder()
191 .register(KryoNamespaces.API);
Naoki Shiota5a056062016-05-05 18:43:59 -0700192
193 @Activate
194 protected void activate() {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700195 deviceService = opticalView(deviceService);
Naoki Shiota5a056062016-05-05 18:43:59 -0700196 appId = coreService.registerApplication("org.onosproject.newoptical");
197
Naoki Shiota03c29e12016-05-16 16:58:07 -0700198 idCounter = storageService.getAtomicCounter(OPTICAL_CONNECTIVITY_ID_COUNTER);
Naoki Shiota5a056062016-05-05 18:43:59 -0700199
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700200 linkPathMap = storageService.<PacketLinkRealizedByOptical, OpticalConnectivity>consistentMapBuilder()
201 .withSerializer(Serializer.using(LINKPATH_SERIALIZER.build()))
202 .withName(LINKPATH_MAP_NAME)
203 .withApplicationId(appId)
204 .build();
205
206 connectivityMap = storageService.<OpticalConnectivityId, OpticalConnectivity>consistentMapBuilder()
207 .withSerializer(Serializer.using(CONNECTIVITY_SERIALIZER.build()))
208 .withName(CONNECTIVITY_MAP_NAME)
209 .withApplicationId(appId)
210 .build();
211
212 usedCrossConnectLinkSet = storageService.<Link>setBuilder()
213 .withSerializer(Serializer.using(CROSSCONNECTLINKS_SERIALIZER.build()))
214 .withName(CROSSCONNECTLINK_SET_NAME)
215 .withApplicationId(appId)
216 .build()
217 .asDistributedSet();
218
Naoki Shiota5a056062016-05-05 18:43:59 -0700219 eventDispatcher.addSink(OpticalPathEvent.class, listenerRegistry);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700220
221 listeners = new ListenerTracker();
222 listeners.addListener(linkService, new InternalLinkListener())
223 .addListener(intentService, new InternalIntentListener());
Naoki Shiota5a056062016-05-05 18:43:59 -0700224
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700225 linkPathMap.addListener(storeListener);
226
Naoki Shiota5a056062016-05-05 18:43:59 -0700227 log.info("Started");
228 }
229
230 @Deactivate
231 protected void deactivate() {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700232 linkPathMap.removeListener(storeListener);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700233 listeners.removeListeners();
234 eventDispatcher.removeSink(OpticalPathEvent.class);
Naoki Shiota5a056062016-05-05 18:43:59 -0700235
236 log.info("Stopped");
237 }
238
Yuta HIGUCHIf167bf92017-03-08 13:19:04 -0800239 @Override
240 public Collection<OpticalConnectivity> listConnectivity() {
241 return connectivityMap.values().stream()
242 .map(Versioned::value)
243 .collect(GuavaCollectors.toImmutableList());
244 }
Yuta HIGUCHI8ac05092017-03-15 00:41:22 -0700245
246 public Set<Key> listIntents(OpticalConnectivityId id) {
247 return linkPathMap.entrySet().stream()
248 .filter(ent -> id.equals(ent.getValue().value().id()))
249 .map(Entry::getKey)
250 .map(PacketLinkRealizedByOptical::realizingIntentKey)
251 .collect(Collectors.toSet());
252 }
253
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800254 /*
255 * Request packet-layer connectivity between specified ports,
256 * over packet-optical multi-layer infrastructure.
257 *
258 * Functionality-wise this is effectively submitting Packet-Optical
259 * multi-layer P2P Intent.
260 *
261 * It computes multi-layer path meeting specified constraint,
262 * and calls setupPath.
263 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700264 @Override
265 public OpticalConnectivityId setupConnectivity(ConnectPoint ingress, ConnectPoint egress,
266 Bandwidth bandwidth, Duration latency) {
267 checkNotNull(ingress);
268 checkNotNull(egress);
269 log.info("setupConnectivity({}, {}, {}, {})", ingress, egress, bandwidth, latency);
270
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800271 bandwidth = (bandwidth == null) ? NO_BW_REQUIREMENT : bandwidth;
Naoki Shiota5a056062016-05-05 18:43:59 -0700272
273 Set<Path> paths = pathService.getPaths(ingress.deviceId(), egress.deviceId(),
274 new BandwidthLinkWeight(bandwidth));
275 if (paths.isEmpty()) {
276 log.warn("Unable to find multi-layer path.");
277 return null;
278 }
279
280 // Search path with available cross connect points
281 for (Path path : paths) {
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700282 // Path service calculates from node to node, we're only interested in port to port
283 if (!path.src().equals(ingress) || !path.dst().equals(egress)) {
284 continue;
285 }
286
Naoki Shiota5a056062016-05-05 18:43:59 -0700287 OpticalConnectivityId id = setupPath(path, bandwidth, latency);
288 if (id != null) {
289 log.info("Assigned OpticalConnectivityId: {}", id);
290 return id;
291 }
292 }
293
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800294 log.error("setupConnectivity({}, {}, {}, {}) failed.", ingress, egress, bandwidth, latency);
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700295
Naoki Shiota5a056062016-05-05 18:43:59 -0700296 return null;
297 }
298
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800299 /*
300 * Given a multi-layer path,
301 * compute a set of segments which requires
302 * OpticalConnectivity(~=OpticalConnectivityIntent or OpticalCircuitPath)
303 * to provide packet-layer connectivity.
304 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700305 @Override
306 public OpticalConnectivityId setupPath(Path path, Bandwidth bandwidth, Duration latency) {
307 checkNotNull(path);
Yuta HIGUCHI8fc32f82017-03-14 22:12:42 -0700308 log.debug("setupPath({}, {}, {})", path, bandwidth, latency);
Naoki Shiota5a056062016-05-05 18:43:59 -0700309
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700310 // map of cross connect points (optical port -> packet port)
311 Map<ConnectPoint, ConnectPoint> crossConnectPointMap = new HashMap<>();
312
313 // list of (src, dst) pair of optical ports between which optical path should be installed
314 List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints = new ArrayList<>();
315
316 // Scan path to find pairs of connect points between which optical intent is installed
317 // opticalSrcPort works as a flag parameter to show scanning status
318 ConnectPoint opticalSrcPort = null;
319 for (Link link : path.links()) {
320 if (!isCrossConnectLink(link)) {
321 continue;
322 }
323
324 if (opticalSrcPort != null) {
325 // opticalSrcPort!=null means src port was already found
326 // in this case link.src() is optical layer, and link.dst() is packet layer
327
328 // Check if types of src port and dst port matches
329 Device srcDevice = checkNotNull(deviceService.getDevice(opticalSrcPort.deviceId()),
330 "Unknown device ID");
331 Device dstDevice = checkNotNull(deviceService.getDevice(link.src().deviceId()),
332 "Unknown device ID");
333 if (srcDevice.type() != dstDevice.type()) {
334 log.error("Unsupported mix of cross connect points : {}, {}",
335 srcDevice.type(), dstDevice.type());
336 return null;
337 }
338
339 // Update cross connect points map
340 crossConnectPointMap.put(link.src(), link.dst());
341
342 // Add optical ports pair to list
343 crossConnectPoints.add(Pair.of(opticalSrcPort, link.src()));
344
345 // Reset flag parameter
346 opticalSrcPort = null;
347 } else {
348 // opticalSrcPort==null means src port was not found yet
349 // in this case link.src() is packet layer, and link.dst() is optical layer
350
351 // Update cross connect points map
352 crossConnectPointMap.put(link.dst(), link.src());
353 // Set opticalSrcPort to src of link (optical port)
354 opticalSrcPort = link.dst();
355 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700356 }
357
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700358 // create intents from cross connect points
359 List<Intent> intents = createIntents(crossConnectPoints);
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800360 if (intents.isEmpty()) {
361 log.error("No intents produced from {}", crossConnectPoints);
362 return null;
363 }
364
Naoki Shiota5a056062016-05-05 18:43:59 -0700365
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700366 // create set of PacketLinkRealizedByOptical
367 Set<PacketLinkRealizedByOptical> packetLinks = createPacketLinkSet(crossConnectPoints,
368 intents, crossConnectPointMap);
369
370 // create OpticalConnectivity object and store information to distributed store
371 OpticalConnectivity connectivity = createConnectivity(path, bandwidth, latency, packetLinks);
Naoki Shiota5a056062016-05-05 18:43:59 -0700372
373 // store cross connect port usage
374 path.links().stream().filter(this::isCrossConnectLink)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700375 .forEach(usedCrossConnectLinkSet::add);
Naoki Shiota5a056062016-05-05 18:43:59 -0700376
377 // Submit the intents
378 for (Intent i : intents) {
379 intentService.submit(i);
380 log.debug("Submitted an intent: {}", i);
381 }
382
383 return connectivity.id();
384 }
385
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700386 private OpticalConnectivity createConnectivity(Path path, Bandwidth bandwidth, Duration latency,
387 Set<PacketLinkRealizedByOptical> links) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700388 OpticalConnectivityId id = OpticalConnectivityId.of(idCounter.getAndIncrement());
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700389 OpticalConnectivity connectivity = new OpticalConnectivity(id, path.links(), bandwidth, latency,
390 links, Collections.emptySet());
391
392 links.forEach(l -> linkPathMap.put(l, connectivity));
Naoki Shiota5a056062016-05-05 18:43:59 -0700393
Naoki Shiota5a056062016-05-05 18:43:59 -0700394 // store connectivity information
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700395 connectivityMap.put(connectivity.id(), connectivity);
Naoki Shiota5a056062016-05-05 18:43:59 -0700396
397 return connectivity;
398 }
399
400 @Override
401 public boolean removeConnectivity(OpticalConnectivityId id) {
402 log.info("removeConnectivity({})", id);
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700403 Versioned<OpticalConnectivity> connectivity = connectivityMap.remove(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700404
405 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700406 log.info("OpticalConnectivity with id {} not found.", id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700407 return false;
408 }
409
410 // TODO withdraw intent only if all of connectivities that use the optical path are withdrawn
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700411 connectivity.value().getRealizingLinks().forEach(l -> {
Naoki Shiota5a056062016-05-05 18:43:59 -0700412 Intent intent = intentService.getIntent(l.realizingIntentKey());
413 intentService.withdraw(intent);
414 });
415
416 return true;
417 }
418
419 @Override
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700420 public Optional<List<Link>> getPath(OpticalConnectivityId id) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700421 Versioned<OpticalConnectivity> connectivity = connectivityMap.get(id);
Naoki Shiota5a056062016-05-05 18:43:59 -0700422 if (connectivity == null) {
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700423 log.info("OpticalConnectivity with id {} not found.", id);
424 return Optional.empty();
Naoki Shiota5a056062016-05-05 18:43:59 -0700425 }
426
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700427 return Optional.of(ImmutableList.copyOf(connectivity.value().links()));
Naoki Shiota5a056062016-05-05 18:43:59 -0700428 }
429
430 /**
431 * Scans the list of cross connection points and returns a list of optical connectivity intents.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700432 * During the process, save information about packet links to given set.
Naoki Shiota5a056062016-05-05 18:43:59 -0700433 *
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700434 * @param crossConnectPoints list of (src, dst) pair between which optical path will be set up
Naoki Shiota5a056062016-05-05 18:43:59 -0700435 * @return list of optical connectivity intents
436 */
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700437 private List<Intent> createIntents(List<Pair<ConnectPoint, ConnectPoint>> crossConnectPoints) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700438 List<Intent> intents = new LinkedList<>();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700439 Iterator<Pair<ConnectPoint, ConnectPoint>> itr = crossConnectPoints.iterator();
Naoki Shiota5a056062016-05-05 18:43:59 -0700440
441 while (itr.hasNext()) {
442 // checkArgument at start ensures we'll always have pairs of connect points
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700443 Pair<ConnectPoint, ConnectPoint> next = itr.next();
444 ConnectPoint src = next.getLeft();
445 ConnectPoint dst = next.getRight();
Naoki Shiota5a056062016-05-05 18:43:59 -0700446
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700447 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
448 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
Naoki Shiota5a056062016-05-05 18:43:59 -0700449
450 if (srcPort instanceof OduCltPort && dstPort instanceof OduCltPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700451 OduCltPort srcOCPort = (OduCltPort) srcPort;
452 OduCltPort dstOCPort = (OduCltPort) dstPort;
453 if (!srcOCPort.signalType().equals(dstOCPort.signalType())) {
454 continue;
455 }
456
Naoki Shiota5a056062016-05-05 18:43:59 -0700457 // Create OTN circuit
458 OpticalCircuitIntent circuitIntent = OpticalCircuitIntent.builder()
459 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700460 .src(src)
461 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700462 .signalType(srcOCPort.signalType())
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700463 .bidirectional(false)
Naoki Shiota5a056062016-05-05 18:43:59 -0700464 .build();
465 intents.add(circuitIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700466 } else if (srcPort instanceof OchPort && dstPort instanceof OchPort) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700467 OchPort srcOchPort = (OchPort) srcPort;
468 OchPort dstOchPort = (OchPort) dstPort;
469 if (!srcOchPort.signalType().equals(dstOchPort.signalType())) {
470 continue;
471 }
472
Naoki Shiota5a056062016-05-05 18:43:59 -0700473 // Create lightpath
Naoki Shiota5a056062016-05-05 18:43:59 -0700474 OpticalConnectivityIntent opticalIntent = OpticalConnectivityIntent.builder()
475 .appId(appId)
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700476 .src(src)
477 .dst(dst)
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700478 .signalType(srcOchPort.signalType())
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700479 .bidirectional(false)
Naoki Shiota5a056062016-05-05 18:43:59 -0700480 .build();
481 intents.add(opticalIntent);
Naoki Shiota5a056062016-05-05 18:43:59 -0700482 } else {
483 log.warn("Unsupported cross connect point types {} {}", srcPort.type(), dstPort.type());
484 return Collections.emptyList();
485 }
486 }
487
488 return intents;
489 }
490
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700491 private Set<PacketLinkRealizedByOptical> createPacketLinkSet(List<Pair<ConnectPoint, ConnectPoint>> connectPoints,
492 List<Intent> intents,
493 Map<ConnectPoint, ConnectPoint> crossConnectPoints) {
494 checkArgument(connectPoints.size() == intents.size());
495
496 Set<PacketLinkRealizedByOptical> pLinks = new HashSet<>();
497
498 Iterator<Pair<ConnectPoint, ConnectPoint>> xcPointsItr = connectPoints.iterator();
499 Iterator<Intent> intentItr = intents.iterator();
500 while (xcPointsItr.hasNext()) {
501 Pair<ConnectPoint, ConnectPoint> xcPoints = xcPointsItr.next();
502 Intent intent = intentItr.next();
503
504 ConnectPoint packetSrc = checkNotNull(crossConnectPoints.get(xcPoints.getLeft()));
505 ConnectPoint packetDst = checkNotNull(crossConnectPoints.get(xcPoints.getRight()));
506
507 if (intent instanceof OpticalConnectivityIntent) {
508 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
509 (OpticalConnectivityIntent) intent));
510 } else if (intent instanceof OpticalCircuitIntent) {
511 pLinks.add(PacketLinkRealizedByOptical.create(packetSrc, packetDst,
512 (OpticalCircuitIntent) intent));
513 } else {
514 log.warn("Unexpected intent type: {}", intent.getClass());
515 }
516 }
517
518 return pLinks;
519 }
520
Naoki Shiota5a056062016-05-05 18:43:59 -0700521 /**
522 * Verifies if given device type is in packet layer, i.e., ROADM, OTN or ROADM_OTN device.
523 *
524 * @param type device type
525 * @return true if in packet layer, false otherwise
526 */
527 private boolean isPacketLayer(Device.Type type) {
528 return type == Device.Type.SWITCH || type == Device.Type.ROUTER || type == Device.Type.VIRTUAL;
529 }
530
531 /**
Yuta HIGUCHId2f041e2017-03-10 12:45:15 -0800532 * Verifies if given device type is NOT in packet layer, i.e., switch or router device.
Naoki Shiota5a056062016-05-05 18:43:59 -0700533 *
534 * @param type device type
535 * @return true if in packet layer, false otherwise
536 */
537 private boolean isTransportLayer(Device.Type type) {
538 return type == Device.Type.ROADM || type == Device.Type.OTN || type == Device.Type.ROADM_OTN;
539 }
540
541 /**
542 * Verifies if given link forms a cross-connection between packet and optical layer.
543 *
544 * @param link the link
545 * @return true if the link is a cross-connect link, false otherwise
546 */
547 private boolean isCrossConnectLink(Link link) {
548 if (link.type() != Link.Type.OPTICAL) {
549 return false;
550 }
551
552 Device.Type src = deviceService.getDevice(link.src().deviceId()).type();
553 Device.Type dst = deviceService.getDevice(link.dst().deviceId()).type();
554
555 return src != dst &&
556 ((isPacketLayer(src) && isTransportLayer(dst)) || (isPacketLayer(dst) && isTransportLayer(src)));
557 }
558
559 /**
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800560 * Updates bandwidth resource of given connect point to specified value.
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700561 *
Naoki Shiota5a056062016-05-05 18:43:59 -0700562 * @param cp Connect point
563 * @param bandwidth New bandwidth
564 */
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800565 private void setPortBandwidth(ConnectPoint cp, Bandwidth bandwidth) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700566 log.debug("update Port {} Bandwidth {}", cp, bandwidth);
567 BandwidthCapacity bwCapacity = networkConfigService.addConfig(cp, BandwidthCapacity.class);
568 bwCapacity.capacity(bandwidth).apply();
Naoki Shiota5a056062016-05-05 18:43:59 -0700569 }
570
571 /**
572 * Updates usage information of bandwidth based on connectivity which is established.
573 * @param connectivity Optical connectivity
574 */
575 private void updateBandwidthUsage(OpticalConnectivity connectivity) {
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800576 if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) {
577 // no bandwidth requirement, nothing to allocate.
578 return;
579 }
580
Naoki Shiotacb744db2016-05-13 19:32:35 -0700581 OpticalConnectivityId connectivityId = connectivity.id();
Naoki Shiota5a056062016-05-05 18:43:59 -0700582
583 List<Link> links = connectivity.links();
584
585 List<Resource> resources = links.stream().flatMap(l -> Stream.of(l.src(), l.dst()))
586 .filter(cp -> !isTransportLayer(deviceService.getDevice(cp.deviceId()).type()))
587 .map(cp -> Resources.continuous(cp.deviceId(), cp.port(),
588 Bandwidth.class).resource(connectivity.bandwidth().bps()))
589 .collect(Collectors.toList());
590
Naoki Shiotacb744db2016-05-13 19:32:35 -0700591 log.debug("allocating bandwidth for {} : {}", connectivityId, resources);
592 List<ResourceAllocation> allocations = resourceService.allocate(connectivityId, resources);
Naoki Shiota5a056062016-05-05 18:43:59 -0700593 if (allocations.isEmpty()) {
594 log.warn("Failed to allocate bandwidth {} to {}",
595 connectivity.bandwidth().bps(), resources);
596 // TODO any recovery?
597 }
Naoki Shiotacb744db2016-05-13 19:32:35 -0700598 log.debug("Done allocating bandwidth for {}", connectivityId);
Naoki Shiota5a056062016-05-05 18:43:59 -0700599 }
600
601 /**
602 * Release bandwidth allocated by given connectivity.
603 * @param connectivity Optical connectivity
604 */
605 private void releaseBandwidthUsage(OpticalConnectivity connectivity) {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700606 if (connectivity.links().isEmpty()) {
607 return;
Naoki Shiota5a056062016-05-05 18:43:59 -0700608 }
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800609 if (NO_BW_REQUIREMENT.equals(connectivity.bandwidth())) {
610 // no bandwidth requirement, nothing to release.
611 return;
612 }
613
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700614
615 // release resource only if this node is the master for link head device
616 if (mastershipService.isLocalMaster(connectivity.links().get(0).src().deviceId())) {
617 OpticalConnectivityId connectivityId = connectivity.id();
618
619 log.debug("releasing bandwidth allocated to {}", connectivityId);
620 if (!resourceService.release(connectivityId)) {
621 log.warn("Failed to release bandwidth allocated to {}",
622 connectivityId);
623 // TODO any recovery?
624 }
625 log.debug("DONE releasing bandwidth for {}", connectivityId);
626 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700627 }
628
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800629 private boolean linkDiscoveryEnabled(ConnectPoint cp) {
630 // FIXME should check Device feature and configuration state.
631
632 // short-term hack for ONS'17 time-frame,
633 // only expect OF device to have link discovery.
Jon Halla3fcf672017-03-28 16:53:22 -0700634 return "of".equals(cp.deviceId().uri().getScheme());
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800635 }
636
637 /**
638 * Returns true if both connect point support for link discovery & enabled.
639 *
640 * @param cp1 port 1
641 * @param cp2 port 2
642 * @return true if both connect point support for link discovery & enabled.
643 */
644 private boolean linkDiscoveryEnabled(ConnectPoint cp1, ConnectPoint cp2) {
645 return linkDiscoveryEnabled(cp1) && linkDiscoveryEnabled(cp2);
646 }
647
Naoki Shiota5a056062016-05-05 18:43:59 -0700648 private class BandwidthLinkWeight implements LinkWeight {
649 private Bandwidth bandwidth = null;
650
651 public BandwidthLinkWeight(Bandwidth bandwidth) {
652 this.bandwidth = bandwidth;
653 }
654
655 @Override
656 public double weight(TopologyEdge edge) {
657 Link l = edge.link();
658
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700659 // Avoid inactive links
Naoki Shiota5a056062016-05-05 18:43:59 -0700660 if (l.state() == Link.State.INACTIVE) {
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800661 log.trace("{} is not active", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700662 return -1.0;
663 }
664
Naoki Shiota0d2943e2016-05-13 18:53:21 -0700665 // Avoid cross connect links with used ports
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700666 if (isCrossConnectLink(l) && usedCrossConnectLinkSet.contains(l)) {
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800667 log.trace("Cross connect {} in use", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700668 return -1.0;
669 }
670
671 // Check availability of bandwidth
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800672 if (bandwidth != null && !NO_BW_REQUIREMENT.equals(bandwidth)) {
Naoki Shiota5a056062016-05-05 18:43:59 -0700673 if (hasEnoughBandwidth(l.src()) && hasEnoughBandwidth(l.dst())) {
674 return 1.0;
675 } else {
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700676 log.trace("Not enough bandwidth on {}", l);
Naoki Shiota5a056062016-05-05 18:43:59 -0700677 return -1.0;
678 }
679 } else {
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700680 // Use everything except our own indirect links
681 if (l.type() == Link.Type.INDIRECT) {
682 return -1.0;
Naoki Shiota5a056062016-05-05 18:43:59 -0700683 } else {
Naoki Shiota5a056062016-05-05 18:43:59 -0700684 return 1.0;
685 }
686 }
687 }
688
689 private boolean hasEnoughBandwidth(ConnectPoint cp) {
690 if (cp.elementId() instanceof DeviceId) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700691 Device device = deviceService.getDevice(cp.deviceId());
692 Device.Type type = device.type();
693
Naoki Shiota5a056062016-05-05 18:43:59 -0700694 if (isTransportLayer(type)) {
Naoki Shiotae1366ad2016-05-11 19:24:11 -0700695 // Check if the port has enough capacity
696 Port port = deviceService.getPort(cp.deviceId(), cp.port());
697 if (port instanceof OduCltPort || port instanceof OchPort) {
698 // Port with capacity
699 return bandwidth.bps() < port.portSpeed() * 1000000.0;
700 } else {
701 // Port without valid capacity (OMS port, etc.)
702 return true;
703 }
704 } else {
705 // Check if enough amount of bandwidth resource remains
706 ContinuousResource resource = Resources.continuous(cp.deviceId(), cp.port(), Bandwidth.class)
707 .resource(bandwidth.bps());
Yuta HIGUCHI806113f2017-03-07 22:46:20 -0800708 try {
709 return resourceService.isAvailable(resource);
710 } catch (Exception e) {
711 log.error("Resource service failed checking availability of {}",
712 resource, e);
713 throw e;
714 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700715 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700716 }
717 return false;
718 }
719 }
720
721
722 public class InternalIntentListener implements IntentListener {
723 @Override
724 public void event(IntentEvent event) {
725 switch (event.type()) {
726 case INSTALLED:
727 log.info("Intent {} installed.", event.subject());
728 updateCrossConnectLink(event.subject());
729 break;
730 case WITHDRAWN:
731 log.info("Intent {} withdrawn.", event.subject());
732 removeCrossConnectLinks(event.subject());
733 break;
734 case FAILED:
735 log.info("Intent {} failed.", event.subject());
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800736 // TODO If it was one of it's own optical Intent,
737 // update link state
738 // TODO If it was packet P2P Intent, call setupConnectivity
Naoki Shiota5a056062016-05-05 18:43:59 -0700739 break;
740 default:
741 break;
742 }
743 }
744
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800745 // TODO rename "CrossConnectLink"?
746 /**
747 * Update packet-layer link/port state once Intent is installed.
748 *
749 * @param intent which reached installed state
750 */
Naoki Shiota5a056062016-05-05 18:43:59 -0700751 private void updateCrossConnectLink(Intent intent) {
752 linkPathMap.entrySet().stream()
753 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
754 .forEach(e -> {
755 ConnectPoint packetSrc = e.getKey().src();
756 ConnectPoint packetDst = e.getKey().dst();
757 Bandwidth bw = e.getKey().bandwidth();
Naoki Shiota5a056062016-05-05 18:43:59 -0700758
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700759 // reflect modification only if packetSrc is local_
760 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
761 // Updates bandwidth of packet ports
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800762 setPortBandwidth(packetSrc, bw);
763 setPortBandwidth(packetDst, bw);
Naoki Shiota5a056062016-05-05 18:43:59 -0700764
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700765 // Updates link status in distributed map
766 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
767 e.getValue().value().setLinkEstablished(packetSrc, packetDst, true));
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800768
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800769
770 if (!linkDiscoveryEnabled(packetSrc, packetDst)) {
771 injectLink(packetSrc, packetDst);
772 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700773 }
774 });
775 }
776
777 private void removeCrossConnectLinks(Intent intent) {
778 ConnectPoint src, dst;
779
780 if (intent instanceof OpticalCircuitIntent) {
781 OpticalCircuitIntent circuit = (OpticalCircuitIntent) intent;
782 src = circuit.getSrc();
783 dst = circuit.getDst();
784 } else if (intent instanceof OpticalConnectivityIntent) {
785 OpticalConnectivityIntent conn = (OpticalConnectivityIntent) intent;
786 src = conn.getSrc();
787 dst = conn.getDst();
788 } else {
789 return;
790 }
791
792 removeXcLinkUsage(src);
793 removeXcLinkUsage(dst);
794
795 // Set bandwidth of 0 to cross connect ports
796 Bandwidth bw = Bandwidth.bps(0);
797 linkPathMap.entrySet().stream()
798 .filter(e -> e.getKey().realizingIntentKey().equals(intent.key()))
799 .forEach(e -> {
800 ConnectPoint packetSrc = e.getKey().src();
801 ConnectPoint packetDst = e.getKey().dst();
Naoki Shiota5a056062016-05-05 18:43:59 -0700802
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700803 // reflect modification only if packetSrc is local_
804 if (mastershipService.isLocalMaster(packetSrc.deviceId())) {
805 // Updates bandwidth of packet ports
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800806 setPortBandwidth(packetSrc, bw);
807 setPortBandwidth(packetDst, bw);
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700808
809 // Updates link status in distributed map
810 linkPathMap.computeIfPresent(e.getKey(), (link, connectivity) ->
811 e.getValue().value().setLinkEstablished(packetSrc, packetDst, false));
Yuta HIGUCHIe80ec2c2017-03-06 14:16:13 -0800812
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800813
814 if (!linkDiscoveryEnabled(packetSrc, packetDst)) {
815 removeInjectedLink(packetSrc, packetDst);
816 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700817 }
818 });
819 }
820
821 private void removeXcLinkUsage(ConnectPoint cp) {
822 Optional<Link> link = linkService.getLinks(cp).stream()
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700823 .filter(usedCrossConnectLinkSet::contains)
Naoki Shiota5a056062016-05-05 18:43:59 -0700824 .findAny();
825
826 if (!link.isPresent()) {
827 log.warn("Cross connect point {} has no cross connect link.", cp);
828 return;
829 }
830
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700831 usedCrossConnectLinkSet.remove(link.get());
Naoki Shiota5a056062016-05-05 18:43:59 -0700832 }
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800833
834 /**
835 * Injects link between specified packet port.
836 *
837 * @param packetSrc port 1
838 * @param packetDst port 2
839 */
840 private void injectLink(ConnectPoint packetSrc,
841 ConnectPoint packetDst) {
842 // inject expected link or durable link
843 // if packet device cannot advertise packet link
844 try {
845 BasicLinkConfig lnkCfg = networkConfigService
846 .addConfig(linkKey(packetSrc, packetDst),
847 BasicLinkConfig.class);
848 lnkCfg.isAllowed(true);
849 lnkCfg.isDurable(true);
Marc De Leenheer43b91ad2017-03-17 14:45:28 -0700850 lnkCfg.type(Link.Type.INDIRECT);
851 lnkCfg.isBidirectional(false);
Yuta HIGUCHI9b9c7292017-03-07 22:50:04 -0800852 lnkCfg.apply();
853 } catch (Exception ex) {
854 log.error("Applying BasicLinkConfig failed", ex);
855 }
856 }
857
858 /**
859 * Removes link injected between specified packet port.
860 *
861 * @param packetSrc port 1
862 * @param packetDst port 2
863 */
864 private void removeInjectedLink(ConnectPoint packetSrc,
865 ConnectPoint packetDst) {
866 // remove expected link or durable link
867 // if packet device cannot monitor packet link
868
869 try {
870 // hack to mark link off-line
871 BasicLinkConfig lnkCfg = networkConfigService
872 .getConfig(linkKey(packetSrc, packetDst),
873 BasicLinkConfig.class);
874 lnkCfg.isAllowed(false);
875 lnkCfg.apply();
876 } catch (Exception ex) {
877 log.error("Applying BasicLinkConfig failed", ex);
878 }
879
880 networkConfigService
881 .removeConfig(linkKey(packetSrc, packetDst),
882 BasicLinkConfig.class);
883 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700884 }
885
886
887 private class InternalLinkListener implements LinkListener {
888
889 @Override
890 public void event(LinkEvent event) {
891 switch (event.type()) {
892 case LINK_REMOVED:
893 Link link = event.subject();
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700894 // updates linkPathMap only if src device of link is local
895 if (!mastershipService.isLocalMaster(link.src().deviceId())) {
896 return;
897 }
898
899 // find all packet links that correspond to removed link
Naoki Shiota5a056062016-05-05 18:43:59 -0700900 Set<PacketLinkRealizedByOptical> pLinks = linkPathMap.keySet().stream()
901 .filter(l -> l.isBetween(link.src(), link.dst()) || l.isBetween(link.dst(), link.src()))
902 .collect(Collectors.toSet());
903
904 pLinks.forEach(l -> {
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700905 // remove found packet links from distributed store
906 linkPathMap.computeIfPresent(l, (plink, conn) -> {
907 // Notifies listeners if all packet links are gone
908 if (conn.isAllRealizingLinkNotEstablished()) {
909 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, conn.id()));
910 }
911 return null;
912 });
Naoki Shiota5a056062016-05-05 18:43:59 -0700913 });
914 default:
915 break;
916 }
917 }
918 }
Naoki Shiota7c3111b2016-06-09 16:12:11 -0700919
920 private class InternalStoreListener
921 implements MapEventListener<PacketLinkRealizedByOptical, OpticalConnectivity> {
922
923 @Override
924 public void event(MapEvent<PacketLinkRealizedByOptical, OpticalConnectivity> event) {
925 switch (event.type()) {
926 case UPDATE:
927 OpticalConnectivity oldConnectivity = event.oldValue().value();
928 OpticalConnectivity newConnectivity = event.newValue().value();
929
930 if (!oldConnectivity.isAllRealizingLinkEstablished() &&
931 newConnectivity.isAllRealizingLinkEstablished()) {
932 // Notifies listeners if all links are established
933 updateBandwidthUsage(newConnectivity);
934 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_INSTALLED, newConnectivity.id()));
935 } else if (!oldConnectivity.isAllRealizingLinkNotEstablished() &&
936 newConnectivity.isAllRealizingLinkNotEstablished()) {
937 // Notifies listeners if all links are gone
938 releaseBandwidthUsage(newConnectivity);
939 post(new OpticalPathEvent(OpticalPathEvent.Type.PATH_REMOVED, newConnectivity.id()));
940 }
941
942 break;
943 default:
944 break;
945 }
946 }
947
948 }
Naoki Shiota5a056062016-05-05 18:43:59 -0700949}
950