blob: 9a2b6bfeaed455d19be3fafa3b6adf34560e0658 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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 Leenheer723f5532015-06-03 20:16:17 -070027import org.onosproject.net.AnnotationKeys;
Marc De Leenheer2c305302015-12-07 21:37:44 -080028import org.onosproject.net.ChannelSpacing;
Brian O'Connorabafb502014-12-02 22:26:20 -080029import org.onosproject.net.ConnectPoint;
Marc De Leenheer2c305302015-12-07 21:37:44 -080030import org.onosproject.net.DefaultOchSignalComparator;
Marc De Leenheer723f5532015-06-03 20:16:17 -070031import org.onosproject.net.DeviceId;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070032import org.onosproject.net.GridType;
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.Resource;
Marc De Leenheer03a52b02017-06-05 20:36:23 -070046import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080047import 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;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070067import java.util.stream.IntStream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070068
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070069import static com.google.common.base.Preconditions.checkArgument;
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();
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700111 checkArgument(deviceService.getPort(src.deviceId(), src.port()) instanceof OchPort);
112 checkArgument(deviceService.getPort(dst.deviceId(), dst.port()) instanceof OchPort);
113 List<Resource> resources = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700114
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700115 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
116
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200117 // Release of intent resources here is only a temporary solution for handling the
118 // case of recompiling due to intent restoration (when intent state is FAILED).
119 // TODO: try to release intent resources in IntentManager.
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700120 resourceService.release(intent.key());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200121
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800122 // Check OCh port availability
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700123 // If ports are not available, compilation fails
124 // Else add port to resource reservation list
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800125 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
126 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800127 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 }
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800131 resources.add(srcPortResource);
132 resources.add(dstPortResource);
133
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700134 // Find first path that has the required resources
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700135 Stream<Path> paths = getOpticalPaths(intent);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700136 Optional<Map.Entry<Path, List<OchSignal>>> found = paths
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700137 .map(path -> Maps.immutableEntry(path, findFirstAvailableLambda(intent, path)))
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800138 .filter(entry -> !entry.getValue().isEmpty())
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700139 .filter(entry -> convertToResources(entry.getKey(),
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700140 entry.getValue()).stream().allMatch(resourceService::isAvailable))
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800141 .findFirst();
142
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700143 // Allocate resources and create optical path intent
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800144 if (found.isPresent()) {
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700145 resources.addAll(convertToResources(found.get().getKey(), found.get().getValue()));
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800146 allocateResources(intent, resources);
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800147 OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
148 return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
149 } else {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700150 log.error("Unable to find suitable lightpath for intent {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700151 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700152 }
weibitf32383b2014-10-22 10:17:31 -0700153 }
154
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700155 /**
156 * Create installable optical path intent.
157 * Only supports fixed grid for now.
158 *
159 * @param parentIntent this intent (used for resource tracking)
160 * @param path the path to use
161 * @param lambda the lambda to use
162 * @return optical path intent
163 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800164 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800165 OchSignalType signalType = OchSignalType.FIXED_GRID;
166
167 return OpticalPathIntent.builder()
168 .appId(parentIntent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700169 .key(parentIntent.key())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800170 .src(parentIntent.getSrc())
171 .dst(parentIntent.getDst())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800172 .path(path)
173 .lambda(lambda)
174 .signalType(signalType)
175 .bidirectional(parentIntent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800176 .resourceGroup(parentIntent.resourceGroup())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800177 .build();
178 }
179
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700180 /**
181 * Convert given lambda as discrete resource of all path ports.
182 *
183 * @param path the path
184 * @param lambda the lambda
185 * @return list of discrete resources
186 */
187 private List<Resource> convertToResources(Path path, Collection<OchSignal> lambda) {
188 return path.links().stream()
189 .flatMap(x -> Stream.of(
190 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
191 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
192 ))
193 .flatMap(x -> lambda.stream().map(x::child))
194 .collect(Collectors.toList());
195 }
196
197 /**
198 * Reserve all required resources for this intent.
199 *
200 * @param intent the intent
201 * @param resources list of resources to reserve
202 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800203 private void allocateResources(Intent intent, List<Resource> resources) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700204 List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800205 if (allocations.isEmpty()) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700206 log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700207 if (log.isDebugEnabled()) {
208 log.debug("requested resources:\n\t{}", resources.stream()
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700209 .map(Resource::toString)
210 .collect(Collectors.joining("\n\t")));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700211 }
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700212 throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800213 }
214 }
215
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700216 /**
217 * Find the first available lambda on the given path by checking all the port resources.
218 *
219 * @param path the path
220 * @return list of consecutive and available OChSignals
221 */
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700222 private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) {
223 if (intent.ochSignal().isPresent()) {
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700224 //create lambdas w.r.t. slotGanularity/slotWidth
225 OchSignal ochSignal = intent.ochSignal().get();
226 if (ochSignal.gridType() == GridType.FLEX) {
227 // multiplier sits in the middle of slots
228 int startMultiplier = ochSignal.spacingMultiplier() - (ochSignal.slotGranularity() / 2);
229 return IntStream.range(0, ochSignal.slotGranularity())
230 .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x)))
231 .collect(Collectors.toList());
232 } else if (ochSignal.gridType() == GridType.DWDM) {
233 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
234 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
235 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
236 return IntStream.range(0, ochSignal.slotGranularity())
237 .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x)))
238 .collect(Collectors.toList());
239 }
240 //TODO: add support for other gridTypes
241 log.error("Grid type: {} not supported for user defined signal intents", ochSignal.gridType());
242 return Collections.emptyList();
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700243 }
244
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700245 Set<OchSignal> lambdas = findCommonLambdas(path);
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700246 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800247 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700248 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700249
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800250 return findFirstLambda(lambdas, slotCount());
251 }
252
Marc De Leenheer2c305302015-12-07 21:37:44 -0800253 /**
254 * Get the number of 12.5 GHz slots required for the path.
255 *
256 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
257 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
258 *
259 * @return number of slots
260 */
261 private int slotCount() {
262 return SLOT_COUNT;
263 }
264
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700265 /**
266 * Find common lambdas on all ports that compose the path.
267 *
268 * @param path the path
269 * @return set of common lambdas
270 */
271 private Set<OchSignal> findCommonLambdas(Path path) {
272 return path.links().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800273 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800274 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
275 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800276 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800277 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800278 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700279 .reduce(Sets::intersection)
280 .orElse(Collections.emptySet());
281 }
282
Marc De Leenheer2c305302015-12-07 21:37:44 -0800283 /**
284 * Returns list of consecutive resources in given set of lambdas.
285 *
286 * @param lambdas list of lambdas
287 * @param count number of consecutive lambdas to return
288 * @return list of consecutive lambdas
289 */
290 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
291 // Sort available lambdas
292 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
293 lambdaList.sort(new DefaultOchSignalComparator());
294
295 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
296 for (int i = 0; i < lambdaList.size() - count; i++) {
297 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
298 lambdaList.get(i + count).spacingMultiplier()) {
299 return lambdaList.subList(i, i + count);
300 }
301 }
302
303 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700304 }
305
Marc De Leenheer723f5532015-06-03 20:16:17 -0700306 private ConnectPoint staticPort(ConnectPoint connectPoint) {
307 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
308
309 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
310
311 // FIXME: need a better way to match the port
312 if (staticPort != null) {
313 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
314 if (staticPort.equals(p.number().name())) {
315 return new ConnectPoint(p.element().id(), p.number());
316 }
317 }
318 }
319
320 return null;
321 }
322
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700323 /**
324 * Calculates optical paths in WDM topology.
325 *
326 * @param intent optical connectivity intent
327 * @return set of paths in WDM topology
328 */
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700329 private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700330 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700331 Topology topology = topologyService.currentTopology();
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700332 //TODO: refactor with LinkWeigher class Implementation
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700333 LinkWeight weight = new LinkWeight() {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700334
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700335 @Override
336 public double weight(TopologyEdge edge) {
337 // Disregard inactive or non-optical links
338 if (edge.link().state() == Link.State.INACTIVE) {
339 return -1;
340 }
341 if (edge.link().type() != Link.Type.OPTICAL) {
342 return -1;
343 }
344 // Adhere to static port mappings
345 DeviceId srcDeviceId = edge.link().src().deviceId();
346 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
347 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
348 if (srcStaticPort != null) {
349 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
350 }
351 }
352 DeviceId dstDeviceId = edge.link().dst().deviceId();
353 if (dstDeviceId.equals(intent.getDst().deviceId())) {
354 ConnectPoint dstStaticPort = staticPort(intent.getDst());
355 if (dstStaticPort != null) {
356 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
357 }
358 }
359
360 return 1;
361 }
weibit7e583462014-10-23 10:14:05 -0700362 };
weibitf32383b2014-10-22 10:17:31 -0700363
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700364 ConnectPoint start = intent.getSrc();
365 ConnectPoint end = intent.getDst();
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700366 //head link's src port should be same as intent src port and tail link dst port
367 //should be same as intent dst port in the path.
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700368 Stream<Path> paths = topologyService.getKShortestPaths(topology,
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700369 start.deviceId(),
370 end.deviceId(),
371 AdapterLinkWeigher.adapt(weight))
372 .filter(p -> p.links().get(0).src().port().equals(start.port()) &&
373 p.links().get(p.links().size() - 1).dst().port().equals(end.port()));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700374 if (log.isDebugEnabled()) {
375 return paths
376 .map(path -> {
377 // no-op map stage to add debug logging
378 log.debug("Candidate path: {}",
379 path.links().stream()
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700380 .map(lk -> lk.src() + "-" + lk.dst())
381 .collect(Collectors.toList()));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700382 return path;
383 });
384 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700385 return paths;
weibitf32383b2014-10-22 10:17:31 -0700386 }
weibitf32383b2014-10-22 10:17:31 -0700387}