Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame^] | 2 | * Copyright 2015-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 | */ |
Sho SHIMIZU | 6c28f83 | 2015-02-20 16:12:19 -0800 | [diff] [blame] | 16 | package org.onosproject.net.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.OchPort; |
| 35 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 36 | import org.onosproject.net.OchSignalType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 38 | import org.onosproject.net.Port; |
| 39 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.Intent; |
| 41 | import org.onosproject.net.intent.IntentCompiler; |
| 42 | import org.onosproject.net.intent.IntentExtensionService; |
| 43 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 44 | import org.onosproject.net.intent.OpticalPathIntent; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intent.impl.IntentCompilationException; |
Sho SHIMIZU | e18cb12 | 2016-02-22 21:04:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.resource.ResourceAllocation; |
| 47 | import org.onosproject.net.resource.Resource; |
| 48 | import org.onosproject.net.resource.ResourceService; |
| 49 | import org.onosproject.net.resource.Resources; |
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; |
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; |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 57 | import java.util.Collections; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 58 | import java.util.List; |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 59 | import java.util.Map; |
| 60 | import java.util.Optional; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 61 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 62 | import java.util.stream.Collectors; |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 63 | import java.util.stream.Stream; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 64 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 65 | import static com.google.common.base.Preconditions.checkArgument; |
| 66 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 67 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 68 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 69 | */ |
Sho SHIMIZU | 9a2b829 | 2015-10-28 13:00:16 -0700 | [diff] [blame] | 70 | @Component(immediate = true) |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 71 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 72 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 73 | protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 74 | // By default, allocate 50 GHz lambdas (4 slots of 12.5 GHz) for each intent. |
| 75 | private static final int SLOT_COUNT = 4; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 76 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected IntentExtensionService intentManager; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 81 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 82 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected DeviceService deviceService; |
| 85 | |
| 86 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Sho SHIMIZU | 5c16df8 | 2015-09-29 12:52:07 -0700 | [diff] [blame] | 87 | protected ResourceService resourceService; |
| 88 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 89 | @Activate |
| 90 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 91 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | public void deactivate() { |
| 96 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 97 | } |
| 98 | |
| 99 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 100 | public List<Intent> compile(OpticalConnectivityIntent intent, |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 101 | List<Intent> installable) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 102 | // Check if source and destination are optical OCh ports |
| 103 | ConnectPoint src = intent.getSrc(); |
| 104 | ConnectPoint dst = intent.getDst(); |
| 105 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 106 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
| 107 | checkArgument(srcPort instanceof OchPort); |
| 108 | checkArgument(dstPort instanceof OchPort); |
| 109 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 110 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 111 | |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 112 | // Release of intent resources here is only a temporary solution for handling the |
| 113 | // case of recompiling due to intent restoration (when intent state is FAILED). |
| 114 | // TODO: try to release intent resources in IntentManager. |
| 115 | resourceService.release(intent.id()); |
| 116 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 117 | // Check OCh port availability |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 118 | Resource srcPortResource = Resources.discrete(src.deviceId(), src.port()).resource(); |
| 119 | Resource dstPortResource = Resources.discrete(dst.deviceId(), dst.port()).resource(); |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 120 | // If ports are not available, compilation fails |
| 121 | if (!Stream.of(srcPortResource, dstPortResource).allMatch(resourceService::isAvailable)) { |
| 122 | throw new IntentCompilationException("Ports for the intent are not available. Intent: " + intent); |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 125 | List<Resource> resources = new ArrayList<>(); |
| 126 | resources.add(srcPortResource); |
| 127 | resources.add(dstPortResource); |
| 128 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 129 | // Calculate available light paths |
| 130 | Set<Path> paths = getOpticalPaths(intent); |
| 131 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 132 | if (paths.isEmpty()) { |
| 133 | throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
| 134 | } |
| 135 | |
Sho SHIMIZU | d978d9a | 2016-01-28 08:55:49 -0800 | [diff] [blame] | 136 | // Static or dynamic lambda allocation |
| 137 | String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA); |
| 138 | OchPort srcOchPort = (OchPort) srcPort; |
| 139 | OchPort dstOchPort = (OchPort) dstPort; |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 140 | |
| 141 | Path firstPath = paths.iterator().next(); |
| 142 | // FIXME: need to actually reserve the lambda for static lambda's |
| 143 | // static lambda case: early return |
| 144 | if (staticLambda != null) { |
| 145 | allocateResources(intent, resources); |
| 146 | |
| 147 | OchSignal lambda = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)), |
| 148 | srcOchPort.lambda().channelSpacing(), |
| 149 | srcOchPort.lambda().slotGranularity()); |
| 150 | return ImmutableList.of(createIntent(intent, firstPath, lambda)); |
| 151 | } |
| 152 | |
| 153 | // FIXME: also check destination OCh port |
| 154 | // non-tunable case: early return |
| 155 | if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) { |
| 156 | allocateResources(intent, resources); |
| 157 | |
| 158 | OchSignal lambda = srcOchPort.lambda(); |
| 159 | return ImmutableList.of(createIntent(intent, firstPath, lambda)); |
| 160 | } |
Sho SHIMIZU | d978d9a | 2016-01-28 08:55:49 -0800 | [diff] [blame] | 161 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 162 | // remaining cases |
| 163 | // Use first path that the required resources are available |
| 164 | Optional<Map.Entry<Path, List<OchSignal>>> found = paths.stream() |
| 165 | .map(path -> Maps.immutableEntry(path, findFirstAvailableOch(path))) |
| 166 | .filter(entry -> !entry.getValue().isEmpty()) |
| 167 | .filter(entry -> convertToResources(entry.getKey().links(), |
| 168 | entry.getValue()).stream().allMatch(resourceService::isAvailable)) |
| 169 | .findFirst(); |
| 170 | |
| 171 | if (found.isPresent()) { |
| 172 | resources.addAll(convertToResources(found.get().getKey().links(), found.get().getValue())); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 173 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 174 | allocateResources(intent, resources); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 175 | |
Sho SHIMIZU | 07ade9b | 2016-01-28 19:38:50 -0800 | [diff] [blame] | 176 | OchSignal ochSignal = OchSignal.toFixedGrid(found.get().getValue(), ChannelSpacing.CHL_50GHZ); |
| 177 | return ImmutableList.of(createIntent(intent, found.get().getKey(), ochSignal)); |
| 178 | } else { |
| 179 | throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 180 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 183 | private Intent createIntent(OpticalConnectivityIntent parentIntent, Path path, OchSignal lambda) { |
| 184 | // Create installable optical path intent |
| 185 | // Only support fixed grid for now |
| 186 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
| 187 | |
| 188 | return OpticalPathIntent.builder() |
| 189 | .appId(parentIntent.appId()) |
| 190 | .src(parentIntent.getSrc()) |
| 191 | .dst(parentIntent.getDst()) |
| 192 | // calling paths.iterator().next() is safe because of non-empty set |
| 193 | .path(path) |
| 194 | .lambda(lambda) |
| 195 | .signalType(signalType) |
| 196 | .bidirectional(parentIntent.isBidirectional()) |
| 197 | .build(); |
| 198 | } |
| 199 | |
| 200 | private void allocateResources(Intent intent, List<Resource> resources) { |
| 201 | // reserve all of required resources |
| 202 | List<ResourceAllocation> allocations = resourceService.allocate(intent.id(), resources); |
| 203 | if (allocations.isEmpty()) { |
| 204 | log.info("Resource allocation for {} failed (resource request: {})", intent, resources); |
| 205 | throw new IntentCompilationException("Unable to allocate resources: " + resources); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private List<OchSignal> findFirstAvailableOch(Path path) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 210 | Set<OchSignal> lambdas = findCommonLambdasOverLinks(path.links()); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 211 | if (lambdas.isEmpty()) { |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 212 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 213 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 214 | |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 215 | return findFirstLambda(lambdas, slotCount()); |
| 216 | } |
| 217 | |
| 218 | private List<Resource> convertToResources(List<Link> links, List<OchSignal> lambda) { |
| 219 | return links.stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 220 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | 460b972 | 2016-01-28 10:48:26 -0800 | [diff] [blame] | 221 | Resources.discrete(x.src().deviceId(), x.src().port()).resource(), |
| 222 | Resources.discrete(x.dst().deviceId(), x.dst().port()).resource() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 223 | )) |
Sho SHIMIZU | 394918d | 2016-01-28 16:32:10 -0800 | [diff] [blame] | 224 | .flatMap(x -> lambda.stream().map(x::child)) |
Sho SHIMIZU | 7f44cfc | 2015-10-28 13:54:40 -0700 | [diff] [blame] | 225 | .collect(Collectors.toList()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 228 | /** |
| 229 | * Get the number of 12.5 GHz slots required for the path. |
| 230 | * |
| 231 | * For now this returns a constant value of 4 (i.e., fixed grid 50 GHz slot), |
| 232 | * but in the future can depend on optical reach, line rate, transponder port capabilities, etc. |
| 233 | * |
| 234 | * @return number of slots |
| 235 | */ |
| 236 | private int slotCount() { |
| 237 | return SLOT_COUNT; |
| 238 | } |
| 239 | |
| 240 | private Set<OchSignal> findCommonLambdasOverLinks(List<Link> links) { |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 241 | return links.stream() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 242 | .flatMap(x -> Stream.of( |
Sho SHIMIZU | dd3750c | 2016-02-01 11:37:04 -0800 | [diff] [blame] | 243 | Resources.discrete(x.src().deviceId(), x.src().port()).id(), |
| 244 | Resources.discrete(x.dst().deviceId(), x.dst().port()).id() |
Sho SHIMIZU | 44f3761 | 2015-11-25 16:23:22 -0800 | [diff] [blame] | 245 | )) |
Sho SHIMIZU | 7332fe4 | 2016-02-15 14:58:33 -0800 | [diff] [blame] | 246 | .map(x -> resourceService.getAvailableResourceValues(x, OchSignal.class)) |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 247 | .map(x -> (Set<OchSignal>) ImmutableSet.copyOf(x)) |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 248 | .reduce(Sets::intersection) |
| 249 | .orElse(Collections.emptySet()); |
| 250 | } |
| 251 | |
Marc De Leenheer | 2c30530 | 2015-12-07 21:37:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Returns list of consecutive resources in given set of lambdas. |
| 254 | * |
| 255 | * @param lambdas list of lambdas |
| 256 | * @param count number of consecutive lambdas to return |
| 257 | * @return list of consecutive lambdas |
| 258 | */ |
| 259 | private List<OchSignal> findFirstLambda(Set<OchSignal> lambdas, int count) { |
| 260 | // Sort available lambdas |
| 261 | List<OchSignal> lambdaList = new ArrayList<>(lambdas); |
| 262 | lambdaList.sort(new DefaultOchSignalComparator()); |
| 263 | |
| 264 | // Look ahead by count and ensure spacing multiplier is as expected (i.e., no gaps) |
| 265 | for (int i = 0; i < lambdaList.size() - count; i++) { |
| 266 | if (lambdaList.get(i).spacingMultiplier() + 2 * count == |
| 267 | lambdaList.get(i + count).spacingMultiplier()) { |
| 268 | return lambdaList.subList(i, i + count); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return Collections.emptyList(); |
Sho SHIMIZU | c25a008 | 2015-10-27 17:06:29 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 275 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 276 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 277 | |
| 278 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 279 | |
| 280 | // FIXME: need a better way to match the port |
| 281 | if (staticPort != null) { |
| 282 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 283 | if (staticPort.equals(p.number().name())) { |
| 284 | return new ConnectPoint(p.element().id(), p.number()); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return null; |
| 290 | } |
| 291 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 292 | /** |
| 293 | * Calculates optical paths in WDM topology. |
| 294 | * |
| 295 | * @param intent optical connectivity intent |
| 296 | * @return set of paths in WDM topology |
| 297 | */ |
| 298 | private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
| 299 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 300 | Topology topology = topologyService.currentTopology(); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 301 | LinkWeight weight = edge -> { |
| 302 | // Disregard inactive or non-optical links |
| 303 | if (edge.link().state() == Link.State.INACTIVE) { |
| 304 | return -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 305 | } |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 306 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 307 | return -1; |
| 308 | } |
| 309 | // Adhere to static port mappings |
| 310 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 311 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 312 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 313 | if (srcStaticPort != null) { |
| 314 | return srcStaticPort.equals(edge.link().src()) ? 1 : -1; |
| 315 | } |
| 316 | } |
| 317 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 318 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 319 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 320 | if (dstStaticPort != null) { |
| 321 | return dstStaticPort.equals(edge.link().dst()) ? 1 : -1; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return 1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 326 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 327 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 328 | ConnectPoint start = intent.getSrc(); |
| 329 | ConnectPoint end = intent.getDst(); |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 330 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 331 | end.deviceId(), weight); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 332 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 333 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 334 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 335 | } |