blob: 682ea1f435d5f5c9e2db2fb8bd7d2cfb684828fb [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;
Boyuan Yan10822382019-02-22 09:45:42 -080022import org.onosproject.net.ConnectPoint;
23import org.onosproject.net.GridType;
24import org.onosproject.net.OchSignal;
25import org.onosproject.net.Path;
26import org.onosproject.net.ChannelSpacing;
27import org.onosproject.net.Port;
28import org.onosproject.net.Link;
29import org.onosproject.net.DeviceId;
30import org.onosproject.net.Annotations;
31import org.onosproject.net.AnnotationKeys;
32import org.onosproject.net.DefaultAnnotations;
33import org.onosproject.net.DefaultPath;
34import org.onosproject.net.OchSignalType;
35import org.onosproject.net.DefaultOchSignalComparator;
36import org.onosproject.net.DefaultLink;
alessiof3beff22020-07-17 17:17:11 +020037import org.onosproject.net.resource.Resource;
38import org.onosproject.net.resource.ResourceAllocation;
39import org.onosproject.net.resource.ResourceService;
40import org.onosproject.net.resource.Resources;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070041import org.osgi.service.component.annotations.Activate;
42import org.osgi.service.component.annotations.Component;
43import org.osgi.service.component.annotations.Deactivate;
44import org.osgi.service.component.annotations.Reference;
45import org.osgi.service.component.annotations.ReferenceCardinality;
Ray Milkey7483e1b2018-02-07 15:43:01 -080046import org.onlab.graph.ScalarWeight;
47import org.onlab.graph.Weight;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070048import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.intent.Intent;
50import org.onosproject.net.intent.IntentCompiler;
51import org.onosproject.net.intent.IntentExtensionService;
52import org.onosproject.net.intent.OpticalConnectivityIntent;
53import org.onosproject.net.intent.OpticalPathIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080054import org.onosproject.net.optical.OchPort;
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -080055import org.onosproject.net.provider.ProviderId;
Ray Milkey7483e1b2018-02-07 15:43:01 -080056import org.onosproject.net.topology.LinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080057import org.onosproject.net.topology.Topology;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070058import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080059import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070060import org.slf4j.Logger;
61import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070062
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070063import java.util.List;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080064import java.util.Map;
65import java.util.Optional;
alessio3039bdb2018-11-29 14:12:32 +010066import java.util.Collection;
67import java.util.LinkedList;
68import java.util.ArrayList;
69import java.util.Collections;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070070import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070071import java.util.stream.Collectors;
fahadnaeemkhanffc917f2017-10-03 14:04:46 -070072import java.util.stream.IntStream;
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -080073import java.util.stream.Stream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070074
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070075import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080076import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070077
weibitf32383b2014-10-22 10:17:31 -070078/**
Brian O'Connorabafb502014-12-02 22:26:20 -080079 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070080 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070081@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070082public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
83
Ray Milkey9c9cde42018-01-12 14:22:06 -080084 private static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
Marc De Leenheer2c305302015-12-07 21:37:44 -080085 // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent.
86 private static final int SLOT_COUNT = 4;
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -080087 private static final ProviderId PROVIDER_ID = new ProviderId("opticalConnectivityIntent",
88 "org.onosproject.net.optical.intent");
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070089
Ray Milkeyd84f89b2018-08-17 14:54:17 -070090 @Reference(cardinality = ReferenceCardinality.MANDATORY)
weibitf32383b2014-10-22 10:17:31 -070091 protected IntentExtensionService intentManager;
92
Ray Milkeyd84f89b2018-08-17 14:54:17 -070093 @Reference(cardinality = ReferenceCardinality.MANDATORY)
weibit7e583462014-10-23 10:14:05 -070094 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070095
Ray Milkeyd84f89b2018-08-17 14:54:17 -070096 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070097 protected DeviceService deviceService;
98
Ray Milkeyd84f89b2018-08-17 14:54:17 -070099 @Reference(cardinality = ReferenceCardinality.MANDATORY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -0700100 protected ResourceService resourceService;
101
weibitf32383b2014-10-22 10:17:31 -0700102 @Activate
103 public void activate() {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -0800104 deviceService = opticalView(deviceService);
weibitf32383b2014-10-22 10:17:31 -0700105 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
106 }
107
108 @Deactivate
109 public void deactivate() {
110 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
111 }
112
113 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700114 public List<Intent> compile(OpticalConnectivityIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800115 List<Intent> installable) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700116 // Check if source and destination are optical OCh ports
117 ConnectPoint src = intent.getSrc();
118 ConnectPoint dst = intent.getDst();
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700119 checkArgument(deviceService.getPort(src.deviceId(), src.port()) instanceof OchPort);
120 checkArgument(deviceService.getPort(dst.deviceId(), dst.port()) instanceof OchPort);
121 List<Resource> resources = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700122
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700123 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
124
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200125 // Release of intent resources here is only a temporary solution for handling the
126 // case of recompiling due to intent restoration (when intent state is FAILED).
127 // TODO: try to release intent resources in IntentManager.
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700128 resourceService.release(intent.key());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200129
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800130 // Check OCh port availability
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700131 // If ports are not available, compilation fails
132 // Else add port to resource reservation list
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800133 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
134 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800135 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700136 log.error("Ports for the intent are not available. Intent: {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700137 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700138 }
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800139 resources.add(srcPortResource);
140 resources.add(dstPortResource);
141
alessio3039bdb2018-11-29 14:12:32 +0100142 // If there is a suggestedPath, use this path without further checking, otherwise trigger path computation
143 Stream<Path> paths;
144 if (intent.suggestedPath().isPresent()) {
145 paths = Stream.of(intent.suggestedPath().get());
146 } else {
147 paths = getOpticalPaths(intent);
148 }
149
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700150 // Find first path that has the required resources
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700151 Optional<Map.Entry<Path, List<OchSignal>>> found = paths
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700152 .map(path -> Maps.immutableEntry(path, findFirstAvailableLambda(intent, path)))
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800153 .filter(entry -> !entry.getValue().isEmpty())
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700154 .filter(entry -> convertToResources(entry.getKey(),
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800155 entry.getValue()).stream().allMatch(resourceService::isAvailable))
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800156 .findFirst();
157
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700158 // Allocate resources and create optical path intent
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800159 if (found.isPresent()) {
alessiof3beff22020-07-17 17:17:11 +0200160 log.debug("Suitable lightpath FOUND for intent {}", intent);
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700161 resources.addAll(convertToResources(found.get().getKey(), found.get().getValue()));
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800162 allocateResources(intent, resources);
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800163 OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
164 return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
165 } else {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700166 log.error("Unable to find suitable lightpath for intent {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700167 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700168 }
weibitf32383b2014-10-22 10:17:31 -0700169 }
170
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700171 /**
172 * Create installable optical path intent.
173 * Only supports fixed grid for now.
174 *
175 * @param parentIntent this intent (used for resource tracking)
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800176 * @param path the path to use
177 * @param lambda the lambda to use
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700178 * @return optical path intent
179 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800180 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800181 OchSignalType signalType = OchSignalType.FIXED_GRID;
182
183 return OpticalPathIntent.builder()
184 .appId(parentIntent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700185 .key(parentIntent.key())
alessio0a0f3342019-10-28 16:58:01 +0100186 .priority(parentIntent.priority())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800187 .src(parentIntent.getSrc())
188 .dst(parentIntent.getDst())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800189 .path(path)
190 .lambda(lambda)
191 .signalType(signalType)
192 .bidirectional(parentIntent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800193 .resourceGroup(parentIntent.resourceGroup())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800194 .build();
195 }
196
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700197 /**
198 * Convert given lambda as discrete resource of all path ports.
199 *
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800200 * @param path the path
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700201 * @param lambda the lambda
202 * @return list of discrete resources
203 */
204 private List<Resource> convertToResources(Path path, Collection<OchSignal> lambda) {
205 return path.links().stream()
206 .flatMap(x -> Stream.of(
alessiof3beff22020-07-17 17:17:11 +0200207 Resources.discrete(x.src().deviceId(),
208 deviceService.getPort(x.src().deviceId(), x.src().port()).number()).resource(),
209 Resources.discrete(x.dst().deviceId(),
210 deviceService.getPort(x.dst().deviceId(), x.dst().port()).number()).resource()
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700211 ))
212 .flatMap(x -> lambda.stream().map(x::child))
213 .collect(Collectors.toList());
214 }
215
216 /**
217 * Reserve all required resources for this intent.
218 *
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800219 * @param intent the intent
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700220 * @param resources list of resources to reserve
221 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800222 private void allocateResources(Intent intent, List<Resource> resources) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700223 List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800224 if (allocations.isEmpty()) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700225 log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700226 if (log.isDebugEnabled()) {
227 log.debug("requested resources:\n\t{}", resources.stream()
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700228 .map(Resource::toString)
229 .collect(Collectors.joining("\n\t")));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700230 }
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700231 throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800232 }
233 }
234
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700235 /**
236 * Find the first available lambda on the given path by checking all the port resources.
237 *
238 * @param path the path
239 * @return list of consecutive and available OChSignals
240 */
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700241 private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) {
242 if (intent.ochSignal().isPresent()) {
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700243 //create lambdas w.r.t. slotGanularity/slotWidth
244 OchSignal ochSignal = intent.ochSignal().get();
245 if (ochSignal.gridType() == GridType.FLEX) {
246 // multiplier sits in the middle of slots
247 int startMultiplier = ochSignal.spacingMultiplier() - (ochSignal.slotGranularity() / 2);
248 return IntStream.range(0, ochSignal.slotGranularity())
249 .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x)))
250 .collect(Collectors.toList());
251 } else if (ochSignal.gridType() == GridType.DWDM) {
252 int startMultiplier = (int) (1 - ochSignal.slotGranularity() +
253 ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() /
254 ChannelSpacing.CHL_6P25GHZ.frequency().asHz());
255 return IntStream.range(0, ochSignal.slotGranularity())
256 .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x)))
257 .collect(Collectors.toList());
258 }
259 //TODO: add support for other gridTypes
260 log.error("Grid type: {} not supported for user defined signal intents", ochSignal.gridType());
261 return Collections.emptyList();
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700262 }
263
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700264 Set<OchSignal> lambdas = findCommonLambdas(path);
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700265 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800266 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700267 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700268
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800269 return findFirstLambda(lambdas, slotCount());
270 }
271
Marc De Leenheer2c305302015-12-07 21:37:44 -0800272 /**
273 * Get the number of 12.5 GHz slots required for the path.
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800274 * <p>
Marc De Leenheer2c305302015-12-07 21:37:44 -0800275 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
276 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
277 *
278 * @return number of slots
279 */
280 private int slotCount() {
281 return SLOT_COUNT;
282 }
283
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700284 /**
285 * Find common lambdas on all ports that compose the path.
286 *
287 * @param path the path
288 * @return set of common lambdas
289 */
290 private Set<OchSignal> findCommonLambdas(Path path) {
alessiof3beff22020-07-17 17:17:11 +0200291
292 Set<OchSignal> ochSignals = path.links().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800293 .flatMap(x -> Stream.of(
alessiof3beff22020-07-17 17:17:11 +0200294 Resources.discrete(x.src().deviceId(),
295 deviceService.getPort(x.src().deviceId(), x.src().port()).number()).id(),
296 Resources.discrete(x.dst().deviceId(),
297 deviceService.getPort(x.dst().deviceId(), x.dst().port()).number()).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());
alessiof3beff22020-07-17 17:17:11 +0200303
304 if (ochSignals.isEmpty()) {
305 log.warn("Common lambdas not found");
306 } else {
307 log.debug("Common lambdas found {}", ochSignals);
308 }
309
310 return ochSignals;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700311 }
312
Marc De Leenheer2c305302015-12-07 21:37:44 -0800313 /**
314 * Returns list of consecutive resources in given set of lambdas.
315 *
316 * @param lambdas list of lambdas
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800317 * @param count number of consecutive lambdas to return
Marc De Leenheer2c305302015-12-07 21:37:44 -0800318 * @return list of consecutive lambdas
319 */
320 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
321 // Sort available lambdas
322 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
323 lambdaList.sort(new DefaultOchSignalComparator());
Andrea Campanellae4b2c682019-08-23 15:42:55 +0200324 //Means there is only exactly one set of lambdas available
325 if (lambdaList.size() == count) {
326 return lambdaList;
327 }
Marc De Leenheer2c305302015-12-07 21:37:44 -0800328 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
329 for (int i = 0; i < lambdaList.size() - count; i++) {
330 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
331 lambdaList.get(i + count).spacingMultiplier()) {
332 return lambdaList.subList(i, i + count);
333 }
334 }
335
336 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700337 }
338
Marc De Leenheer723f5532015-06-03 20:16:17 -0700339 private ConnectPoint staticPort(ConnectPoint connectPoint) {
340 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
341
342 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
343
344 // FIXME: need a better way to match the port
345 if (staticPort != null) {
346 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
347 if (staticPort.equals(p.number().name())) {
348 return new ConnectPoint(p.element().id(), p.number());
349 }
350 }
351 }
352
353 return null;
354 }
355
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700356 /**
357 * Calculates optical paths in WDM topology.
358 *
359 * @param intent optical connectivity intent
360 * @return set of paths in WDM topology
361 */
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700362 private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700363 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700364 Topology topology = topologyService.currentTopology();
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700365 //TODO: refactor with LinkWeigher class Implementation
Ray Milkey7483e1b2018-02-07 15:43:01 -0800366 LinkWeigher weight = new LinkWeigher() {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700367
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700368 @Override
Ray Milkey7483e1b2018-02-07 15:43:01 -0800369 public Weight getInitialWeight() {
370 return ScalarWeight.toWeight(0.0);
371 }
372
373 @Override
374 public Weight getNonViableWeight() {
375 return ScalarWeight.NON_VIABLE_WEIGHT;
376 }
377
alessio3039bdb2018-11-29 14:12:32 +0100378 /**
379 *
380 * @param edge edge to be weighed
381 * @return the metric retrieved from the annotations otherwise 1
382 */
Ray Milkey7483e1b2018-02-07 15:43:01 -0800383 @Override
384 public Weight weight(TopologyEdge edge) {
alessio3039bdb2018-11-29 14:12:32 +0100385
386 log.debug("Link {} metric {}", edge.link(), edge.link().annotations().value("metric"));
387
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700388 // Disregard inactive or non-optical links
389 if (edge.link().state() == Link.State.INACTIVE) {
Ray Milkey7483e1b2018-02-07 15:43:01 -0800390 return ScalarWeight.toWeight(-1);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700391 }
392 if (edge.link().type() != Link.Type.OPTICAL) {
Ray Milkey7483e1b2018-02-07 15:43:01 -0800393 return ScalarWeight.toWeight(-1);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700394 }
395 // Adhere to static port mappings
396 DeviceId srcDeviceId = edge.link().src().deviceId();
397 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
398 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
399 if (srcStaticPort != null) {
Ray Milkey7483e1b2018-02-07 15:43:01 -0800400 return ScalarWeight.toWeight(srcStaticPort.equals(edge.link().src()) ? 1 : -1);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700401 }
402 }
403 DeviceId dstDeviceId = edge.link().dst().deviceId();
404 if (dstDeviceId.equals(intent.getDst().deviceId())) {
405 ConnectPoint dstStaticPort = staticPort(intent.getDst());
406 if (dstStaticPort != null) {
Ray Milkey7483e1b2018-02-07 15:43:01 -0800407 return ScalarWeight.toWeight(dstStaticPort.equals(edge.link().dst()) ? 1 : -1);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700408 }
409 }
410
Boyuan Yan10822382019-02-22 09:45:42 -0800411 Annotations annotations = edge.link().annotations();
Jan Kundrát12eb26a2019-11-08 10:47:10 +0100412 if (annotations != null &&
413 annotations.value("metric") != null && !annotations.value("metric").isEmpty()) {
Boyuan Yan10822382019-02-22 09:45:42 -0800414 double metric = Double.parseDouble(annotations.value("metric"));
alessio3039bdb2018-11-29 14:12:32 +0100415 return ScalarWeight.toWeight(metric);
416 } else {
417 return ScalarWeight.toWeight(1);
418 }
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700419 }
weibit7e583462014-10-23 10:14:05 -0700420 };
weibitf32383b2014-10-22 10:17:31 -0700421
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700422 ConnectPoint start = intent.getSrc();
423 ConnectPoint end = intent.getDst();
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800424
425 // 0 hop case
426 if (start.deviceId().equals(end.deviceId())) {
427 log.debug("install optical intent for 0 hop i.e srcDeviceId=dstDeviceId");
428 DefaultLink defaultLink = DefaultLink.builder()
429 .providerId(PROVIDER_ID)
430 .src(start)
431 .dst(end)
432 .state(Link.State.ACTIVE)
433 .type(Link.Type.DIRECT)
434 .isExpected(true)
435 .build();
436 List<Link> links = ImmutableList.<Link>builder().add(defaultLink).build();
437 Annotations annotations = DefaultAnnotations.builder().build();
438 DefaultPath defaultPath = new DefaultPath(PROVIDER_ID, links, null, annotations);
439 return ImmutableList.<Path>builder().add(defaultPath).build().stream();
440 }
441
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700442 //head link's src port should be same as intent src port and tail link dst port
443 //should be same as intent dst port in the path.
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700444 Stream<Path> paths = topologyService.getKShortestPaths(topology,
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700445 start.deviceId(),
446 end.deviceId(),
Ray Milkey7483e1b2018-02-07 15:43:01 -0800447 weight)
fahadnaeemkhanffc917f2017-10-03 14:04:46 -0700448 .filter(p -> p.links().get(0).src().port().equals(start.port()) &&
449 p.links().get(p.links().size() - 1).dst().port().equals(end.port()));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700450 if (log.isDebugEnabled()) {
451 return paths
452 .map(path -> {
453 // no-op map stage to add debug logging
454 log.debug("Candidate path: {}",
fahadnaeemkhan0e1b4d92018-02-08 16:40:08 -0800455 path.links().stream()
456 .map(lk -> lk.src() + "-" + lk.dst())
457 .collect(Collectors.toList()));
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700458 return path;
459 });
460 }
alessio3039bdb2018-11-29 14:12:32 +0100461
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700462 return paths;
weibitf32383b2014-10-22 10:17:31 -0700463 }
weibitf32383b2014-10-22 10:17:31 -0700464}