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