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. |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 86 | private static final int DEFAULT_SLOT_GRANULARITY = 4; |
| 87 | private static final GridType DEFAULT_GRID_TYPE = GridType.DWDM; |
| 88 | private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ; |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 89 | private static final ProviderId PROVIDER_ID = new ProviderId("opticalConnectivityIntent", |
| 90 | "org.onosproject.net.optical.intent"); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 91 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 92 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 93 | protected IntentExtensionService intentManager; |
| 94 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 96 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 97 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 99 | protected DeviceService deviceService; |
| 100 | |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 102 | protected ResourceService resourceService; |
| 103 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 104 | @Activate |
| 105 | public void activate() { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 106 | deviceService = opticalView(deviceService); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 107 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 108 | } |
| 109 | |
| 110 | @Deactivate |
| 111 | public void deactivate() { |
| 112 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 113 | } |
| 114 | |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 115 | /** |
| 116 | * Path computation and spectrum assignment. |
| 117 | * Supports fixed and flex grids. |
| 118 | * |
| 119 | * Path and signal can be provided within the intent. |
| 120 | * If not provided they are computed in this function. |
| 121 | * If signal is not provided a default channel is allocated with width 50 GHz, on the 50 GHz spacing. |
| 122 | * |
| 123 | * @param intent this intent (used for resource tracking) |
| 124 | * @param installable intents |
| 125 | * @return list of optical path intents (one per direction) |
| 126 | */ |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 127 | @Override |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 128 | public List<Intent> compile(OpticalConnectivityIntent intent, List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 129 | // Check if source and destination are optical OCh ports |
| 130 | ConnectPoint src = intent.getSrc(); |
| 131 | ConnectPoint dst = intent.getDst(); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 132 | checkArgument(deviceService.getPort(src.deviceId(), src.port()) instanceof OchPort); |
| 133 | checkArgument(deviceService.getPort(dst.deviceId(), dst.port()) instanceof OchPort); |
| 134 | List<Resource> resources = new LinkedList<>(); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 135 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 136 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 137 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 138 | // Release of intent resources here is only a temporary solution for handling the |
| 139 | // case of recompiling due to intent restoration (when intent state is FAILED). |
| 140 | // TODO: try to release intent resources in IntentManager. |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 141 | resourceService.release(intent.key()); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 142 | |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 143 | // Check OCH src and dst port availability |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 144 | // If ports are not available, compilation fails |
| 145 | // Else add port to resource reservation list |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 146 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 147 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 148 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 149 | log.error("Ports for the intent are not available. Intent: {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 150 | 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] | 151 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 152 | resources.add(srcPortResource); |
| 153 | resources.add(dstPortResource); |
| 154 | |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 155 | // If there is a valid suggestedPath, use this path without further checking |
| 156 | // Otherwise trigger path computation |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 157 | Stream<Path> paths; |
| 158 | if (intent.suggestedPath().isPresent()) { |
| 159 | paths = Stream.of(intent.suggestedPath().get()); |
| 160 | } else { |
| 161 | paths = getOpticalPaths(intent); |
| 162 | } |
| 163 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 164 | // Find first path that has the required resources |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 165 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 166 | .map(path -> Maps.immutableEntry(path, findFirstAvailableLambda(intent, path))) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 167 | .filter(entry -> !entry.getValue().isEmpty()) |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 168 | .filter(entry -> convertToResources(entry.getKey(), |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 169 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 170 | .findFirst(); |
| 171 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 172 | // Allocate resources and create optical path intent |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 173 | if (found.isPresent()) { |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 174 | log.debug("Suitable path and lambdas FOUND for intent {}", intent); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 175 | resources.addAll(convertToResources(found.get().getKey(), found.get().getValue())); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 176 | allocateResources(intent, resources); |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 177 | |
| 178 | if (intent.ochSignal().isPresent()) { |
| 179 | if (intent.ochSignal().get().gridType() == GridType.FLEX) { |
| 180 | return ImmutableList.of(createIntent(intent, found.get().getKey(), intent.ochSignal().get())); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), DEFAULT_CHANNEL_SPACING); |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 185 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 186 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 187 | } else { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 188 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 189 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 190 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 193 | /** |
| 194 | * Create installable optical path intent. |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 195 | * Supports fixed and flex grids. |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 196 | * |
| 197 | * @param parentIntent this intent (used for resource tracking) |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 198 | * @param path the path to use |
| 199 | * @param lambda the lambda to use |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 200 | * @return optical path intent |
| 201 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 202 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 203 | OchSignalType signalType; |
| 204 | if (lambda.gridType().equals(GridType.FLEX)) { |
| 205 | signalType = OchSignalType.FLEX_GRID; |
| 206 | } else { |
| 207 | signalType = OchSignalType.FIXED_GRID; |
| 208 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 209 | |
| 210 | return OpticalPathIntent.builder() |
| 211 | .appId(parentIntent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 212 | .key(parentIntent.key()) |
alessio | 0a0f334 | 2019-10-28 16:58:01 +0100 | [diff] [blame] | 213 | .priority(parentIntent.priority()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 214 | .src(parentIntent.getSrc()) |
| 215 | .dst(parentIntent.getDst()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 216 | .path(path) |
| 217 | .lambda(lambda) |
| 218 | .signalType(signalType) |
| 219 | .bidirectional(parentIntent.isBidirectional()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 220 | .resourceGroup(parentIntent.resourceGroup()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 221 | .build(); |
| 222 | } |
| 223 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 224 | /** |
| 225 | * Convert given lambda as discrete resource of all path ports. |
| 226 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 227 | * @param path the path |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 228 | * @param lambda the lambda |
| 229 | * @return list of discrete resources |
| 230 | */ |
| 231 | private List<Resource> convertToResources(Path path, Collection<OchSignal> lambda) { |
| 232 | return path.links().stream() |
| 233 | .flatMap(x -> Stream.of( |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 234 | Resources.discrete(x.src().deviceId(), |
| 235 | deviceService.getPort(x.src().deviceId(), x.src().port()).number()).resource(), |
| 236 | Resources.discrete(x.dst().deviceId(), |
| 237 | deviceService.getPort(x.dst().deviceId(), x.dst().port()).number()).resource() |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 238 | )) |
| 239 | .flatMap(x -> lambda.stream().map(x::child)) |
| 240 | .collect(Collectors.toList()); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Reserve all required resources for this intent. |
| 245 | * |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 246 | * @param intent the intent |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 247 | * @param resources list of resources to reserve |
| 248 | */ |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 249 | private void allocateResources(Intent intent, List<Resource> resources) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 250 | List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 251 | if (allocations.isEmpty()) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 252 | log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 253 | if (log.isDebugEnabled()) { |
| 254 | log.debug("requested resources:\n\t{}", resources.stream() |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 255 | .map(Resource::toString) |
| 256 | .collect(Collectors.joining("\n\t"))); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 257 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 258 | throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 262 | /** |
| 263 | * Find the first available lambda on the given path by checking all the port resources. |
| 264 | * |
| 265 | * @param path the path |
| 266 | * @return list of consecutive and available OChSignals |
| 267 | */ |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 268 | private List<OchSignal> findFirstAvailableLambda(OpticalConnectivityIntent intent, Path path) { |
| 269 | if (intent.ochSignal().isPresent()) { |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 270 | //create lambdas w.r.t. slotGanularity/slotWidth |
| 271 | OchSignal ochSignal = intent.ochSignal().get(); |
| 272 | if (ochSignal.gridType() == GridType.FLEX) { |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 273 | int startMultiplier = (int) (1 - ochSignal.slotGranularity() + ochSignal.spacingMultiplier()); |
| 274 | |
| 275 | List<OchSignal> channels = IntStream.range(0, ochSignal.slotGranularity()) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 276 | .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x))) |
| 277 | .collect(Collectors.toList()); |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 278 | |
| 279 | return channels; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 280 | } else if (ochSignal.gridType() == GridType.DWDM) { |
| 281 | int startMultiplier = (int) (1 - ochSignal.slotGranularity() + |
| 282 | ochSignal.spacingMultiplier() * ochSignal.channelSpacing().frequency().asHz() / |
| 283 | ChannelSpacing.CHL_6P25GHZ.frequency().asHz()); |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 284 | |
| 285 | List<OchSignal> channels = IntStream.range(0, ochSignal.slotGranularity()) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 286 | .mapToObj(x -> OchSignal.newFlexGridSlot(startMultiplier + (2 * x))) |
| 287 | .collect(Collectors.toList()); |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 288 | |
| 289 | return channels; |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 290 | } |
| 291 | //TODO: add support for other gridTypes |
| 292 | log.error("Grid type: {} not supported for user defined signal intents", ochSignal.gridType()); |
| 293 | return Collections.emptyList(); |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 296 | Set<OchSignal> lambdas = findCommonLambdas(path); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 297 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 298 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 299 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 300 | |
alessio | 13a3f68 | 2023-02-09 15:39:31 +0100 | [diff] [blame] | 301 | return findFirstLambda(lambdas, DEFAULT_SLOT_GRANULARITY); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 302 | } |
| 303 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Find common lambdas on all ports that compose the path. |
| 306 | * |
| 307 | * @param path the path |
| 308 | * @return set of common lambdas |
| 309 | */ |
| 310 | private Set<OchSignal> findCommonLambdas(Path path) { |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 311 | |
| 312 | Set<OchSignal> ochSignals = path.links().stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 313 | .flatMap(x -> Stream.of( |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 314 | Resources.discrete(x.src().deviceId(), |
| 315 | deviceService.getPort(x.src().deviceId(), x.src().port()).number()).id(), |
| 316 | Resources.discrete(x.dst().deviceId(), |
| 317 | deviceService.getPort(x.dst().deviceId(), x.dst().port()).number()).id() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 318 | )) |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 319 | .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class)) |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 320 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 321 | .reduce(Sets::intersection) |
| 322 | .orElse(Collections.emptySet()); |
alessio | f3beff2 | 2020-07-17 17:17:11 +0200 | [diff] [blame] | 323 | |
| 324 | if (ochSignals.isEmpty()) { |
| 325 | log.warn("Common lambdas not found"); |
| 326 | } else { |
| 327 | log.debug("Common lambdas found {}", ochSignals); |
| 328 | } |
| 329 | |
| 330 | return ochSignals; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 333 | /** |
| 334 | * Returns list of consecutive resources in given set of lambdas. |
| 335 | * |
| 336 | * @param lambdas list of lambdas |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 337 | * @param count number of consecutive lambdas to return |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 338 | * @return list of consecutive lambdas |
| 339 | */ |
| 340 | private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) { |
| 341 | // Sort available lambdas |
| 342 | List<OchSignal> lambdaList = new ArrayList<>(lambdas); |
| 343 | lambdaList.sort(new DefaultOchSignalComparator()); |
Andrea Campanella | e4b2c68 | 2019-08-23 15:42:55 +0200 | [diff] [blame] | 344 | //Means there is only exactly one set of lambdas available |
| 345 | if (lambdaList.size() == count) { |
| 346 | return lambdaList; |
| 347 | } |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 348 | // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps) |
| 349 | for (int i = 0; i < lambdaList.size() - count; i++) { |
| 350 | if (lambdaList.get(i).spacingMultiplier() + 2 * count == |
| 351 | lambdaList.get(i + count).spacingMultiplier()) { |
| 352 | return lambdaList.subList(i, i + count); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 359 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 360 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 361 | |
| 362 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 363 | |
| 364 | // FIXME: need a better way to match the port |
| 365 | if (staticPort != null) { |
| 366 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 367 | if (staticPort.equals(p.number().name())) { |
| 368 | return new ConnectPoint(p.element().id(), p.number()); |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | return null; |
| 374 | } |
| 375 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 376 | /** |
| 377 | * Calculates optical paths in WDM topology. |
| 378 | * |
| 379 | * @param intent optical connectivity intent |
| 380 | * @return set of paths in WDM topology |
| 381 | */ |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 382 | private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 383 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 384 | Topology topology = topologyService.currentTopology(); |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 385 | //TODO: refactor with LinkWeigher class Implementation |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 386 | LinkWeigher weight = new LinkWeigher() { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 387 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 388 | @Override |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 389 | public Weight getInitialWeight() { |
| 390 | return ScalarWeight.toWeight(0.0); |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public Weight getNonViableWeight() { |
| 395 | return ScalarWeight.NON_VIABLE_WEIGHT; |
| 396 | } |
| 397 | |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 398 | /** |
| 399 | * |
| 400 | * @param edge edge to be weighed |
| 401 | * @return the metric retrieved from the annotations otherwise 1 |
| 402 | */ |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 403 | @Override |
| 404 | public Weight weight(TopologyEdge edge) { |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 405 | |
| 406 | log.debug("Link {} metric {}", edge.link(), edge.link().annotations().value("metric")); |
| 407 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 408 | // Disregard inactive or non-optical links |
| 409 | if (edge.link().state() == Link.State.INACTIVE) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 410 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 411 | } |
| 412 | if (edge.link().type() != Link.Type.OPTICAL) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 413 | return ScalarWeight.toWeight(-1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 414 | } |
| 415 | // Adhere to static port mappings |
| 416 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 417 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 418 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 419 | if (srcStaticPort != null) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 420 | return ScalarWeight.toWeight(srcStaticPort.equals(edge.link().src()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 424 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 425 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 426 | if (dstStaticPort != null) { |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 427 | return ScalarWeight.toWeight(dstStaticPort.equals(edge.link().dst()) ? 1 : -1); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
Boyuan Yan | 1082238 | 2019-02-22 09:45:42 -0800 | [diff] [blame] | 431 | Annotations annotations = edge.link().annotations(); |
Jan Kundrát | 12eb26a | 2019-11-08 10:47:10 +0100 | [diff] [blame] | 432 | if (annotations != null && |
| 433 | annotations.value("metric") != null && !annotations.value("metric").isEmpty()) { |
Boyuan Yan | 1082238 | 2019-02-22 09:45:42 -0800 | [diff] [blame] | 434 | double metric = Double.parseDouble(annotations.value("metric")); |
alessio | 3039bdb | 2018-11-29 14:12:32 +0100 | [diff] [blame] | 435 | return ScalarWeight.toWeight(metric); |
| 436 | } else { |
| 437 | return ScalarWeight.toWeight(1); |
| 438 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 439 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 440 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 441 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 442 | ConnectPoint start = intent.getSrc(); |
| 443 | ConnectPoint end = intent.getDst(); |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 444 | |
| 445 | // 0 hop case |
| 446 | if (start.deviceId().equals(end.deviceId())) { |
| 447 | log.debug("install optical intent for 0 hop i.e srcDeviceId=dstDeviceId"); |
| 448 | DefaultLink defaultLink = DefaultLink.builder() |
| 449 | .providerId(PROVIDER_ID) |
| 450 | .src(start) |
| 451 | .dst(end) |
| 452 | .state(Link.State.ACTIVE) |
| 453 | .type(Link.Type.DIRECT) |
| 454 | .isExpected(true) |
| 455 | .build(); |
| 456 | List<Link> links = ImmutableList.<Link>builder().add(defaultLink).build(); |
| 457 | Annotations annotations = DefaultAnnotations.builder().build(); |
| 458 | DefaultPath defaultPath = new DefaultPath(PROVIDER_ID, links, null, annotations); |
| 459 | return ImmutableList.<Path>builder().add(defaultPath).build().stream(); |
| 460 | } |
| 461 | |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 462 | //head link's src port should be same as intent src port and tail link dst port |
| 463 | //should be same as intent dst port in the path. |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 464 | Stream<Path> paths = topologyService.getKShortestPaths(topology, |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 465 | start.deviceId(), |
| 466 | end.deviceId(), |
Ray Milkey | 7483e1b | 2018-02-07 15:43:01 -0800 | [diff] [blame] | 467 | weight) |
fahadnaeemkhan | ffc917f | 2017-10-03 14:04:46 -0700 | [diff] [blame] | 468 | .filter(p -> p.links().get(0).src().port().equals(start.port()) && |
| 469 | p.links().get(p.links().size() - 1).dst().port().equals(end.port())); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 470 | if (log.isDebugEnabled()) { |
| 471 | return paths |
| 472 | .map(path -> { |
| 473 | // no-op map stage to add debug logging |
| 474 | log.debug("Candidate path: {}", |
fahadnaeemkhan | 0e1b4d9 | 2018-02-08 16:40:08 -0800 | [diff] [blame] | 475 | path.links().stream() |
| 476 | .map(lk -> lk.src() + "-" + lk.dst()) |
| 477 | .collect(Collectors.toList())); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 478 | return path; |
| 479 | }); |
| 480 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 481 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 482 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 483 | } |