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 | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 27 | import org.onlab.util.Frequency; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 28 | import org.onosproject.net.AnnotationKeys; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 31 | import org.onosproject.net.DefaultOchSignalComparator; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 33 | import org.onosproject.net.Link; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 34 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 35 | import org.onosproject.net.OchSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.Intent; |
| 40 | import org.onosproject.net.intent.IntentCompiler; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 43 | import org.onosproject.net.intent.OpticalPathIntent; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 44 | import org.onosproject.net.optical.OchPort; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 45 | import org.onosproject.net.resource.ResourceAllocation; |
| 46 | import org.onosproject.net.resource.Resource; |
| 47 | import org.onosproject.net.resource.ResourceService; |
| 48 | import org.onosproject.net.resource.Resources; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 49 | import org.onosproject.net.topology.AdapterLinkWeigher; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 50 | import org.onosproject.net.topology.LinkWeight; |
| 51 | import org.onosproject.net.topology.Topology; |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 52 | import org.onosproject.net.topology.TopologyEdge; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.topology.TopologyService; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 54 | import org.slf4j.Logger; |
| 55 | import org.slf4j.LoggerFactory; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 56 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 57 | import java.util.ArrayList; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 58 | import java.util.Collection; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 60 | import java.util.LinkedList; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 61 | import java.util.List; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 62 | import java.util.Map; |
| 63 | import java.util.Optional; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 64 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 65 | import java.util.stream.Collectors; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 66 | import java.util.stream.Stream; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 67 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 68 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 69 | import static org.onosproject.net.OchSignal.toFlexGrid; |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 70 | import static org.onosproject.net.optical.device.OpticalDeviceServiceView.opticalView; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 71 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 72 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 73 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 74 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 75 | @Component(immediate = true) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 76 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 77 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 78 | protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 79 | // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent. |
| 80 | private static final int SLOT_COUNT = 4; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 81 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected IntentExtensionService intentManager; |
| 84 | |
| 85 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 86 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 87 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected DeviceService deviceService; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 92 | protected ResourceService resourceService; |
| 93 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 94 | @Activate |
| 95 | public void activate() { |
HIGUCHI Yuta | 34a3f69 | 2016-01-09 21:08:57 -0800 | [diff] [blame] | 96 | deviceService = opticalView(deviceService); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 97 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 98 | } |
| 99 | |
| 100 | @Deactivate |
| 101 | public void deactivate() { |
| 102 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 103 | } |
| 104 | |
| 105 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 106 | public List<Intent> compile(OpticalConnectivityIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 107 | List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 108 | // Check if source and destination are optical OCh ports |
| 109 | ConnectPoint src = intent.getSrc(); |
| 110 | ConnectPoint dst = intent.getDst(); |
| 111 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 112 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
| 113 | checkArgument(srcPort instanceof OchPort); |
| 114 | checkArgument(dstPort instanceof OchPort); |
| 115 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 116 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 117 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 118 | // Release of intent resources here is only a temporary solution for handling the |
| 119 | // case of recompiling due to intent restoration (when intent state is FAILED). |
| 120 | // TODO: try to release intent resources in IntentManager. |
Yuta HIGUCHI | 65d9d0e | 2017-05-04 12:44:32 -0700 | [diff] [blame] | 121 | resourceService.release(intent.key()); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 122 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 123 | // Check OCh port availability |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 124 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 125 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 126 | // If ports are not available, compilation fails |
| 127 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 128 | log.error("Ports for the intent are not available. Intent: {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 129 | 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] | 130 | } |
| 131 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 132 | List<Resource> resources = new ArrayList<>(); |
| 133 | resources.add(srcPortResource); |
| 134 | resources.add(dstPortResource); |
| 135 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 136 | // Calculate available light paths |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 137 | Stream<Path> paths = getOpticalPaths(intent); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 138 | |
Sho SHIMIZU | d978d9a | 2016-01-28 08:55:49 -0800 | [diff] [blame] | 139 | // Static or dynamic lambda allocation |
| 140 | String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA); |
| 141 | OchPort srcOchPort = (OchPort) srcPort; |
| 142 | OchPort dstOchPort = (OchPort) dstPort; |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 143 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 144 | // FIXME: need to actually reserve the lambda for static lambda's |
| 145 | // static lambda case: early return |
| 146 | if (staticLambda != null) { |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 147 | |
| 148 | OchSignal lambda = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)), |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 149 | srcOchPort.lambda().channelSpacing(), |
| 150 | srcOchPort.lambda().slotGranularity()); |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 151 | log.debug("Using statically assigned lambda : {}", lambda); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 152 | |
| 153 | List<Resource> res = new ArrayList<>(); |
| 154 | |
| 155 | Path satPath = paths.filter(path -> { |
| 156 | // FIXME trimming both ends (staticLambda assigned ports) |
| 157 | // Proper fix is to let device advertise static lambda as resource. |
| 158 | LinkedList<Link> links = new LinkedList<>(path.links()); |
| 159 | links.pollFirst(); |
| 160 | links.pollLast(); |
| 161 | |
| 162 | List<Resource> r = convertToResources(links, toFlexGrid(lambda)); |
| 163 | if (r.stream().allMatch(resourceService::isAvailable)) { |
| 164 | // FIXME bad practice to have side-effect during stream |
| 165 | res.addAll(r); |
| 166 | return true; |
| 167 | } |
| 168 | return false; |
| 169 | }).findFirst().orElse(null); |
| 170 | |
| 171 | if (satPath == null) { |
| 172 | // FIXME doing something very wrong to preserve old behavior |
| 173 | // as a fallback |
| 174 | log.warn("No feasible path between {}-{} using {}", |
| 175 | src, dst, lambda); |
| 176 | |
| 177 | satPath = getOpticalPaths(intent).iterator().next(); |
| 178 | if (satPath == null) { |
| 179 | throw new OpticalIntentCompilationException( |
| 180 | "Unable to find suitable lightpath for intent " + intent); |
| 181 | } |
| 182 | return ImmutableList.of(createIntent(intent, satPath, lambda)); |
| 183 | } |
| 184 | |
| 185 | resources.addAll(res); |
| 186 | allocateResources(intent, resources); |
| 187 | |
| 188 | return ImmutableList.of(createIntent(intent, satPath, lambda)); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | // FIXME: also check destination OCh port |
| 192 | // non-tunable case: early return |
| 193 | if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) { |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 194 | Path firstPath = paths.findAny().orElse(null); |
| 195 | if (firstPath == null) { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 196 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 197 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
| 198 | } |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 199 | OchSignal lambda = srcOchPort.lambda(); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 200 | // TODO apply the same as static lambda case above |
| 201 | // - pick feasible path |
| 202 | // - allocate resources |
| 203 | //resources.addAll(convertToResources(firstPath.links(), toFlexGrid(lambda))); |
| 204 | allocateResources(intent, resources); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 205 | return ImmutableList.of(createIntent(intent, firstPath, lambda)); |
| 206 | } |
Sho SHIMIZU | d978d9a | 2016-01-28 08:55:49 -0800 | [diff] [blame] | 207 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 208 | // remaining cases |
| 209 | // Use first path that the required resources are available |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 210 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 211 | .map(path -> Maps.immutableEntry(path, findFirstAvailableOch(path))) |
| 212 | .filter(entry -> !entry.getValue().isEmpty()) |
| 213 | .filter(entry -> convertToResources(entry.getKey().links(), |
| 214 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
| 215 | .findFirst(); |
| 216 | |
| 217 | if (found.isPresent()) { |
| 218 | resources.addAll(convertToResources(found.get().getKey().links(), found.get().getValue())); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 219 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 220 | allocateResources(intent, resources); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 221 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 222 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ); |
| 223 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
| 224 | } else { |
Yuta HIGUCHI | 0164c1c | 2017-05-04 15:43:55 -0700 | [diff] [blame] | 225 | log.error("Unable to find suitable lightpath for intent {}", intent); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 226 | throw new OpticalIntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 227 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 230 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
| 231 | // Create installable optical path intent |
| 232 | // Only support fixed grid for now |
| 233 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
| 234 | |
| 235 | return OpticalPathIntent.builder() |
| 236 | .appId(parentIntent.appId()) |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame] | 237 | .key(parentIntent.key()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 238 | .src(parentIntent.getSrc()) |
| 239 | .dst(parentIntent.getDst()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 240 | .path(path) |
| 241 | .lambda(lambda) |
| 242 | .signalType(signalType) |
| 243 | .bidirectional(parentIntent.isBidirectional()) |
Luca Prete | 670ac5d | 2017-02-03 15:55:43 -0800 | [diff] [blame] | 244 | .resourceGroup(parentIntent.resourceGroup()) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 245 | .build(); |
| 246 | } |
| 247 | |
| 248 | private void allocateResources(Intent intent, List<Resource> resources) { |
| 249 | // reserve all of required 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() |
| 255 | .map(Resource::toString) |
| 256 | .collect(Collectors.joining("\n\t"))); |
| 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 | |
| 262 | private List<OchSignal> findFirstAvailableOch(Path path) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 263 | Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links()); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 264 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 265 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 266 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 267 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 268 | return findFirstLambda(lambdas, slotCount()); |
| 269 | } |
| 270 | |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 271 | private List<Resource> convertToResources(List<Link> links, Collection<OchSignal> lambda) { |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 272 | return links.stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 273 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 274 | Resources.discrete(x.src().deviceId(), x.src().port()).resource(), |
| 275 | Resources.discrete(x.dst().deviceId(), x.dst().port()).resource() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 276 | )) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 277 | .flatMap(x -> lambda.stream().map(x::child)) |
Sho SHIMIZU | 7f44cfc | 2015-10-28 13:54:40 -0700 | [diff] [blame] | 278 | .collect(Collectors.toList()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 281 | /** |
| 282 | * Get the number of 12.5 GHz slots required for the path. |
| 283 | * |
| 284 | * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot), |
| 285 | * but in the future can depend on optical reach, line rate, transponder port capabilities, etc. |
| 286 | * |
| 287 | * @return number of slots |
| 288 | */ |
| 289 | private int slotCount() { |
| 290 | return SLOT_COUNT; |
| 291 | } |
| 292 | |
| 293 | private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) { |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 294 | return links.stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 295 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 296 | Resources.discrete(x.src().deviceId(), x.src().port()).id(), |
| 297 | Resources.discrete(x.dst().deviceId(), x.dst().port()).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()); |
| 303 | } |
| 304 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 305 | /** |
| 306 | * Returns list of consecutive resources in given set of lambdas. |
| 307 | * |
| 308 | * @param lambdas list of lambdas |
| 309 | * @param count number of consecutive lambdas to return |
| 310 | * @return list of consecutive lambdas |
| 311 | */ |
| 312 | private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) { |
| 313 | // Sort available lambdas |
| 314 | List<OchSignal> lambdaList = new ArrayList<>(lambdas); |
| 315 | lambdaList.sort(new DefaultOchSignalComparator()); |
| 316 | |
| 317 | // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps) |
| 318 | for (int i = 0; i < lambdaList.size() - count; i++) { |
| 319 | if (lambdaList.get(i).spacingMultiplier() + 2 * count == |
| 320 | lambdaList.get(i + count).spacingMultiplier()) { |
| 321 | return lambdaList.subList(i, i + count); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 328 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 329 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 330 | |
| 331 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 332 | |
| 333 | // FIXME: need a better way to match the port |
| 334 | if (staticPort != null) { |
| 335 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 336 | if (staticPort.equals(p.number().name())) { |
| 337 | return new ConnectPoint(p.element().id(), p.number()); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | return null; |
| 343 | } |
| 344 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 345 | /** |
| 346 | * Calculates optical paths in WDM topology. |
| 347 | * |
| 348 | * @param intent optical connectivity intent |
| 349 | * @return set of paths in WDM topology |
| 350 | */ |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 351 | private Stream<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 352 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 353 | Topology topology = topologyService.currentTopology(); |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 354 | LinkWeight weight = new LinkWeight() { |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 355 | |
Yuta HIGUCHI | d95d590 | 2016-06-27 00:18:45 -0700 | [diff] [blame] | 356 | @Override |
| 357 | public double weight(TopologyEdge edge) { |
| 358 | // Disregard inactive or non-optical links |
| 359 | if (edge.link().state() == Link.State.INACTIVE) { |
| 360 | return -1; |
| 361 | } |
| 362 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 363 | return -1; |
| 364 | } |
| 365 | // Adhere to static port mappings |
| 366 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 367 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 368 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 369 | if (srcStaticPort != null) { |
| 370 | return srcStaticPort.equals(edge.link().src()) ? 1 : -1; |
| 371 | } |
| 372 | } |
| 373 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 374 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 375 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 376 | if (dstStaticPort != null) { |
| 377 | return dstStaticPort.equals(edge.link().dst()) ? 1 : -1; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | return 1; |
| 382 | } |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 383 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 384 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 385 | ConnectPoint start = intent.getSrc(); |
| 386 | ConnectPoint end = intent.getDst(); |
Yuta HIGUCHI | 02f8d2e | 2017-04-14 17:00:12 -0700 | [diff] [blame] | 387 | Stream<Path> paths = topologyService.getKShortestPaths(topology, |
| 388 | start.deviceId(), |
| 389 | end.deviceId(), |
| 390 | AdapterLinkWeigher.adapt(weight)); |
| 391 | if (log.isDebugEnabled()) { |
| 392 | return paths |
| 393 | .map(path -> { |
| 394 | // no-op map stage to add debug logging |
| 395 | log.debug("Candidate path: {}", |
| 396 | path.links().stream() |
| 397 | .map(lk -> lk.src() + "-" + lk.dst()) |
| 398 | .collect(Collectors.toList())); |
| 399 | return path; |
| 400 | }); |
| 401 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 402 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 403 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 404 | } |