Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 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 | 47eb3bd | 2015-06-12 11:41:23 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableSet; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 25 | import org.onlab.util.Frequency; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 26 | import org.onosproject.net.AnnotationKeys; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 28 | import org.onosproject.net.DeviceId; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.Link; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 30 | import org.onosproject.net.OchPort; |
| 31 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 32 | import org.onosproject.net.OchSignalType; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 33 | import org.onosproject.net.OmsPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 35 | import org.onosproject.net.Port; |
| 36 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentCompiler; |
| 39 | import org.onosproject.net.intent.IntentExtensionService; |
| 40 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 41 | import org.onosproject.net.intent.OpticalPathIntent; |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 42 | import org.onosproject.net.intent.impl.IntentCompilationException; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 43 | import org.onosproject.net.resource.ResourceType; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 44 | import org.onosproject.net.resource.device.DeviceResourceService; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 45 | import org.onosproject.net.resource.link.DefaultLinkResourceRequest; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 46 | import org.onosproject.net.resource.link.LambdaResource; |
| 47 | import org.onosproject.net.resource.link.LambdaResourceAllocation; |
| 48 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 49 | import org.onosproject.net.resource.link.LinkResourceRequest; |
| 50 | import org.onosproject.net.resource.link.LinkResourceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 51 | import org.onosproject.net.topology.LinkWeight; |
| 52 | import org.onosproject.net.topology.Topology; |
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 | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 57 | import java.util.List; |
| 58 | import java.util.Set; |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 59 | import java.util.stream.Collectors; |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 60 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 61 | import static com.google.common.base.Preconditions.checkArgument; |
| 62 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 63 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 64 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 65 | */ |
| 66 | @Component(immediate = true) |
| 67 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 68 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 69 | protected static final Logger log = LoggerFactory.getLogger(OpticalConnectivityIntentCompiler.class); |
| 70 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected IntentExtensionService intentManager; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 75 | protected TopologyService topologyService; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 76 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected DeviceService deviceService; |
| 79 | |
| 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected LinkResourceService linkResourceService; |
| 82 | |
| 83 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 84 | protected DeviceResourceService deviceResourceService; |
| 85 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 86 | @Activate |
| 87 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 88 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 89 | } |
| 90 | |
| 91 | @Deactivate |
| 92 | public void deactivate() { |
| 93 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 94 | } |
| 95 | |
| 96 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 97 | public List<Intent> compile(OpticalConnectivityIntent intent, |
| 98 | List<Intent> installable, |
| 99 | Set<LinkResourceAllocations> resources) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 100 | // Check if source and destination are optical OCh ports |
| 101 | ConnectPoint src = intent.getSrc(); |
| 102 | ConnectPoint dst = intent.getDst(); |
| 103 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 104 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
| 105 | checkArgument(srcPort instanceof OchPort); |
| 106 | checkArgument(dstPort instanceof OchPort); |
| 107 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 108 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 109 | |
| 110 | // Reserve OCh ports |
Sho SHIMIZU | 47eb3bd | 2015-06-12 11:41:23 -0700 | [diff] [blame] | 111 | if (!deviceResourceService.requestPorts(ImmutableSet.of(srcPort, dstPort), intent)) { |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 112 | throw new IntentCompilationException("Unable to reserve ports for intent " + intent); |
| 113 | } |
| 114 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 115 | // Calculate available light paths |
| 116 | Set<Path> paths = getOpticalPaths(intent); |
| 117 | |
| 118 | // Use first path that can be successfully reserved |
| 119 | for (Path path : paths) { |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 120 | |
| 121 | // Static or dynamic lambda allocation |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 122 | String staticLambda = srcPort.annotations().value(AnnotationKeys.STATIC_LAMBDA); |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 123 | OchPort srcOchPort = (OchPort) srcPort; |
| 124 | OchPort dstOchPort = (OchPort) dstPort; |
| 125 | OchSignal ochSignal; |
| 126 | |
| 127 | // FIXME: need to actually reserve the lambda for static lambda's |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 128 | if (staticLambda != null) { |
HIGUCHI Yuta | 4a6a70a | 2015-09-04 14:42:10 -0700 | [diff] [blame] | 129 | ochSignal = new OchSignal(Frequency.ofHz(Long.parseLong(staticLambda)), |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 130 | srcOchPort.lambda().channelSpacing(), |
| 131 | srcOchPort.lambda().slotGranularity()); |
| 132 | } else if (!srcOchPort.isTunable() || !dstOchPort.isTunable()) { |
| 133 | // FIXME: also check OCh port |
| 134 | ochSignal = srcOchPort.lambda(); |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 135 | } else { |
| 136 | // Request and reserve lambda on path |
| 137 | LinkResourceAllocations linkAllocs = assignWavelength(intent, path); |
| 138 | if (linkAllocs == null) { |
| 139 | continue; |
| 140 | } |
Marc De Leenheer | c973308 | 2015-06-04 12:22:38 -0700 | [diff] [blame] | 141 | LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs); |
| 142 | OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port()); |
Marc De Leenheer | 0b8b2ef | 2015-08-03 15:39:00 -0700 | [diff] [blame] | 143 | ochSignal = new OchSignal(lambdaAlloc.lambda().toInt(), omsPort.maxFrequency(), omsPort.grid()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 146 | // Create installable optical path intent |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 147 | // Only support fixed grid for now |
| 148 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 149 | |
| 150 | Intent newIntent = OpticalPathIntent.builder() |
| 151 | .appId(intent.appId()) |
| 152 | .src(intent.getSrc()) |
| 153 | .dst(intent.getDst()) |
| 154 | .path(path) |
| 155 | .lambda(ochSignal) |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 156 | .signalType(signalType) |
Marc De Leenheer | 4a1c1fa | 2015-06-01 18:08:56 -0700 | [diff] [blame] | 157 | .bidirectional(intent.isBidirectional()) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 158 | .build(); |
| 159 | |
| 160 | return ImmutableList.of(newIntent); |
| 161 | } |
| 162 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 163 | // Release port allocations if unsuccessful |
| 164 | deviceResourceService.releasePorts(intent.id()); |
| 165 | |
| 166 | throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 169 | /** |
| 170 | * Find the lambda allocated to the path. |
| 171 | * |
| 172 | * @param path the path |
| 173 | * @param linkAllocs the link allocations |
Sho SHIMIZU | 5a6e2e5 | 2015-09-09 15:04:12 -0700 | [diff] [blame] | 174 | * @return lambda allocated to the given path |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 175 | */ |
| 176 | private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) { |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 177 | return path.links().stream() |
| 178 | .flatMap(x -> linkAllocs.getResourceAllocation(x).stream()) |
| 179 | .filter(x -> x.type() == ResourceType.LAMBDA) |
| 180 | .findFirst() |
| 181 | .map(x -> (LambdaResourceAllocation) x) |
| 182 | .orElse(null); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Request and reserve first available wavelength across path. |
| 187 | * |
| 188 | * @param path path in WDM topology |
| 189 | * @return first available lambda resource allocation |
| 190 | */ |
| 191 | private LinkResourceAllocations assignWavelength(Intent intent, Path path) { |
| 192 | LinkResourceRequest.Builder request = |
| 193 | DefaultLinkResourceRequest.builder(intent.id(), path.links()) |
| 194 | .addLambdaRequest(); |
| 195 | |
| 196 | LinkResourceAllocations allocations = linkResourceService.requestResources(request.build()); |
| 197 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 198 | if (!checkWavelengthContinuity(allocations, path)) { |
| 199 | linkResourceService.releaseResources(allocations); |
| 200 | return null; |
| 201 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 202 | |
| 203 | return allocations; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links. |
| 208 | * @return true if wavelength continuity is met, false otherwise |
| 209 | */ |
| 210 | private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) { |
| 211 | if (allocations == null) { |
| 212 | return false; |
| 213 | } |
| 214 | |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 215 | List<LambdaResource> lambdas = path.links().stream() |
| 216 | .flatMap(x -> allocations.getResourceAllocation(x).stream()) |
| 217 | .filter(x -> x.type() == ResourceType.LAMBDA) |
| 218 | .map(x -> ((LambdaResourceAllocation) x).lambda()) |
| 219 | .collect(Collectors.toList()); |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 220 | |
Sho SHIMIZU | 280e791 | 2015-09-29 13:42:03 -0700 | [diff] [blame] | 221 | LambdaResource lambda = null; |
| 222 | for (LambdaResource nextLambda: lambdas) { |
| 223 | if (nextLambda == null) { |
| 224 | return false; |
| 225 | } |
| 226 | if (lambda == null) { |
| 227 | lambda = nextLambda; |
| 228 | continue; |
| 229 | } |
| 230 | if (!lambda.equals(nextLambda)) { |
| 231 | return false; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
| 235 | return true; |
| 236 | } |
| 237 | |
Marc De Leenheer | 723f553 | 2015-06-03 20:16:17 -0700 | [diff] [blame] | 238 | private ConnectPoint staticPort(ConnectPoint connectPoint) { |
| 239 | Port port = deviceService.getPort(connectPoint.deviceId(), connectPoint.port()); |
| 240 | |
| 241 | String staticPort = port.annotations().value(AnnotationKeys.STATIC_PORT); |
| 242 | |
| 243 | // FIXME: need a better way to match the port |
| 244 | if (staticPort != null) { |
| 245 | for (Port p : deviceService.getPorts(connectPoint.deviceId())) { |
| 246 | if (staticPort.equals(p.number().name())) { |
| 247 | return new ConnectPoint(p.element().id(), p.number()); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return null; |
| 253 | } |
| 254 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 255 | /** |
| 256 | * Calculates optical paths in WDM topology. |
| 257 | * |
| 258 | * @param intent optical connectivity intent |
| 259 | * @return set of paths in WDM topology |
| 260 | */ |
| 261 | private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
| 262 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 263 | Topology topology = topologyService.currentTopology(); |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 264 | LinkWeight weight = edge -> { |
| 265 | // Disregard inactive or non-optical links |
| 266 | if (edge.link().state() == Link.State.INACTIVE) { |
| 267 | return -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 268 | } |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 269 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 270 | return -1; |
| 271 | } |
| 272 | // Adhere to static port mappings |
| 273 | DeviceId srcDeviceId = edge.link().src().deviceId(); |
| 274 | if (srcDeviceId.equals(intent.getSrc().deviceId())) { |
| 275 | ConnectPoint srcStaticPort = staticPort(intent.getSrc()); |
| 276 | if (srcStaticPort != null) { |
| 277 | return srcStaticPort.equals(edge.link().src()) ? 1 : -1; |
| 278 | } |
| 279 | } |
| 280 | DeviceId dstDeviceId = edge.link().dst().deviceId(); |
| 281 | if (dstDeviceId.equals(intent.getDst().deviceId())) { |
| 282 | ConnectPoint dstStaticPort = staticPort(intent.getDst()); |
| 283 | if (dstStaticPort != null) { |
| 284 | return dstStaticPort.equals(edge.link().dst()) ? 1 : -1; |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return 1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 289 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 290 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 291 | ConnectPoint start = intent.getSrc(); |
| 292 | ConnectPoint end = intent.getDst(); |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 293 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 294 | end.deviceId(), weight); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 295 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 296 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 297 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 298 | } |