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