Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0a4e674 | 2016-09-15 23:03:10 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
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; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Component; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 24 | import org.apache.felix.scr.annotations.Deactivate; |
| 25 | import org.apache.felix.scr.annotations.Reference; |
| 26 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 27 | import org.onosproject.net.AnnotationKeys; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 28 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DefaultOchSignalComparator; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import org.onosproject.net.Link; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 33 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 34 | import org.onosproject.net.OchSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 36 | import org.onosproject.net.Port; |
| 37 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 38 | import org.onosproject.net.intent.Intent; |
| 39 | import org.onosproject.net.intent.IntentCompiler; |
| 40 | import org.onosproject.net.intent.IntentExtensionService; |
| 41 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 42 | import org.onosproject.net.intent.OpticalPathIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 43 | import org.onosproject.net.optical.OchPort; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 44 | import org.onosproject.net.resource.Resource; |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 45 | import org.onosproject.net.resource.ResourceAllocation; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.resource.ResourceService; |
| 47 | import org.onosproject.net.resource.Resources; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 48 | import org.onosproject.net.topology.AdapterLinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 49 | import org.onosproject.net.topology.LinkWeight; |
| 50 | import org.onosproject.net.topology.Topology; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 51 | import org.onosproject.net.topology.TopologyEdge; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 52 | import org.onosproject.net.topology.TopologyService; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | import org.slf4j.LoggerFactory; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 55 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 56 | import java.util.ArrayList; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 57 | import java.util.Collection; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 59 | import java.util.LinkedList; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 60 | import java.util.List; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 61 | import java.util.Map; |
| 62 | import java.util.Optional; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 63 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 64 | import java.util.stream.Collectors; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 65 | import java.util.stream.Stream; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 66 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 67 | import static com.google.common.base.Preconditions.checkArgument; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 68 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 69 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 70 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 71 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 72 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 73 | @Component(immediate = true) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 74 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 75 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 76 | protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 77 | // 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 Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 79 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected IntentExtensionService intentManager; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 84 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 85 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 87 | protected DeviceService deviceService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 90 | protected ResourceService resourceService; |
| 91 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 94 | deviceService = opticalView(deviceService); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 95 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 96 | } |
| 97 | |
| 98 | @Deactivate |
| 99 | public void deactivate() { |
| 100 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 101 | } |
| 102 | |
| 103 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 104 | public List<Intent> compile(OpticalConnectivityIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 105 | List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 106 | // Check if source and destination are optical OCh ports |
| 107 | ConnectPoint src = intent.getSrc(); |
| 108 | ConnectPoint dst = intent.getDst(); |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 109 | 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 Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 112 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 113 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 114 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 115 | // 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 HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 118 | resourceService.release(intent.key()); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 119 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 120 | // Check OCh port availability |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 121 | // If ports are not available, compilation fails |
| 122 | // Else add port to resource reservation list |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 123 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 124 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 125 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 126 | log.error("Ports for the intent are not available. Intent: {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 127 | 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] | 128 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 129 | resources.add(srcPortResource); |
| 130 | resources.add(dstPortResource); |
| 131 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 132 | // Find first path that has the required resources |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 133 | Stream<Path> paths = getOpticalPaths(intent); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 134 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 135 | .map(path -> |
| 136 | Maps.immutableEntry(path, findFirstAvailableLambda(path))) |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 137 | .filter(entry -> !entry.getValue().isEmpty()) |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 138 | .filter(entry -> convertToResources(entry.getKey(), |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 139 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
| 140 | .findFirst(); |
| 141 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 142 | // Allocate resources and create optical path intent |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 143 | if (found.isPresent()) { |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 144 | resources.addAll(convertToResources(found.get().getKey(), found.get().getValue())); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 145 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 146 | allocateResources(intent, resources); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 147 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 148 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ); |
| 149 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
| 150 | } else { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 151 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 152 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 153 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 156 | /** |
| 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 SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 165 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 166 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
| 167 | |
| 168 | return OpticalPathIntent.builder() |
| 169 | .appId(parentIntent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 170 | .key(parentIntent.key()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 171 | .src(parentIntent.getSrc()) |
| 172 | .dst(parentIntent.getDst()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 173 | .path(path) |
| 174 | .lambda(lambda) |
| 175 | .signalType(signalType) |
| 176 | .bidirectional(parentIntent.isBidirectional()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 177 | .resourceGroup(parentIntent.resourceGroup()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 178 | .build(); |
| 179 | } |
| 180 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 181 | /** |
| 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 SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 204 | private void allocateResources(Intent intent, List<Resource> resources) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 205 | List<ResourceAllocation> allocations = resourceService.allocate(intent.key(), resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 206 | if (allocations.isEmpty()) { |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 207 | log.error("Resource allocation for {} failed (resource request: {})", intent.key(), resources); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 208 | if (log.isDebugEnabled()) { |
| 209 | log.debug("requested resources:\n\t{}", resources.stream() |
| 210 | .map(Resource::toString) |
| 211 | .collect(Collectors.joining("\n\t"))); |
| 212 | } |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 213 | throw new OpticalIntentCompilationException("Unable to allocate resources: " + resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 217 | /** |
| 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 | */ |
| 223 | private List<OchSignal> findFirstAvailableLambda(Path path) { |
| 224 | Set<OchSignal> lambdas = findCommonLambdas(path); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 225 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 226 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 227 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 228 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 229 | return findFirstLambda(lambdas, slotCount()); |
| 230 | } |
| 231 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 232 | /** |
| 233 | * Get the number of 12.5 GHz slots required for the path. |
| 234 | * |
| 235 | * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot), |
| 236 | * but in the future can depend on optical reach, line rate, transponder port capabilities, etc. |
| 237 | * |
| 238 | * @return number of slots |
| 239 | */ |
| 240 | private int slotCount() { |
| 241 | return SLOT_COUNT; |
| 242 | } |
| 243 | |
Marc De Leenheer | 03a52b0 | 2017-06-05 20:36:23 -0700 | [diff] [blame] | 244 | /** |
| 245 | * Find common lambdas on all ports that compose the path. |
| 246 | * |
| 247 | * @param path the path |
| 248 | * @return set of common lambdas |
| 249 | */ |
| 250 | private Set<OchSignal> findCommonLambdas(Path path) { |
| 251 | return path.links().stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 252 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 253 | Resources.discrete(x.src().deviceId(), x.src().port()).id(), |
| 254 | Resources.discrete(x.dst().deviceId(), x.dst().port()).id() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 255 | )) |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 256 | .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class)) |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 257 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 258 | .reduce(Sets::intersection) |
| 259 | .orElse(Collections.emptySet()); |
| 260 | } |
| 261 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 262 | /** |
| 263 | * Returns list of consecutive resources in given set of lambdas. |
| 264 | * |
| 265 | * @param lambdas list of lambdas |
| 266 | * @param count number of consecutive lambdas to return |
| 267 | * @return list of consecutive lambdas |
| 268 | */ |
| 269 | private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) { |
| 270 | // Sort available lambdas |
| 271 | List<OchSignal> lambdaList = new ArrayList<>(lambdas); |
| 272 | lambdaList.sort(new DefaultOchSignalComparator()); |
| 273 | |
| 274 | // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps) |
| 275 | for (int i = 0; i < lambdaList.size() - count; i++) { |
| 276 | if (lambdaList.get(i).spacingMultiplier() + 2 * count == |
| 277 | lambdaList.get(i + count).spacingMultiplier()) { |
| 278 | return lambdaList.subList(i, i + count); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 283 | } |
| 284 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 285 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 286 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 287 | |
| 288 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 289 | |
| 290 | // FIXME: need a better way to match the port |
| 291 | if (staticPort != null) { |
| 292 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 293 | if (staticPort.equals(p.number().name())) { |
| 294 | return new ConnectPoint(p.element().id(), p.number()); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return null; |
| 300 | } |
| 301 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 302 | /** |
| 303 | * Calculates optical paths in WDM topology. |
| 304 | * |
| 305 | * @param intent optical connectivity intent |
| 306 | * @return set of paths in WDM topology |
| 307 | */ |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 308 | private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 309 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 310 | Topology topology = topologyService.currentTopology(); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 311 | LinkWeight weight = new LinkWeight() { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 312 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 313 | @Override |
| 314 | public double weight(TopologyEdge edge) { |
| 315 | // Disregard inactive or non-optical links |
| 316 | if (edge.link().state() == Link.State.INACTIVE) { |
| 317 | return -1; |
| 318 | } |
| 319 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 320 | return -1; |
| 321 | } |
| 322 | // Adhere to static port mappings |
| 323 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 324 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 325 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 326 | if (srcStaticPort != null) { |
| 327 | return srcStaticPort.equals(edge.link().src()) ? 1 : -1; |
| 328 | } |
| 329 | } |
| 330 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 331 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 332 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 333 | if (dstStaticPort != null) { |
| 334 | return dstStaticPort.equals(edge.link().dst()) ? 1 : -1; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return 1; |
| 339 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 340 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 341 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 342 | ConnectPoint start = intent.getSrc(); |
| 343 | ConnectPoint end = intent.getDst(); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 344 | Stream<Path> paths = topologyService.getKShortestPaths(topology, |
| 345 | start.deviceId(), |
| 346 | end.deviceId(), |
| 347 | AdapterLinkWeigher.adapt(weight)); |
| 348 | if (log.isDebugEnabled()) { |
| 349 | return paths |
| 350 | .map(path -> { |
| 351 | // no-op map stage to add debug logging |
| 352 | log.debug("Candidate path: {}", |
| 353 | path.links().stream() |
| 354 | .map(lk -> lk.src() + "-" + lk.dst()) |
| 355 | .collect(Collectors.toList())); |
| 356 | return path; |
| 357 | }); |
| 358 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 359 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 360 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 361 | } |