blob: cbf670f0a47f726181396bc93e1f6a255076657d [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 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;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.Link;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070033import org.onosproject.net.OchSignal;
Marc De Leenheerd24420f2015-05-27 09:40:59 -070034import org.onosproject.net.OchSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080035import org.onosproject.net.Path;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070036import org.onosproject.net.Port;
37import org.onosproject.net.device.DeviceService;
Brian O'Connorabafb502014-12-02 22:26:20 -080038import org.onosproject.net.intent.Intent;
39import org.onosproject.net.intent.IntentCompiler;
40import org.onosproject.net.intent.IntentExtensionService;
41import org.onosproject.net.intent.OpticalConnectivityIntent;
42import org.onosproject.net.intent.OpticalPathIntent;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080043import org.onosproject.net.optical.OchPort;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080044import org.onosproject.net.resource.Resource;
Marc De Leenheer03a52b02017-06-05 20:36:23 -070045import org.onosproject.net.resource.ResourceAllocation;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080046import org.onosproject.net.resource.ResourceService;
47import org.onosproject.net.resource.Resources;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070048import org.onosproject.net.topology.AdapterLinkWeigher;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.topology.LinkWeight;
50import org.onosproject.net.topology.Topology;
Yuta HIGUCHId95d5902016-06-27 00:18:45 -070051import org.onosproject.net.topology.TopologyEdge;
Brian O'Connorabafb502014-12-02 22:26:20 -080052import org.onosproject.net.topology.TopologyService;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070053import org.slf4j.Logger;
54import org.slf4j.LoggerFactory;
weibitf32383b2014-10-22 10:17:31 -070055
Marc De Leenheer2c305302015-12-07 21:37:44 -080056import java.util.ArrayList;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070057import java.util.Collection;
Sho SHIMIZUc25a0082015-10-27 17:06:29 -070058import java.util.Collections;
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -070059import java.util.LinkedList;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070060import java.util.List;
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -080061import java.util.Map;
62import java.util.Optional;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070063import java.util.Set;
Sho SHIMIZU280e7912015-09-29 13:42:03 -070064import java.util.stream.Collectors;
Sho SHIMIZU44f37612015-11-25 16:23:22 -080065import java.util.stream.Stream;
Marc De Leenheer0b8b2ef2015-08-03 15:39:00 -070066
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070067import static com.google.common.base.Preconditions.checkArgument;
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080068import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView;
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070069
weibitf32383b2014-10-22 10:17:31 -070070/**
Brian O'Connorabafb502014-12-02 22:26:20 -080071 * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}.
weibitf32383b2014-10-22 10:17:31 -070072 */
Sho SHIMIZU9a2b8292015-10-28 13:00:16 -070073@Component(immediate = true)
weibitf32383b2014-10-22 10:17:31 -070074public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> {
75
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070076 protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class);
Marc De Leenheer2c305302015-12-07 21:37:44 -080077 // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent.
78 private static final int SLOT_COUNT = 4;
Marc De Leenheer8c2caac2015-05-28 16:37:33 -070079
weibitf32383b2014-10-22 10:17:31 -070080 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
81 protected IntentExtensionService intentManager;
82
83 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
weibit7e583462014-10-23 10:14:05 -070084 protected TopologyService topologyService;
weibitf32383b2014-10-22 10:17:31 -070085
Marc De Leenheer1afa2a02015-05-13 09:18:07 -070086 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
87 protected DeviceService deviceService;
88
89 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Sho SHIMIZU5c16df82015-09-29 12:52:07 -070090 protected ResourceService resourceService;
91
weibitf32383b2014-10-22 10:17:31 -070092 @Activate
93 public void activate() {
HIGUCHI Yuta34a3f692016-01-09 21:08:57 -080094 deviceService = opticalView(deviceService);
weibitf32383b2014-10-22 10:17:31 -070095 intentManager.registerCompiler(OpticalConnectivityIntent.class, this);
96 }
97
98 @Deactivate
99 public void deactivate() {
100 intentManager.unregisterCompiler(OpticalConnectivityIntent.class);
101 }
102
103 @Override
Brian O'Connorfa81eae2014-10-30 13:20:05 -0700104 public List<Intent> compile(OpticalConnectivityIntent intent,
Sho SHIMIZUec07ffd2016-02-22 20:45:21 -0800105 List<Intent> installable) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700106 // Check if source and destination are optical OCh ports
107 ConnectPoint src = intent.getSrc();
108 ConnectPoint dst = intent.getDst();
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700109 checkArgument(deviceService.getPort(src.deviceId(), src.port()) instanceof OchPort);
110 checkArgument(deviceService.getPort(dst.deviceId(), dst.port()) instanceof OchPort);
111 List<Resource> resources = new LinkedList<>();
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700112
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700113 log.debug("Compiling optical connectivity intent between {} and {}", src, dst);
114
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200115 // Release of intent resources here is only a temporary solution for handling the
116 // case of recompiling due to intent restoration (when intent state is FAILED).
117 // TODO: try to release intent resources in IntentManager.
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700118 resourceService.release(intent.key());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200119
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800120 // Check OCh port availability
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700121 // If ports are not available, compilation fails
122 // Else add port to resource reservation list
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800123 Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource();
124 Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource();
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800125 if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700126 log.error("Ports for the intent are not available. Intent: {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700127 throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent);
Marc De Leenheer8c2caac2015-05-28 16:37:33 -0700128 }
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800129 resources.add(srcPortResource);
130 resources.add(dstPortResource);
131
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700132 // Find first path that has the required resources
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700133 Stream<Path> paths = getOpticalPaths(intent);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700134 Optional<Map.Entry<Path, List<OchSignal>>> found = paths
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700135 .map(path ->
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700136 Maps.immutableEntry(path, findFirstAvailableLambda(intent, path)))
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800137 .filter(entry -> !entry.getValue().isEmpty())
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700138 .filter(entry -> convertToResources(entry.getKey(),
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800139 entry.getValue()).stream().allMatch(resourceService::isAvailable))
140 .findFirst();
141
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700142 // Allocate resources and create optical path intent
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800143 if (found.isPresent()) {
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700144 resources.addAll(convertToResources(found.get().getKey(), found.get().getValue()));
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700145
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800146 allocateResources(intent, resources);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700147
Sho SHIMIZU07ade9b2016-01-28 19:38:50 -0800148 OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ);
149 return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal));
150 } else {
Yuta HIGUCHI0164c1c2017-05-04 15:43:55 -0700151 log.error("Unable to find suitable lightpath for intent {}", intent);
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700152 throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent);
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700153 }
weibitf32383b2014-10-22 10:17:31 -0700154 }
155
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700156 /**
157 * Create installable optical path intent.
158 * Only supports fixed grid for now.
159 *
160 * @param parentIntent this intent (used for resource tracking)
161 * @param path the path to use
162 * @param lambda the lambda to use
163 * @return optical path intent
164 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800165 private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) {
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800166 OchSignalType signalType = OchSignalType.FIXED_GRID;
167
168 return OpticalPathIntent.builder()
169 .appId(parentIntent.appId())
Yuta HIGUCHI652f27f2016-10-31 16:54:30 -0700170 .key(parentIntent.key())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800171 .src(parentIntent.getSrc())
172 .dst(parentIntent.getDst())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800173 .path(path)
174 .lambda(lambda)
175 .signalType(signalType)
176 .bidirectional(parentIntent.isBidirectional())
Luca Prete670ac5d2017-02-03 15:55:43 -0800177 .resourceGroup(parentIntent.resourceGroup())
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800178 .build();
179 }
180
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700181 /**
182 * Convert given lambda as discrete resource of all path ports.
183 *
184 * @param path the path
185 * @param lambda the lambda
186 * @return list of discrete resources
187 */
188 private List<Resource> convertToResources(Path path, Collection<OchSignal> lambda) {
189 return path.links().stream()
190 .flatMap(x -> Stream.of(
191 Resources.discrete(x.src().deviceId(), x.src().port()).resource(),
192 Resources.discrete(x.dst().deviceId(), x.dst().port()).resource()
193 ))
194 .flatMap(x -> lambda.stream().map(x::child))
195 .collect(Collectors.toList());
196 }
197
198 /**
199 * Reserve all required resources for this intent.
200 *
201 * @param intent the intent
202 * @param resources list of resources to reserve
203 */
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800204 private void allocateResources(Intent intent, List<Resource> resources) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700205 List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800206 if (allocations.isEmpty()) {
Yuta HIGUCHI65d9d0e2017-05-04 12:44:32 -0700207 log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources);
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700208 if (log.isDebugEnabled()) {
209 log.debug("requested resources:\n\t{}", resources.stream()
210 .map(Resource::toString)
211 .collect(Collectors.joining("\n\t")));
212 }
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700213 throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources);
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800214 }
215 }
216
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700217 /**
218 * Find the first available lambda on the given path by checking all the port resources.
219 *
220 * @param path the path
221 * @return list of consecutive and available OChSignals
222 */
Marc De Leenheeradfeffd2017-06-22 16:05:34 -0700223 private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) {
224 if (intent.ochSignal().isPresent()) {
225 return Collections.singletonList(intent.ochSignal().get());
226 }
227
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700228 Set<OchSignal> lambdas = findCommonLambdas(path);
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700229 if (lambdas.isEmpty()) {
Marc De Leenheer2c305302015-12-07 21:37:44 -0800230 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700231 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700232
Sho SHIMIZU394918d2016-01-28 16:32:10 -0800233 return findFirstLambda(lambdas, slotCount());
234 }
235
Marc De Leenheer2c305302015-12-07 21:37:44 -0800236 /**
237 * Get the number of 12.5 GHz slots required for the path.
238 *
239 * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot),
240 * but in the future can depend on optical reach, line rate, transponder port capabilities, etc.
241 *
242 * @return number of slots
243 */
244 private int slotCount() {
245 return SLOT_COUNT;
246 }
247
Marc De Leenheer03a52b02017-06-05 20:36:23 -0700248 /**
249 * Find common lambdas on all ports that compose the path.
250 *
251 * @param path the path
252 * @return set of common lambdas
253 */
254 private Set<OchSignal> findCommonLambdas(Path path) {
255 return path.links().stream()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800256 .flatMap(x -> Stream.of(
Sho SHIMIZUdd3750c2016-02-01 11:37:04 -0800257 Resources.discrete(x.src().deviceId(), x.src().port()).id(),
258 Resources.discrete(x.dst().deviceId(), x.dst().port()).id()
Sho SHIMIZU44f37612015-11-25 16:23:22 -0800259 ))
Sho SHIMIZU7332fe42016-02-15 14:58:33 -0800260 .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class))
Marc De Leenheer2c305302015-12-07 21:37:44 -0800261 .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x))
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700262 .reduce(Sets::intersection)
263 .orElse(Collections.emptySet());
264 }
265
Marc De Leenheer2c305302015-12-07 21:37:44 -0800266 /**
267 * Returns list of consecutive resources in given set of lambdas.
268 *
269 * @param lambdas list of lambdas
270 * @param count number of consecutive lambdas to return
271 * @return list of consecutive lambdas
272 */
273 private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) {
274 // Sort available lambdas
275 List<OchSignal> lambdaList = new ArrayList<>(lambdas);
276 lambdaList.sort(new DefaultOchSignalComparator());
277
278 // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps)
279 for (int i = 0; i < lambdaList.size() - count; i++) {
280 if (lambdaList.get(i).spacingMultiplier() + 2 * count ==
281 lambdaList.get(i + count).spacingMultiplier()) {
282 return lambdaList.subList(i, i + count);
283 }
284 }
285
286 return Collections.emptyList();
Sho SHIMIZUc25a0082015-10-27 17:06:29 -0700287 }
288
Marc De Leenheer723f5532015-06-03 20:16:17 -0700289 private ConnectPoint staticPort(ConnectPoint connectPoint) {
290 Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port());
291
292 String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT);
293
294 // FIXME: need a better way to match the port
295 if (staticPort != null) {
296 for (Port p : deviceService.getPorts(connectPoint.deviceId())) {
297 if (staticPort.equals(p.number().name())) {
298 return new ConnectPoint(p.element().id(), p.number());
299 }
300 }
301 }
302
303 return null;
304 }
305
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700306 /**
307 * Calculates optical paths in WDM topology.
308 *
309 * @param intent optical connectivity intent
310 * @return set of paths in WDM topology
311 */
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700312 private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) {
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700313 // Route in WDM topology
weibit7e583462014-10-23 10:14:05 -0700314 Topology topology = topologyService.currentTopology();
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700315 LinkWeight weight = new LinkWeight() {
Sho SHIMIZU74626412015-09-11 11:46:27 -0700316
Yuta HIGUCHId95d5902016-06-27 00:18:45 -0700317 @Override
318 public double weight(TopologyEdge edge) {
319 // Disregard inactive or non-optical links
320 if (edge.link().state() == Link.State.INACTIVE) {
321 return -1;
322 }
323 if (edge.link().type() != Link.Type.OPTICAL) {
324 return -1;
325 }
326 // Adhere to static port mappings
327 DeviceId srcDeviceId = edge.link().src().deviceId();
328 if (srcDeviceId.equals(intent.getSrc().deviceId())) {
329 ConnectPoint srcStaticPort = staticPort(intent.getSrc());
330 if (srcStaticPort != null) {
331 return srcStaticPort.equals(edge.link().src()) ? 1 : -1;
332 }
333 }
334 DeviceId dstDeviceId = edge.link().dst().deviceId();
335 if (dstDeviceId.equals(intent.getDst().deviceId())) {
336 ConnectPoint dstStaticPort = staticPort(intent.getDst());
337 if (dstStaticPort != null) {
338 return dstStaticPort.equals(edge.link().dst()) ? 1 : -1;
339 }
340 }
341
342 return 1;
343 }
weibit7e583462014-10-23 10:14:05 -0700344 };
weibitf32383b2014-10-22 10:17:31 -0700345
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700346 ConnectPoint start = intent.getSrc();
347 ConnectPoint end = intent.getDst();
Yuta HIGUCHI02f8d2e2017-04-14 17:00:12 -0700348 Stream<Path> paths = topologyService.getKShortestPaths(topology,
349 start.deviceId(),
350 end.deviceId(),
351 AdapterLinkWeigher.adapt(weight));
352 if (log.isDebugEnabled()) {
353 return paths
354 .map(path -> {
355 // no-op map stage to add debug logging
356 log.debug("Candidate path: {}",
357 path.links().stream()
358 .map(lk -> lk.src() + "-" + lk.dst())
359 .collect(Collectors.toList()));
360 return path;
361 });
362 }
Marc De Leenheer1afa2a02015-05-13 09:18:07 -0700363 return paths;
weibitf32383b2014-10-22 10:17:31 -0700364 }
weibitf32383b2014-10-22 10:17:31 -0700365}