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