blob: 55932330646ff075794941aae9841f991e4218a8 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor0a4e6742016-09-15 23:03:10 -07002 * Copyright 2016-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070016package org.onosproject.net.optical.intent.impl.compiler;
weibitf32383b2014-10-22 10:17:31 -070017
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070018import com.google.common.collect.ImmutableList;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070019import com.google.common.collect.ImmutableSet;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080020import com.google.common.collect.Maps;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070021import com.google.common.collect.Sets;
weibitf32383b2014-10-22 10:17:31 -070022import org.apache.felix.scr.annotations.Activate;
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070023import org.apache.felix.scr.annotations.Component;
weibitf32383b2014-10-22 10:17:31 -070024import org.apache.felix.scr.annotations.Deactivate;
25import org.apache.felix.scr.annotations.Reference;
26import org.apache.felix.scr.annotations.ReferenceCardinality;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070027import org.onlab.util.Frequency;
Marc De Leenheer723f5532015-06-03 20:16:17 -070028import org.onosproject.net.AnnotationKeys;
Marc De Leenheer2c305302015-12-07 21:37:44 -080029import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.ConnectPoint;
Marc De Leenheer2c305302015-12-07 21:37:44 -080031import org.onosproject.net.DefaultOchSignalComparator;
Marc De Leenheer723f5532015-06-03 20:16:17 -070032import org.onosproject.net.DeviceId;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070034import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070035import org.onosproject.net.OchSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070037import org.onosproject.net.Port;
38import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.intent.Intent;
40import org.onosproject.net.intent.IntentCompiler;
41import org.onosproject.net.intent.IntentExtensionService;
42import org.onosproject.net.intent.OpticalConnectivityIntent;
43import org.onosproject.net.intent.OpticalPathIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080044import org.onosproject.net.optical.OchPort;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080045import org.onosproject.net.resource.ResourceAllocation;
46import org.onosproject.net.resource.Resource;
47import org.onosproject.net.resource.ResourceService;
48import org.onosproject.net.resource.Resources;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070049import org.onosproject.net.topology.AdapterLinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080050import org.onosproject.net.topology.LinkWeight;
51import org.onosproject.net.topology.Topology;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070052import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080053import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070054import org.slf4j.Logger;
55import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070056
Marc De Leenheer2c305302015-12-07 21:37:44 -080057import java.util.ArrayList;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070058import java.util.Collection;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070059import java.util.Collections;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070060import java.util.LinkedList;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070061import java.util.List;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080062import java.util.Map;
63import java.util.Optional;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070064import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070065import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080066import java.util.stream.Stream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070067
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070068import static com.google.common.base.Preconditions.checkArgument;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070069import static org.onosproject.net.OchSignal.toFlexGrid;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080070import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070071
weibitf32383b2014-10-22 10:17:31 -070072/**
Brian O'Connorabafb502014-12-02 22:26:20 -080073 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070074 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070075@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070076public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
77
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070078 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
Marc De Leenheer2c305302015-12-07 21:37:44 -080079 // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent.
80 private static final int SLOT_COUNT = 4;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070081
weibitf32383b2014-10-22 10:17:31 -070082 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
83 protected IntentExtensionService intentManager;
84
85 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070086 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070087
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070088 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
89 protected DeviceService deviceService;
90
91 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070092 protected ResourceService resourceService;
93
weibitf32383b2014-10-22 10:17:31 -070094 @Activate
95 public void activate() {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080096 deviceService = opticalView(deviceService);
weibitf32383b2014-10-22 10:17:31 -070097 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
98 }
99
100 @Deactivate
101 public void deactivate() {
102 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
103 }
104
105 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700106 public List<Intent> compile(OpticalConnectivityIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800107 List<Intent> installable) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700108 // Check if source and destination are optical OCh ports
109 ConnectPoint src = intent.getSrc();
110 ConnectPoint dst = intent.getDst();
111 Port srcPort = deviceService.getPort(src.deviceId(), src.port());
112 Port dstPort = deviceService.getPort(dst.deviceId(), dst.port());
113 checkArgument(srcPort instanceof OchPort);
114 checkArgument(dstPort instanceof OchPort);
115
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700116 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
117
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200118 // Release of intent resources here is only a temporary solution for handling the
119 // case of recompiling due to intent restoration (when intent state is FAILED).
120 // TODO: try to release intent resources in IntentManager.
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700121 resourceService.release(intent.key());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200122
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800123 // Check OCh port availability
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800124 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
125 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800126 // If ports are not available, compilation fails
127 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700128 log.error("Ports for the intent are not available. Intent: {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700129 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700130 }
131
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800132 List<Resource> resources = new ArrayList<>();
133 resources.add(srcPortResource);
134 resources.add(dstPortResource);
135
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700136 // Calculate available light paths
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700137 Stream<Path> paths = getOpticalPaths(intent);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800138
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800139 // Static or dynamic lambda allocation
140 String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA);
141 OchPort srcOchPort = (OchPort) srcPort;
142 OchPort dstOchPort = (OchPort) dstPort;
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800143
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800144 // FIXME: need to actually reserve the lambda for static lambda's
145 // static lambda case: early return
146 if (staticLambda != null) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800147
148 OchSignal lambda = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)),
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700149 srcOchPort.lambda().channelSpacing(),
150 srcOchPort.lambda().slotGranularity());
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700151 log.debug("Using statically assigned lambda : {}", lambda);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700152
153 List<Resource> res = new ArrayList<>();
154
155 Path satPath = paths.filter(path -> {
156 // FIXME trimming both ends (staticLambda assigned ports)
157 // Proper fix is to let device advertise static lambda as resource.
158 LinkedList<Link> links = new LinkedList<>(path.links());
159 links.pollFirst();
160 links.pollLast();
161
162 List<Resource> r = convertToResources(links, toFlexGrid(lambda));
163 if (r.stream().allMatch(resourceService::isAvailable)) {
164 // FIXME bad practice to have side-effect during stream
165 res.addAll(r);
166 return true;
167 }
168 return false;
169 }).findFirst().orElse(null);
170
171 if (satPath == null) {
172 // FIXME doing something very wrong to preserve old behavior
173 // as a fallback
174 log.warn("No feasible path between {}-{} using {}",
175 src, dst, lambda);
176
177 satPath = getOpticalPaths(intent).iterator().next();
178 if (satPath == null) {
179 throw new OpticalIntentCompilationException(
180 "Unable to find suitable lightpath for intent " + intent);
181 }
182 return ImmutableList.of(createIntent(intent, satPath, lambda));
183 }
184
185 resources.addAll(res);
186 allocateResources(intent, resources);
187
188 return ImmutableList.of(createIntent(intent, satPath, lambda));
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800189 }
190
191 // FIXME: also check destination OCh port
192 // non-tunable case: early return
193 if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) {
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700194 Path firstPath = paths.findAny().orElse(null);
195 if (firstPath == null) {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700196 log.error("Unable to find suitable lightpath for intent {}", intent);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700197 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
198 }
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800199 OchSignal lambda = srcOchPort.lambda();
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700200 // TODO apply the same as static lambda case above
201 // - pick feasible path
202 // - allocate resources
203 //resources.addAll(convertToResources(firstPath.links(), toFlexGrid(lambda)));
204 allocateResources(intent, resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800205 return ImmutableList.of(createIntent(intent, firstPath, lambda));
206 }
Sho SHIMIZUd978d9a2016-01-28 08:55:49 -0800207
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800208 // remaining cases
209 // Use first path that the required resources are available
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700210 Optional<Map.Entry<Path, List<OchSignal>>> found = paths
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800211 .map(path -> Maps.immutableEntry(path, findFirstAvailableOch(path)))
212 .filter(entry -> !entry.getValue().isEmpty())
213 .filter(entry -> convertToResources(entry.getKey().links(),
214 entry.getValue()).stream().allMatch(resourceService::isAvailable))
215 .findFirst();
216
217 if (found.isPresent()) {
218 resources.addAll(convertToResources(found.get().getKey().links(), found.get().getValue()));
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700219
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800220 allocateResources(intent, resources);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700221
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800222 OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
223 return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
224 } else {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700225 log.error("Unable to find suitable lightpath for intent {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700226 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700227 }
weibitf32383b2014-10-22 10:17:31 -0700228 }
229
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800230 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
231 // Create installable optical path intent
232 // Only support fixed grid for now
233 OchSignalType signalType = OchSignalType.FIXED_GRID;
234
235 return OpticalPathIntent.builder()
236 .appId(parentIntent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700237 .key(parentIntent.key())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800238 .src(parentIntent.getSrc())
239 .dst(parentIntent.getDst())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800240 .path(path)
241 .lambda(lambda)
242 .signalType(signalType)
243 .bidirectional(parentIntent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800244 .resourceGroup(parentIntent.resourceGroup())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800245 .build();
246 }
247
248 private void allocateResources(Intent intent, List<Resource> resources) {
249 // reserve all of required resources
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700250 List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800251 if (allocations.isEmpty()) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700252 log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700253 if (log.isDebugEnabled()) {
254 log.debug("requested resources:\n\t{}", resources.stream()
255 .map(Resource::toString)
256 .collect(Collectors.joining("\n\t")));
257 }
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700258 throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800259 }
260 }
261
262 private List<OchSignal> findFirstAvailableOch(Path path) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800263 Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links());
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700264 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800265 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700266 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700267
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800268 return findFirstLambda(lambdas, slotCount());
269 }
270
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700271 private List<Resource> convertToResources(List<Link> links, Collection<OchSignal> lambda) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800272 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800273 .flatMap(x -> Stream.of(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800274 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
275 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800276 ))
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800277 .flatMap(x -> lambda.stream().map(x::child))
Sho SHIMIZU7f44cfc2015-10-28 13:54:40 -0700278 .collect(Collectors.toList());
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700279 }
280
Marc De Leenheer2c305302015-12-07 21:37:44 -0800281 /**
282 * Get the number of 12.5 GHz slots required for the path.
283 *
284 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
285 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
286 *
287 * @return number of slots
288 */
289 private int slotCount() {
290 return SLOT_COUNT;
291 }
292
293 private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) {
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700294 return links.stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800295 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800296 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
297 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800298 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800299 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800300 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700301 .reduce(Sets::intersection)
302 .orElse(Collections.emptySet());
303 }
304
Marc De Leenheer2c305302015-12-07 21:37:44 -0800305 /**
306 * Returns list of consecutive resources in given set of lambdas.
307 *
308 * @param lambdas list of lambdas
309 * @param count number of consecutive lambdas to return
310 * @return list of consecutive lambdas
311 */
312 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
313 // Sort available lambdas
314 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
315 lambdaList.sort(new DefaultOchSignalComparator());
316
317 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
318 for (int i = 0; i < lambdaList.size() - count; i++) {
319 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
320 lambdaList.get(i + count).spacingMultiplier()) {
321 return lambdaList.subList(i, i + count);
322 }
323 }
324
325 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700326 }
327
Marc De Leenheer723f5532015-06-03 20:16:17 -0700328 private ConnectPoint staticPort(ConnectPoint connectPoint) {
329 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
330
331 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
332
333 // FIXME: need a better way to match the port
334 if (staticPort != null) {
335 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
336 if (staticPort.equals(p.number().name())) {
337 return new ConnectPoint(p.element().id(), p.number());
338 }
339 }
340 }
341
342 return null;
343 }
344
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700345 /**
346 * Calculates optical paths in WDM topology.
347 *
348 * @param intent optical connectivity intent
349 * @return set of paths in WDM topology
350 */
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700351 private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700352 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700353 Topology topology = topologyService.currentTopology();
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700354 LinkWeight weight = new LinkWeight() {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700355
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700356 @Override
357 public double weight(TopologyEdge edge) {
358 // Disregard inactive or non-optical links
359 if (edge.link().state() == Link.State.INACTIVE) {
360 return -1;
361 }
362 if (edge.link().type() != Link.Type.OPTICAL) {
363 return -1;
364 }
365 // Adhere to static port mappings
366 DeviceId srcDeviceId = edge.link().src().deviceId();
367 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
368 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
369 if (srcStaticPort != null) {
370 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
371 }
372 }
373 DeviceId dstDeviceId = edge.link().dst().deviceId();
374 if (dstDeviceId.equals(intent.getDst().deviceId())) {
375 ConnectPoint dstStaticPort = staticPort(intent.getDst());
376 if (dstStaticPort != null) {
377 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
378 }
379 }
380
381 return 1;
382 }
weibit7e583462014-10-23 10:14:05 -0700383 };
weibitf32383b2014-10-22 10:17:31 -0700384
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700385 ConnectPoint start = intent.getSrc();
386 ConnectPoint end = intent.getDst();
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700387 Stream<Path> paths = topologyService.getKShortestPaths(topology,
388 start.deviceId(),
389 end.deviceId(),
390 AdapterLinkWeigher.adapt(weight));
391 if (log.isDebugEnabled()) {
392 return paths
393 .map(path -> {
394 // no-op map stage to add debug logging
395 log.debug("Candidate path: {}",
396 path.links().stream()
397 .map(lk -> lk.src() + "-" + lk.dst())
398 .collect(Collectors.toList()));
399 return path;
400 });
401 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700402 return paths;
weibitf32383b2014-10-22 10:17:31 -0700403 }
weibitf32383b2014-10-22 10:17:31 -0700404}