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 | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 18 | import java.util.Collections; |
Ray Milkey | e076c79 | 2015-03-24 09:38:30 -0700 | [diff] [blame] | 19 | import java.util.List; |
| 20 | import java.util.Set; |
| 21 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 22 | import org.apache.felix.scr.annotations.Activate; |
| 23 | import org.apache.felix.scr.annotations.Component; |
| 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; |
| 28 | import org.onosproject.net.ChannelSpacing; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 30 | import org.onosproject.net.GridType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import org.onosproject.net.Link; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 32 | import org.onosproject.net.OchPort; |
| 33 | import org.onosproject.net.OchSignal; |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame^] | 34 | import org.onosproject.net.OchSignalType; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 35 | import org.onosproject.net.OmsPort; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.Path; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 37 | import org.onosproject.net.Port; |
| 38 | import org.onosproject.net.device.DeviceService; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 39 | import org.onosproject.net.intent.Intent; |
| 40 | import org.onosproject.net.intent.IntentCompiler; |
| 41 | import org.onosproject.net.intent.IntentExtensionService; |
| 42 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 43 | import org.onosproject.net.intent.OpticalPathIntent; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 44 | import org.onosproject.net.resource.link.DefaultLinkResourceRequest; |
| 45 | import org.onosproject.net.resource.device.DeviceResourceService; |
| 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; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 51 | import org.onosproject.net.resource.ResourceAllocation; |
| 52 | import org.onosproject.net.resource.ResourceType; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 53 | import org.onosproject.net.topology.LinkWeight; |
| 54 | import org.onosproject.net.topology.Topology; |
| 55 | import org.onosproject.net.topology.TopologyEdge; |
| 56 | import org.onosproject.net.topology.TopologyService; |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 57 | |
Ray Milkey | e076c79 | 2015-03-24 09:38:30 -0700 | [diff] [blame] | 58 | import com.google.common.collect.ImmutableList; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 59 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 60 | import static com.google.common.base.Preconditions.checkArgument; |
| 61 | |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 62 | /** |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 63 | * An intent compiler for {@link org.onosproject.net.intent.OpticalConnectivityIntent}. |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 64 | */ |
| 65 | @Component(immediate = true) |
| 66 | public class OpticalConnectivityIntentCompiler implements IntentCompiler<OpticalConnectivityIntent> { |
| 67 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 68 | private static final GridType DEFAULT_OCH_GRIDTYPE = GridType.DWDM; |
| 69 | private static final ChannelSpacing DEFAULT_CHANNEL_SPACING = ChannelSpacing.CHL_50GHZ; |
| 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 | |
| 108 | // Calculate available light paths |
| 109 | Set<Path> paths = getOpticalPaths(intent); |
| 110 | |
| 111 | // Use first path that can be successfully reserved |
| 112 | for (Path path : paths) { |
| 113 | // Request and reserve lambda on path |
| 114 | LinkResourceAllocations linkAllocs = assignWavelength(intent, path); |
| 115 | if (linkAllocs == null) { |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | OmsPort omsPort = (OmsPort) deviceService.getPort(path.src().deviceId(), path.src().port()); |
| 120 | |
| 121 | // Try to reserve port resources, roll back if unsuccessful |
| 122 | Set<Port> portAllocs = deviceResourceService.requestPorts(intent); |
| 123 | if (portAllocs == null) { |
| 124 | linkResourceService.releaseResources(linkAllocs); |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // Create installable optical path intent |
| 129 | LambdaResourceAllocation lambdaAlloc = getWavelength(path, linkAllocs); |
| 130 | OchSignal ochSignal = getOchSignal(lambdaAlloc, omsPort.minFrequency(), omsPort.grid()); |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame^] | 131 | // Only support fixed grid for now |
| 132 | OchSignalType signalType = OchSignalType.FIXED_GRID; |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 133 | |
| 134 | Intent newIntent = OpticalPathIntent.builder() |
| 135 | .appId(intent.appId()) |
| 136 | .src(intent.getSrc()) |
| 137 | .dst(intent.getDst()) |
| 138 | .path(path) |
| 139 | .lambda(ochSignal) |
Marc De Leenheer | d24420f | 2015-05-27 09:40:59 -0700 | [diff] [blame^] | 140 | .signalType(signalType) |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 141 | .build(); |
| 142 | |
| 143 | return ImmutableList.of(newIntent); |
| 144 | } |
| 145 | |
| 146 | return Collections.emptyList(); |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Find the lambda allocated to the path. |
| 151 | * |
| 152 | * @param path the path |
| 153 | * @param linkAllocs the link allocations |
| 154 | * @return |
| 155 | */ |
| 156 | private LambdaResourceAllocation getWavelength(Path path, LinkResourceAllocations linkAllocs) { |
| 157 | for (Link link : path.links()) { |
| 158 | for (ResourceAllocation alloc : linkAllocs.getResourceAllocation(link)) { |
| 159 | if (alloc.type() == ResourceType.LAMBDA) { |
| 160 | return (LambdaResourceAllocation) alloc; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return null; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Request and reserve first available wavelength across path. |
| 170 | * |
| 171 | * @param path path in WDM topology |
| 172 | * @return first available lambda resource allocation |
| 173 | */ |
| 174 | private LinkResourceAllocations assignWavelength(Intent intent, Path path) { |
| 175 | LinkResourceRequest.Builder request = |
| 176 | DefaultLinkResourceRequest.builder(intent.id(), path.links()) |
| 177 | .addLambdaRequest(); |
| 178 | |
| 179 | LinkResourceAllocations allocations = linkResourceService.requestResources(request.build()); |
| 180 | |
| 181 | checkWavelengthContinuity(allocations, path); |
| 182 | |
| 183 | return allocations; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Checks wavelength continuity constraint across path, i.e., an identical lambda is used on all links. |
| 188 | * @return true if wavelength continuity is met, false otherwise |
| 189 | */ |
| 190 | private boolean checkWavelengthContinuity(LinkResourceAllocations allocations, Path path) { |
| 191 | if (allocations == null) { |
| 192 | return false; |
| 193 | } |
| 194 | |
| 195 | LambdaResource lambda = null; |
| 196 | |
| 197 | for (Link link : path.links()) { |
| 198 | for (ResourceAllocation alloc : allocations.getResourceAllocation(link)) { |
| 199 | if (alloc.type() == ResourceType.LAMBDA) { |
| 200 | LambdaResource nextLambda = ((LambdaResourceAllocation) alloc).lambda(); |
| 201 | if (nextLambda == null) { |
| 202 | return false; |
| 203 | } |
| 204 | if (lambda == null) { |
| 205 | lambda = nextLambda; |
| 206 | continue; |
| 207 | } |
| 208 | if (!lambda.equals(nextLambda)) { |
| 209 | return false; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | return true; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Convert lambda resource allocation in OCh signal. |
| 220 | * |
| 221 | * @param alloc lambda resource allocation |
| 222 | * @param minFrequency minimum frequency |
| 223 | * @param grid grid spacing frequency |
| 224 | * @return OCh signal |
| 225 | */ |
| 226 | private OchSignal getOchSignal(LambdaResourceAllocation alloc, Frequency minFrequency, Frequency grid) { |
| 227 | int channel = alloc.lambda().toInt(); |
| 228 | |
| 229 | // Calculate center frequency |
| 230 | Frequency centerFrequency = minFrequency.add(grid.multiply(channel)).add(grid.floorDivision(2)); |
| 231 | |
| 232 | // Build OCh signal object |
| 233 | int spacingMultiplier = (int) (centerFrequency.subtract(OchSignal.CENTER_FREQUENCY).asHz() / grid.asHz()); |
| 234 | int slotGranularity = (int) (grid.asHz() / ChannelSpacing.CHL_12P5GHZ.frequency().asHz()); |
| 235 | OchSignal ochSignal = new OchSignal(DEFAULT_OCH_GRIDTYPE, DEFAULT_CHANNEL_SPACING, |
| 236 | spacingMultiplier, slotGranularity); |
| 237 | |
| 238 | return ochSignal; |
| 239 | } |
| 240 | |
| 241 | /** |
| 242 | * Calculates optical paths in WDM topology. |
| 243 | * |
| 244 | * @param intent optical connectivity intent |
| 245 | * @return set of paths in WDM topology |
| 246 | */ |
| 247 | private Set<Path> getOpticalPaths(OpticalConnectivityIntent intent) { |
| 248 | // Route in WDM topology |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 249 | Topology topology = topologyService.currentTopology(); |
| 250 | LinkWeight weight = new LinkWeight() { |
| 251 | @Override |
| 252 | public double weight(TopologyEdge edge) { |
Praseed Balakrishnan | 00dd1f9 | 2014-11-19 17:12:36 -0800 | [diff] [blame] | 253 | if (edge.link().state() == Link.State.INACTIVE) { |
| 254 | return -1; |
| 255 | } |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 256 | if (edge.link().type() != Link.Type.OPTICAL) { |
| 257 | return -1; |
| 258 | } |
| 259 | return edge.link().annotations().value("optical.type").equals("WDM") ? +1 : -1; |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 260 | } |
| 261 | }; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 262 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 263 | ConnectPoint start = intent.getSrc(); |
| 264 | ConnectPoint end = intent.getDst(); |
Thomas Vachuska | 425a2d7 | 2014-10-29 11:28:28 -0700 | [diff] [blame] | 265 | Set<Path> paths = topologyService.getPaths(topology, start.deviceId(), |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 266 | end.deviceId(), weight); |
weibit | 7e58346 | 2014-10-23 10:14:05 -0700 | [diff] [blame] | 267 | |
Marc De Leenheer | 1afa2a0 | 2015-05-13 09:18:07 -0700 | [diff] [blame] | 268 | return paths; |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 269 | } |
weibit | f32383b | 2014-10-22 10:17:31 -0700 | [diff] [blame] | 270 | } |