Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 3 | * |
| 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 HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 16 | package org.onosproject.net.optical.intent.impl.compiler; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 17 | |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 20 | import com.google.common.collect.Maps; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Sets; |
Boyuan Yan | 1082238 | 2019-02-22 09:45:42 -0800 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
| 23 | import org.onosproject.net.GridType; |
| 24 | import org.onosproject.net.OchSignal; |
| 25 | import org.onosproject.net.Path; |
| 26 | import org.onosproject.net.ChannelSpacing; |
| 27 | import org.onosproject.net.Port; |
| 28 | import org.onosproject.net.Link; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.net.Annotations; |
| 31 | import org.onosproject.net.AnnotationKeys; |
| 32 | import org.onosproject.net.DefaultAnnotations; |
| 33 | import org.onosproject.net.DefaultPath; |
| 34 | import org.onosproject.net.OchSignalType; |
| 35 | import org.onosproject.net.DefaultOchSignalComparator; |
| 36 | import org.onosproject.net.DefaultLink; |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 37 | import org.onosproject.net.resource.Resource; |
| 38 | import org.onosproject.net.resource.ResourceAllocation; |
| 39 | import org.onosproject.net.resource.ResourceService; |
| 40 | import org.onosproject.net.resource.Resources; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 41 | import org.osgi.service.component.annotations.Activate; |
| 42 | import org.osgi.service.component.annotations.Component; |
| 43 | import org.osgi.service.component.annotations.Deactivate; |
| 44 | import org.osgi.service.component.annotations.Reference; |
| 45 | import org.osgi.service.component.annotations.ReferenceCardinality; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 46 | import org.onlab.graph.ScalarWeight; |
| 47 | import org.onlab.graph.Weight; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 48 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.intent.Intent; |
| 50 | import org.onosproject.net.intent.IntentCompiler; |
| 51 | import org.onosproject.net.intent.IntentExtensionService; |
| 52 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 53 | import org.onosproject.net.intent.OpticalPathIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 54 | import org.onosproject.net.optical.OchPort; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 55 | import org.onosproject.net.provider.ProviderId; |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 56 | import org.onosproject.net.topology.LinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 57 | import org.onosproject.net.topology.Topology; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 58 | import org.onosproject.net.topology.TopologyEdge; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 59 | import org.onosproject.net.topology.TopologyService; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 60 | import org.slf4j.Logger; |
| 61 | import org.slf4j.LoggerFactory; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 62 | |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 63 | import java.util.List; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 64 | import java.util.Map; |
| 65 | import java.util.Optional; |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 66 | import java.util.Collection; |
| 67 | import java.util.LinkedList; |
| 68 | import java.util.ArrayList; |
| 69 | import java.util.Collections; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 70 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 71 | import java.util.stream.Collectors; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 72 | import java.util.stream.IntStream; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 73 | import java.util.stream.Stream; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 74 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 75 | import static com.google.common.base.Preconditions.checkArgument; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 76 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 77 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 78 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 79 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 80 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 81 | @Component(immediate = true) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 83 | |
Ray Milkey | 9c9cde4 | 2018-01-12 14:22:06 -0800 | [diff] [blame] | 84 | private static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 85 | // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent. |
| 86 | private static final int SLOT_COUNT = 4; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 87 | private static final ProviderId PROVIDER_ID = new ProviderId("opticalConnectivityIntent", |
| 88 | "org.onosproject.net.optical.intent"); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 89 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 90 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 91 | protected IntentExtensionService intentManager; |
| 92 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 93 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 94 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 95 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 96 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 97 | protected DeviceService deviceService; |
| 98 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 99 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 100 | protected ResourceService resourceService; |
| 101 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 102 | @Activate |
| 103 | public void activate() { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 104 | deviceService = opticalView(deviceService); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 105 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 106 | } |
| 107 | |
| 108 | @Deactivate |
| 109 | public void deactivate() { |
| 110 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 111 | } |
| 112 | |
| 113 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 114 | public List<Intent> compile(OpticalConnectivityIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 115 | List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 116 | // Check if source and destination are optical OCh ports |
| 117 | ConnectPoint src = intent.getSrc(); |
| 118 | ConnectPoint dst = intent.getDst(); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 119 | 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 Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 122 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 123 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 124 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 125 | // 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 HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 128 | resourceService.release(intent.key()); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 129 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 130 | // Check OCh port availability |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 131 | // If ports are not available, compilation fails |
| 132 | // Else add port to resource reservation list |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 133 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 134 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 135 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 136 | log.error("Ports for the intent are not available. Intent: {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 137 | throw new OpticalIntentCompilationException("Ports for the intent are not available. Intent: " + intent); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 138 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 139 | resources.add(srcPortResource); |
| 140 | resources.add(dstPortResource); |
| 141 | |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 142 | // 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 Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 150 | // Find first path that has the required resources |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 151 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 152 | .map(path -> Maps.immutableEntry(path, findFirstAvailableLambda(intent, path))) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 153 | .filter(entry -> !entry.getValue().isEmpty()) |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 154 | .filter(entry -> convertToResources(entry.getKey(), |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 155 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 156 | .findFirst(); |
| 157 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 158 | // Allocate resources and create optical path intent |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 159 | if (found.isPresent()) { |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 160 | log.debug("Suitable lightpath FOUND for intent {}", intent); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 161 | resources.addAll(convertToResources(found.get().getKey(), found.get().getValue())); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 162 | allocateResources(intent, resources); |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 163 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ); |
| 164 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
| 165 | } else { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 166 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 167 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 168 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 171 | /** |
| 172 | * Create installable optical path intent. |
| 173 | * Only supports fixed grid for now. |
| 174 | * |
| 175 | * @param parentIntent this intent (used for resource tracking) |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 176 | * @param path the path to use |
| 177 | * @param lambda the lambda to use |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 178 | * @return optical path intent |
| 179 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 180 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 181 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
| 182 | |
| 183 | return OpticalPathIntent.builder() |
| 184 | .appId(parentIntent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 185 | .key(parentIntent.key()) |
alessio | 0a0f334 | 2019-10-28 16:58:01 +0100 | [diff] [blame] | 186 | .priority(parentIntent.priority()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 187 | .src(parentIntent.getSrc()) |
| 188 | .dst(parentIntent.getDst()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 189 | .path(path) |
| 190 | .lambda(lambda) |
| 191 | .signalType(signalType) |
| 192 | .bidirectional(parentIntent.isBidirectional()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 193 | .resourceGroup(parentIntent.resourceGroup()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 194 | .build(); |
| 195 | } |
| 196 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 197 | /** |
| 198 | * Convert given lambda as discrete resource of all path ports. |
| 199 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 200 | * @param path the path |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 201 | * @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( |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 207 | 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 Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 211 | )) |
| 212 | .flatMap(x -> lambda.stream().map(x::child)) |
| 213 | .collect(Collectors.toList()); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Reserve all required resources for this intent. |
| 218 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 219 | * @param intent the intent |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 220 | * @param resources list of resources to reserve |
| 221 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 222 | private void allocateResources(Intent intent, List<Resource> resources) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 223 | List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 224 | if (allocations.isEmpty()) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 225 | log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 226 | if (log.isDebugEnabled()) { |
| 227 | log.debug("requested resources:\n\t{}", resources.stream() |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 228 | .map(Resource::toString) |
| 229 | .collect(Collectors.joining("\n\t"))); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 230 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 231 | throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 235 | /** |
| 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 Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 241 | private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) { |
| 242 | if (intent.ochSignal().isPresent()) { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 243 | //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 Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 264 | Set<OchSignal> lambdas = findCommonLambdas(path); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 265 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 266 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 267 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 268 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 269 | return findFirstLambda(lambdas, slotCount()); |
| 270 | } |
| 271 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 272 | /** |
| 273 | * Get the number of 12.5 GHz slots required for the path. |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 274 | * <p> |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 275 | * 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 Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 284 | /** |
| 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) { |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 291 | |
| 292 | Set<OchSignal> ochSignals = path.links().stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 293 | .flatMap(x -> Stream.of( |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 294 | 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 SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 298 | )) |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 299 | .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class)) |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 300 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 301 | .reduce(Sets::intersection) |
| 302 | .orElse(Collections.emptySet()); |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 303 | |
| 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 SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 313 | /** |
| 314 | * Returns list of consecutive resources in given set of lambdas. |
| 315 | * |
| 316 | * @param lambdas list of lambdas |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 317 | * @param count number of consecutive lambdas to return |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 318 | * @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 Campanella | e4b2c68 | 2019-08-23 15:42:55 +0200 | [diff] [blame] | 324 | //Means there is only exactly one set of lambdas available |
| 325 | if (lambdaList.size() == count) { |
| 326 | return lambdaList; |
| 327 | } |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 328 | // 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 SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 339 | 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 Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 356 | /** |
| 357 | * Calculates optical paths in WDM topology. |
| 358 | * |
| 359 | * @param intent optical connectivity intent |
| 360 | * @return set of paths in WDM topology |
| 361 | */ |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 362 | private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 363 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 364 | Topology topology = topologyService.currentTopology(); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 365 | //TODO: refactor with LinkWeigher class Implementation |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 366 | LinkWeigher weight = new LinkWeigher() { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 367 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 368 | @Override |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 369 | 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 | |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 378 | /** |
| 379 | * |
| 380 | * @param edge edge to be weighed |
| 381 | * @return the metric retrieved from the annotations otherwise 1 |
| 382 | */ |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 383 | @Override |
| 384 | public Weight weight(TopologyEdge edge) { |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 385 | |
| 386 | log.debug("Link {} metric {}", edge.link(), edge.link().annotations().value("metric")); |
| 387 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 388 | // Disregard inactive or non-optical links |
| 389 | if (edge.link().state() == Link.State.INACTIVE) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 390 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 391 | } |
| 392 | if (edge.link().type() != Link.Type.OPTICAL) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 393 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 394 | } |
| 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 Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 400 | return ScalarWeight.toWeight(srcStaticPort.equals(edge.link().src()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 401 | } |
| 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 Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 407 | return ScalarWeight.toWeight(dstStaticPort.equals(edge.link().dst()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Boyuan Yan | 1082238 | 2019-02-22 09:45:42 -0800 | [diff] [blame] | 411 | Annotations annotations = edge.link().annotations(); |
Jan Kundrát | 12eb26a | 2019-11-08 10:47:10 +0100 | [diff] [blame] | 412 | if (annotations != null && |
| 413 | annotations.value("metric") != null && !annotations.value("metric").isEmpty()) { |
Boyuan Yan | 1082238 | 2019-02-22 09:45:42 -0800 | [diff] [blame] | 414 | double metric = Double.parseDouble(annotations.value("metric")); |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 415 | return ScalarWeight.toWeight(metric); |
| 416 | } else { |
| 417 | return ScalarWeight.toWeight(1); |
| 418 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 419 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 420 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 421 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 422 | ConnectPoint start = intent.getSrc(); |
| 423 | ConnectPoint end = intent.getDst(); |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 424 | |
| 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 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 442 | //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 HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 444 | Stream<Path> paths = topologyService.getKShortestPaths(topology, |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 445 | start.deviceId(), |
| 446 | end.deviceId(), |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 447 | weight) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 448 | .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 HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 450 | if (log.isDebugEnabled()) { |
| 451 | return paths |
| 452 | .map(path -> { |
| 453 | // no-op map stage to add debug logging |
| 454 | log.debug("Candidate path: {}", |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 455 | path.links().stream() |
| 456 | .map(lk -> lk.src() + "-" + lk.dst()) |
| 457 | .collect(Collectors.toList())); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 458 | return path; |
| 459 | }); |
| 460 | } |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 461 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 462 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 463 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 464 | } |