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; |
| 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 | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 31 | import org.onosproject.net.GridType; |
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 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 74 | private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM; |
| 75 | private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ; |
| 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) |
| 87 | protected LinkResourceService linkResourceService; |
| 88 | |
| 89 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 90 | protected DeviceResourceService deviceResourceService; |
| 91 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 92 | @Activate |
| 93 | public void activate() { |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 94 | intentManager.registerCompiler(OpticalConnectivityIntent.class, this); |
| 95 | } |
| 96 | |
| 97 | @Deactivate |
| 98 | public void deactivate() { |
| 99 | intentManager.unregisterCompiler(OpticalConnectivityIntent.class); |
| 100 | } |
| 101 | |
| 102 | @Override |
Brian O'Connor | fa81eae | 2014-10-30 13:20:05 -0700 | [diff] [blame] | 103 | public List<Intent> compile(OpticalConnectivityIntent intent, |
| 104 | List<Intent> installable, |
| 105 | Set<LinkResourceAllocations> resources) { |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 106 | // Check if source and destination are optical OCh ports |
| 107 | ConnectPoint src = intent.getSrc(); |
| 108 | ConnectPoint dst = intent.getDst(); |
| 109 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 110 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
| 111 | checkArgument(srcPort instanceof OchPort); |
| 112 | checkArgument(dstPort instanceof OchPort); |
| 113 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 114 | log.debug("Compiling optical connectivity intent between {} and {}", src, dst); |
| 115 | |
| 116 | // Reserve OCh ports |
| 117 | if (!deviceResourceService.requestPorts(new HashSet(Arrays.asList(srcPort, dstPort)), intent)) { |
| 118 | throw new IntentCompilationException("Unable to reserve ports for intent " + intent); |
| 119 | } |
| 120 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 121 | // Calculate available light paths |
| 122 | Set<Path> paths = getOpticalPaths(intent); |
| 123 | |
| 124 | // Use first path that can be successfully reserved |
| 125 | for (Path path : paths) { |
| 126 | // Request and reserve lambda on path |
| 127 | LinkResourceAllocations linkAllocs = assignWavelength(intent, path); |
| 128 | if (linkAllocs == null) { |
| 129 | continue; |
| 130 | } |
| 131 | |
| 132 | OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port()); |
| 133 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 134 | // Create installable optical path intent |
| 135 | LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs); |
| 136 | OchSignal ochSignal = getOchSignal(lambdaAlloc, omsPort.minFrequency(), omsPort.grid()); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 137 | // Only support fixed grid for now |
| 138 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 139 | |
| 140 | Intent newIntent = OpticalPathIntent.builder() |
| 141 | .appId(intent.appId()) |
| 142 | .src(intent.getSrc()) |
| 143 | .dst(intent.getDst()) |
| 144 | .path(path) |
| 145 | .lambda(ochSignal) |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame] | 146 | .signalType(signalType) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 147 | .build(); |
| 148 | |
| 149 | return ImmutableList.of(newIntent); |
| 150 | } |
| 151 | |
Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame] | 152 | // Release port allocations if unsuccessful |
| 153 | deviceResourceService.releasePorts(intent.id()); |
| 154 | |
| 155 | throw new IntentCompilationException("Unable to find suitable lightpath for intent " + intent); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 158 | /** |
| 159 | * Find the lambda allocated to the path. |
| 160 | * |
| 161 | * @param path the path |
| 162 | * @param linkAllocs the link allocations |
| 163 | * @return |
| 164 | */ |
| 165 | private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) { |
| 166 | for (Link link : path.links()) { |
| 167 | for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) { |
| 168 | if (alloc.type() == ResourceType.LAMBDA) { |
| 169 | return (LambdaResourceAllocation) alloc; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return null; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Request and reserve first available wavelength across path. |
| 179 | * |
| 180 | * @param path path in WDM topology |
| 181 | * @return first available lambda resource allocation |
| 182 | */ |
| 183 | private LinkResourceAllocations assignWavelength(Intent intent, Path path) { |
| 184 | LinkResourceRequest.Builder request = |
| 185 | DefaultLinkResourceRequest.builder(intent.id(), path.links()) |
| 186 | .addLambdaRequest(); |
| 187 | |
| 188 | LinkResourceAllocations allocations = linkResourceService.requestResources(request.build()); |
| 189 | |
| 190 | checkWavelengthContinuity(allocations, path); |
| 191 | |
| 192 | return allocations; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links. |
| 197 | * @return true if wavelength continuity is met, false otherwise |
| 198 | */ |
| 199 | private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) { |
| 200 | if (allocations == null) { |
| 201 | return false; |
| 202 | } |
| 203 | |
| 204 | LambdaResource lambda = null; |
| 205 | |
| 206 | for (Link link : path.links()) { |
| 207 | for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) { |
| 208 | if (alloc.type() == ResourceType.LAMBDA) { |
| 209 | LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda(); |
| 210 | if (nextLambda == null) { |
| 211 | return false; |
| 212 | } |
| 213 | if (lambda == null) { |
| 214 | lambda = nextLambda; |
| 215 | continue; |
| 216 | } |
| 217 | if (!lambda.equals(nextLambda)) { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Convert lambda resource allocation in OCh signal. |
| 229 | * |
| 230 | * @param alloc lambda resource allocation |
| 231 | * @param minFrequency minimum frequency |
| 232 | * @param grid grid spacing frequency |
| 233 | * @return OCh signal |
| 234 | */ |
| 235 | private OchSignal getOchSignal(LambdaResourceAllocation alloc, Frequency minFrequency, Frequency grid) { |
| 236 | int channel = alloc.lambda().toInt(); |
| 237 | |
| 238 | // Calculate center frequency |
| 239 | Frequency centerFrequency = minFrequency.add(grid.multiply(channel)).add(grid.floorDivision(2)); |
| 240 | |
| 241 | // Build OCh signal object |
| 242 | int spacingMultiplier = (int) (centerFrequency.subtract(OchSignal.CENTER_FREQUENCY).asHz() / grid.asHz()); |
| 243 | int slotGranularity = (int) (grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz()); |
| 244 | OchSignal ochSignal = new OchSignal(DEFAULT_OCH_GRIDTYPE, DEFAULT_CHANNEL_SPACING, |
| 245 | spacingMultiplier, slotGranularity); |
| 246 | |
| 247 | return ochSignal; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Calculates optical paths in WDM topology. |
| 252 | * |
| 253 | * @param intent optical connectivity intent |
| 254 | * @return set of paths in WDM topology |
| 255 | */ |
| 256 | private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
| 257 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 258 | Topology topology = topologyService.currentTopology(); |
| 259 | LinkWeight weight = new LinkWeight() { |
| 260 | @Override |
| 261 | public double weight(TopologyEdge edge) { |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 262 | if (edge.link().state() == Link.State.INACTIVE) { |
| 263 | return -1; |
| 264 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 265 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 266 | return -1; |
| 267 | } |
| 268 | return edge.link().annotations().value("optical.type").equals("WDM") ? +1 : -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 269 | } |
| 270 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 271 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 272 | ConnectPoint start = intent.getSrc(); |
| 273 | ConnectPoint end = intent.getDst(); |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 274 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 275 | end.deviceId(), weight); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 276 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 277 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 278 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 279 | } |