Marc De Leenheer | 8c2caac | 2015-05-28 16:37:33 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import org.apache.commons.lang3.tuple.Pair; |
| 19 | import org.apache.felix.scr.annotations.Activate; |
| 20 | import org.apache.felix.scr.annotations.Component; |
| 21 | import org.apache.felix.scr.annotations.Deactivate; |
| 22 | import org.apache.felix.scr.annotations.Reference; |
| 23 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 24 | import org.onosproject.core.ApplicationId; |
| 25 | import org.onosproject.core.CoreService; |
| 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.OchPort; |
| 29 | import org.onosproject.net.OduCltPort; |
| 30 | import org.onosproject.net.OduSignalType; |
| 31 | import org.onosproject.net.Port; |
| 32 | import org.onosproject.net.device.DeviceService; |
| 33 | import org.onosproject.net.flow.DefaultFlowRule; |
| 34 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 35 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 36 | import org.onosproject.net.flow.FlowRule; |
| 37 | import org.onosproject.net.flow.TrafficSelector; |
| 38 | import org.onosproject.net.flow.TrafficTreatment; |
| 39 | import org.onosproject.net.intent.FlowRuleIntent; |
| 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.IntentId; |
| 44 | import org.onosproject.net.intent.IntentService; |
| 45 | import org.onosproject.net.intent.OpticalCircuitIntent; |
| 46 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
| 47 | import org.onosproject.net.intent.impl.IntentCompilationException; |
| 48 | import org.onosproject.net.resource.device.DeviceResourceService; |
| 49 | import org.onosproject.net.resource.link.LinkResourceAllocations; |
| 50 | import org.slf4j.Logger; |
| 51 | import org.slf4j.LoggerFactory; |
| 52 | |
| 53 | import java.util.Arrays; |
| 54 | import java.util.Collections; |
| 55 | import java.util.HashSet; |
| 56 | import java.util.LinkedList; |
| 57 | import java.util.List; |
| 58 | import java.util.Set; |
| 59 | |
| 60 | import static com.google.common.base.Preconditions.checkArgument; |
| 61 | |
| 62 | /** |
| 63 | * An intent compiler for {@link org.onosproject.net.intent.OpticalCircuitIntent}. |
| 64 | */ |
| 65 | @Component(immediate = true) |
| 66 | public class OpticalCircuitIntentCompiler implements IntentCompiler<OpticalCircuitIntent> { |
| 67 | |
| 68 | private static final Logger log = LoggerFactory.getLogger(OpticalCircuitIntentCompiler.class); |
| 69 | |
| 70 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 71 | protected IntentExtensionService intentManager; |
| 72 | |
| 73 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 74 | protected CoreService coreService; |
| 75 | |
| 76 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 77 | protected DeviceService deviceService; |
| 78 | |
| 79 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 80 | protected DeviceResourceService deviceResourceService; |
| 81 | |
| 82 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 83 | protected IntentService intentService; |
| 84 | |
| 85 | private ApplicationId appId; |
| 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
| 89 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
| 90 | intentManager.registerCompiler(OpticalCircuitIntent.class, this); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
| 95 | intentManager.unregisterCompiler(OpticalCircuitIntent.class); |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public List<Intent> compile(OpticalCircuitIntent intent, List<Intent> installable, |
| 100 | Set<LinkResourceAllocations> resources) { |
| 101 | // Check if ports are OduClt ports |
| 102 | ConnectPoint src = intent.getSrc(); |
| 103 | ConnectPoint dst = intent.getDst(); |
| 104 | Port srcPort = deviceService.getPort(src.deviceId(), src.port()); |
| 105 | Port dstPort = deviceService.getPort(dst.deviceId(), dst.port()); |
| 106 | checkArgument(srcPort instanceof OduCltPort); |
| 107 | checkArgument(dstPort instanceof OduCltPort); |
| 108 | |
| 109 | log.debug("Compiling optical circuit intent between {} and {}", src, dst); |
| 110 | |
| 111 | // Reserve OduClt ports |
| 112 | if (!deviceResourceService.requestPorts(new HashSet(Arrays.asList(srcPort, dstPort)), intent)) { |
| 113 | throw new IntentCompilationException("Unable to reserve ports for intent " + intent); |
| 114 | } |
| 115 | |
| 116 | LinkedList<Intent> intents = new LinkedList<>(); |
| 117 | |
| 118 | FlowRuleIntent circuitIntent; |
| 119 | OpticalConnectivityIntent connIntent = findOpticalConnectivityIntent(intent); |
| 120 | |
| 121 | // Create optical connectivity intent if needed |
| 122 | if (connIntent == null) { |
| 123 | // Find OCh ports with available resources |
| 124 | Pair<OchPort, OchPort> ochPorts = findPorts(intent); |
| 125 | |
| 126 | if (ochPorts == null) { |
| 127 | return Collections.emptyList(); |
| 128 | } |
| 129 | |
| 130 | // Create optical connectivity intent |
| 131 | ConnectPoint srcCP = new ConnectPoint(src.elementId(), ochPorts.getLeft().number()); |
| 132 | ConnectPoint dstCP = new ConnectPoint(dst.elementId(), ochPorts.getRight().number()); |
| 133 | // FIXME: hardcoded ODU signal type |
| 134 | connIntent = OpticalConnectivityIntent.builder() |
| 135 | .appId(appId) |
| 136 | .src(srcCP) |
| 137 | .dst(dstCP) |
| 138 | .signalType(OduSignalType.ODU4) |
| 139 | .build(); |
| 140 | intents.add(connIntent); |
| 141 | } |
| 142 | |
| 143 | // Create optical circuit intent |
| 144 | circuitIntent = new FlowRuleIntent( |
| 145 | appId, |
| 146 | createRules(src, connIntent.getSrc(), dst, connIntent.getDst()), |
| 147 | intent.resources()); |
| 148 | |
| 149 | // Save circuit to connectivity intent mapping |
| 150 | deviceResourceService.requestMapping(connIntent.id(), circuitIntent.id()); |
| 151 | intents.add(circuitIntent); |
| 152 | |
| 153 | return intents; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Checks if current allocations on given resource can satisfy request. |
| 158 | * |
| 159 | * @param request |
| 160 | * @param resource |
| 161 | * @return |
| 162 | */ |
| 163 | private boolean isAvailable(Intent request, IntentId resource) { |
| 164 | Set<IntentId> mapping = deviceResourceService.getMapping(resource); |
| 165 | |
| 166 | // TODO: hardcoded 10 x 10G |
| 167 | return mapping.size() < 10; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Returns existing and available optical connectivity intent that matches the given circuit intent. |
| 172 | * |
| 173 | * @param circuitIntent optical circuit intent |
| 174 | * @return existing optical connectivity intent, null otherwise. |
| 175 | */ |
| 176 | private OpticalConnectivityIntent findOpticalConnectivityIntent(OpticalCircuitIntent circuitIntent) { |
| 177 | for (Intent intent : intentService.getIntents()) { |
| 178 | if (!(intent instanceof OpticalConnectivityIntent)) { |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | OpticalConnectivityIntent connIntent = (OpticalConnectivityIntent) intent; |
| 183 | |
| 184 | ConnectPoint src = circuitIntent.getSrc(); |
| 185 | ConnectPoint dst = circuitIntent.getDst(); |
| 186 | if (!src.equals(connIntent.getSrc()) && !dst.equals(connIntent.getDst())) { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | if (isAvailable(circuitIntent, connIntent.id())) { |
| 191 | return connIntent; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return null; |
| 196 | } |
| 197 | |
| 198 | private OchPort findAvailableOchPort(DeviceId deviceId, OpticalCircuitIntent circuitIntent) { |
| 199 | List<Port> ports = deviceService.getPorts(deviceId); |
| 200 | |
| 201 | for (Port port : ports) { |
| 202 | if (!(port instanceof OchPort)) { |
| 203 | continue; |
| 204 | } |
| 205 | |
| 206 | // Port is not used |
| 207 | IntentId intentId = deviceResourceService.getAllocations(port); |
| 208 | if (intentId == null) { |
| 209 | return (OchPort) port; |
| 210 | } |
| 211 | |
| 212 | // Port is used but has free resources |
| 213 | if (isAvailable(circuitIntent, intentId)) { |
| 214 | return (OchPort) port; |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return null; |
| 219 | } |
| 220 | |
| 221 | // TODO: Add constraints for OduClt to OCh port mappings |
| 222 | // E.g., ports need to belong to same line card. |
| 223 | private Pair<OchPort, OchPort> findPorts(OpticalCircuitIntent intent) { |
| 224 | |
| 225 | OchPort srcPort = findAvailableOchPort(intent.getSrc().deviceId(), intent); |
| 226 | if (srcPort == null) { |
| 227 | return null; |
| 228 | } |
| 229 | |
| 230 | OchPort dstPort = findAvailableOchPort(intent.getDst().deviceId(), intent); |
| 231 | if (dstPort == null) { |
| 232 | return null; |
| 233 | } |
| 234 | |
| 235 | return Pair.of(srcPort, dstPort); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Builds flow rules for mapping between ODU and OCh ports. |
| 240 | * |
| 241 | * @param srcOdu |
| 242 | * @param dstOdu |
| 243 | * @return |
| 244 | */ |
| 245 | private List<FlowRule> createRules(ConnectPoint srcOdu, ConnectPoint srcOch, |
| 246 | ConnectPoint dstOdu, ConnectPoint dstOch) { |
| 247 | TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder(); |
| 248 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 249 | |
| 250 | // Source flow rule |
| 251 | selectorBuilder.matchInPort(srcOdu.port()); |
| 252 | //selectorBuilder.add(Criteria.matchCltSignalType) |
| 253 | treatmentBuilder.setOutput(srcOch.port()); |
| 254 | //treatmentBuilder.add(Instructions.modL1OduSignalType) |
| 255 | |
| 256 | FlowRule srcRule = DefaultFlowRule.builder() |
| 257 | .forDevice(srcOdu.deviceId()) |
| 258 | .withSelector(selectorBuilder.build()) |
| 259 | .withTreatment(treatmentBuilder.build()) |
| 260 | .withPriority(100) |
| 261 | .fromApp(appId) |
| 262 | .makePermanent() |
| 263 | .build(); |
| 264 | |
| 265 | // Destination flow rule |
| 266 | selectorBuilder.matchInPort(dstOch.port()); |
| 267 | //selectorBuilder.add(Criteria.matchOduSignalType) |
| 268 | treatmentBuilder.setOutput(dstOdu.port()); |
| 269 | //treatmentBuilder.add(Instructions.modL1CltSignalType) |
| 270 | |
| 271 | FlowRule dstRule = DefaultFlowRule.builder() |
| 272 | .forDevice(dstOdu.deviceId()) |
| 273 | .withSelector(selectorBuilder.build()) |
| 274 | .withTreatment(treatmentBuilder.build()) |
| 275 | .withPriority(100) |
| 276 | .fromApp(appId) |
| 277 | .makePermanent() |
| 278 | .build(); |
| 279 | |
| 280 | return Arrays.<FlowRule>asList(srcRule, dstRule); |
| 281 | } |
| 282 | } |