Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [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 | */ |
| 16 | package org.onosproject.net.intent.impl.compiler; |
| 17 | |
| 18 | import com.google.common.collect.HashMultimap; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 20 | import com.google.common.collect.SetMultimap; |
| 21 | import org.apache.felix.scr.annotations.Activate; |
| 22 | import org.apache.felix.scr.annotations.Component; |
| 23 | import org.apache.felix.scr.annotations.Deactivate; |
| 24 | import org.apache.felix.scr.annotations.Reference; |
| 25 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 26 | import org.onlab.util.Identifier; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 27 | import org.onosproject.core.ApplicationId; |
| 28 | import org.onosproject.core.CoreService; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 31 | import org.onosproject.net.PortNumber; |
| 32 | import org.onosproject.net.flow.DefaultFlowRule; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 33 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 34 | import org.onosproject.net.flow.FlowRule; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 35 | import org.onosproject.net.flow.TrafficTreatment; |
| 36 | import org.onosproject.net.flow.instructions.Instruction; |
| 37 | import org.onosproject.net.flow.instructions.Instructions; |
| 38 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
| 39 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 40 | import org.onosproject.net.intent.FlowRuleIntent; |
| 41 | import org.onosproject.net.intent.Intent; |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 42 | import org.onosproject.net.intent.IntentCompilationException; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 43 | import org.onosproject.net.intent.IntentCompiler; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 44 | import org.onosproject.net.intent.LinkCollectionIntent; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 45 | import org.onosproject.net.intent.constraint.EncapsulationConstraint; |
| 46 | import org.onosproject.net.resource.ResourceService; |
| 47 | import org.onosproject.net.resource.impl.LabelAllocator; |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 48 | import org.slf4j.Logger; |
| 49 | import org.slf4j.LoggerFactory; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
Sho SHIMIZU | 98ffca8 | 2015-05-11 08:39:24 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 53 | import java.util.List; |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 54 | import java.util.Map; |
| 55 | import java.util.Optional; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 56 | import java.util.Set; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 57 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 58 | import static org.onosproject.net.flow.instructions.Instruction.Type.NOACTION; |
| 59 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 60 | /** |
| 61 | * Compiler to produce flow rules from link collections. |
| 62 | */ |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 63 | @Component(immediate = true) |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 64 | public class LinkCollectionIntentCompiler |
| 65 | extends LinkCollectionCompiler<FlowRule> |
| 66 | implements IntentCompiler<LinkCollectionIntent> { |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 67 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 68 | private static final String UNKNOWN_INSTRUCTION = "Unknown instruction type"; |
| 69 | private static final String UNSUPPORTED_INSTRUCTION = "Unsupported %s instruction"; |
| 70 | |
Pier Ventre | 27d4257 | 2016-08-29 17:37:08 -0700 | [diff] [blame] | 71 | private static Logger log = LoggerFactory.getLogger(LinkCollectionIntentCompiler.class); |
| 72 | |
| 73 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 75 | protected IntentConfigurableRegistrator registrator; |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 76 | |
| 77 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 78 | protected CoreService coreService; |
| 79 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 80 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 81 | protected ResourceService resourceService; |
| 82 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 83 | private ApplicationId appId; |
| 84 | |
| 85 | @Activate |
| 86 | public void activate() { |
| 87 | appId = coreService.registerApplication("org.onosproject.net.intent"); |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 88 | registrator.registerCompiler(LinkCollectionIntent.class, this, false); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 89 | if (labelAllocator == null) { |
| 90 | labelAllocator = new LabelAllocator(resourceService); |
| 91 | } |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Deactivate |
| 95 | public void deactivate() { |
Thomas Vachuska | bdbdd24 | 2016-03-01 01:55:55 -0800 | [diff] [blame] | 96 | registrator.unregisterCompiler(LinkCollectionIntent.class, false); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Sho SHIMIZU | ec07ffd | 2016-02-22 20:45:21 -0800 | [diff] [blame] | 100 | public List<Intent> compile(LinkCollectionIntent intent, List<Intent> installable) { |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 101 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 102 | SetMultimap<DeviceId, PortNumber> inputPorts = HashMultimap.create(); |
| 103 | SetMultimap<DeviceId, PortNumber> outputPorts = HashMultimap.create(); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 104 | Map<ConnectPoint, Identifier<?>> labels = ImmutableMap.of(); |
| 105 | |
| 106 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 107 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 108 | computePorts(intent, inputPorts, outputPorts); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 109 | |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 110 | if (encapConstraint.isPresent()) { |
| 111 | labels = labelAllocator.assignLabelToPorts(intent.links(), |
| 112 | intent.id(), |
| 113 | encapConstraint.get().encapType()); |
| 114 | } |
| 115 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 116 | List<FlowRule> rules = new ArrayList<>(); |
Yi Tseng | 155370e | 2016-09-20 11:08:32 -0700 | [diff] [blame] | 117 | for (DeviceId deviceId: outputPorts.keySet()) { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 118 | rules.addAll(createRules( |
| 119 | intent, |
| 120 | deviceId, |
| 121 | inputPorts.get(deviceId), |
| 122 | outputPorts.get(deviceId), |
| 123 | labels) |
| 124 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 125 | } |
Yuta HIGUCHI | 652f27f | 2016-10-31 16:54:30 -0700 | [diff] [blame^] | 126 | return Collections.singletonList(new FlowRuleIntent(appId, intent.key(), rules, intent.resources())); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 129 | @Override |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 130 | protected List<FlowRule> createRules(LinkCollectionIntent intent, |
| 131 | DeviceId deviceId, |
| 132 | Set<PortNumber> inPorts, |
| 133 | Set<PortNumber> outPorts, |
| 134 | Map<ConnectPoint, Identifier<?>> labels) { |
Brian O'Connor | 406e264 | 2016-04-18 11:45:35 -0700 | [diff] [blame] | 135 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 136 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 137 | /* |
| 138 | * Looking for the encapsulation constraint |
| 139 | */ |
| 140 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 141 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 142 | inPorts.forEach(inport -> { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 143 | |
| 144 | ForwardingInstructions instructions = this.createForwardingInstruction( |
| 145 | encapConstraint, |
| 146 | intent, |
| 147 | inport, |
| 148 | outPorts, |
| 149 | deviceId, |
| 150 | labels |
| 151 | ); |
| 152 | |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 153 | if (optimize) { |
| 154 | TrafficTreatment compactedTreatment = compactActions(instructions.treatment()); |
| 155 | instructions = new ForwardingInstructions(compactedTreatment, instructions.selector()); |
| 156 | } |
| 157 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 158 | FlowRule rule = DefaultFlowRule.builder() |
| 159 | .forDevice(deviceId) |
| 160 | .withSelector(instructions.selector()) |
| 161 | .withTreatment(instructions.treatment()) |
| 162 | .withPriority(intent.priority()) |
| 163 | .fromApp(appId) |
| 164 | .makePermanent() |
| 165 | .build(); |
| 166 | rules.add(rule); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 167 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 168 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 169 | |
| 170 | return rules; |
| 171 | } |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * This method tries to optimize the chain of actions. |
| 175 | * |
| 176 | * @param oldTreatment the list of instructions to optimize |
| 177 | * @return the optimized set of actions |
| 178 | */ |
| 179 | private TrafficTreatment compactActions(TrafficTreatment oldTreatment) { |
| 180 | |
| 181 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 182 | Instruction instruction; |
| 183 | Instruction newInstruction; |
| 184 | |
| 185 | for (int index = 0; index < oldTreatment.allInstructions().size(); index++) { |
| 186 | instruction = oldTreatment.allInstructions().get(index); |
| 187 | /* |
| 188 | * if the action is not optimizable. We simply add |
| 189 | * to the builder. |
| 190 | */ |
| 191 | if (checkInstruction(instruction)) { |
| 192 | treatmentBuilder.add(instruction); |
| 193 | continue; |
| 194 | } |
| 195 | /* |
| 196 | * We try to run an optimization; |
| 197 | */ |
| 198 | newInstruction = optimizeInstruction(index, instruction, oldTreatment.allInstructions()); |
| 199 | if (!newInstruction.type().equals(NOACTION)) { |
| 200 | treatmentBuilder.add(newInstruction); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return treatmentBuilder.build(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Verifies if the given L2 instruction can be optimized. |
| 209 | * |
| 210 | * @param l2instruction the l2 instruction to verify |
| 211 | * @return true if the instruction cannot be optimized. False otherwise |
| 212 | */ |
| 213 | private boolean checkL2Instructions(L2ModificationInstruction l2instruction) { |
| 214 | switch (l2instruction.subtype()) { |
| 215 | /* |
| 216 | * These actions can be performed safely. |
| 217 | */ |
| 218 | case ETH_SRC: |
| 219 | case ETH_DST: |
| 220 | case VLAN_ID: |
| 221 | case VLAN_PCP: |
| 222 | case MPLS_LABEL: |
| 223 | case MPLS_BOS: |
| 224 | case TUNNEL_ID: |
| 225 | case VLAN_PUSH: |
| 226 | case VLAN_POP: |
| 227 | case MPLS_PUSH: |
| 228 | case MPLS_POP: |
| 229 | return true; |
| 230 | /* |
| 231 | * We should avoid dec mpls ttl multiple |
| 232 | * times. |
| 233 | */ |
| 234 | case DEC_MPLS_TTL: |
| 235 | return false; |
| 236 | |
| 237 | default: |
| 238 | throw new IntentCompilationException(String.format(UNSUPPORTED_INSTRUCTION, "L2")); |
| 239 | } |
| 240 | |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Verifies if the given L3 instruction can be optimized. |
| 245 | * |
| 246 | * @param l3instruction the l3 instruction to verify |
| 247 | * @return true if the instruction cannot be optimized. False otherwise |
| 248 | */ |
| 249 | private boolean checkL3Instructions(L3ModificationInstruction l3instruction) { |
| 250 | switch (l3instruction.subtype()) { |
| 251 | /* |
| 252 | * These actions can be performed several times. |
| 253 | */ |
| 254 | case IPV4_SRC: |
| 255 | case IPV4_DST: |
| 256 | case IPV6_SRC: |
| 257 | case IPV6_DST: |
| 258 | case IPV6_FLABEL: |
| 259 | case ARP_SPA: |
| 260 | case ARP_SHA: |
| 261 | case ARP_OP: |
| 262 | case TTL_OUT: |
| 263 | case TTL_IN: |
| 264 | return true; |
| 265 | /* |
| 266 | * This action should be executed one time; |
| 267 | */ |
| 268 | case DEC_TTL: |
| 269 | return false; |
| 270 | default: |
| 271 | throw new IntentCompilationException(String.format(UNSUPPORTED_INSTRUCTION, "L3")); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Helper method to handle the optimization of the ttl instructions. |
| 277 | * |
| 278 | * @param index the index of the instruction |
| 279 | * @param instruction the instruction to optimize |
| 280 | * @param instructions the list of instructions to optimize |
| 281 | * @return no action if the action can be removed. The same instruction |
| 282 | * if we have to perform it |
| 283 | */ |
| 284 | private Instruction optimizeTtlInstructions(int index, Instruction instruction, List<Instruction> instructions) { |
| 285 | /** |
| 286 | * Here we handle the optimization of decrement mpls ttl. The optimization |
| 287 | * is to come back to the start of the list looking for the same |
| 288 | * action. If we find the same action, we can optimize. |
| 289 | */ |
| 290 | Instruction currentInstruction; |
| 291 | for (int i = index - 1; i >= 0; i--) { |
| 292 | currentInstruction = instructions.get(i); |
| 293 | if (currentInstruction.equals(instruction)) { |
| 294 | return Instructions.createNoAction(); |
| 295 | |
| 296 | } |
| 297 | } |
| 298 | return instruction; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Helper method to handle the optimization of the instructions. |
| 303 | * |
| 304 | * @param index the index of the instruction |
| 305 | * @param instruction the instruction to optimize |
| 306 | * @param instructions the list of instructions to optimize |
| 307 | * @return no action if the action can be removed. The same instruction |
| 308 | * if we have to perform it |
| 309 | */ |
| 310 | private Instruction optimizeInstruction(int index, Instruction instruction, List<Instruction> instructions) { |
| 311 | |
| 312 | switch (instruction.type()) { |
| 313 | /* |
| 314 | * Here we have the chance to optimize the dec mpls ttl action. |
| 315 | */ |
| 316 | case L2MODIFICATION: |
| 317 | /* |
| 318 | * Here we have the chance to optimize the ttl related actions. |
| 319 | */ |
| 320 | case L3MODIFICATION: |
| 321 | return optimizeTtlInstructions(index, instruction, instructions); |
| 322 | |
| 323 | default: |
| 324 | throw new IntentCompilationException(UNKNOWN_INSTRUCTION); |
| 325 | |
| 326 | } |
| 327 | |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Helper method to verify if the instruction can be optimized. |
| 332 | * |
| 333 | * @param instruction the instruction to verify |
| 334 | * @return true if the action can be optimized. False otherwise. |
| 335 | */ |
| 336 | private boolean checkInstruction(Instruction instruction) { |
| 337 | |
| 338 | switch (instruction.type()) { |
| 339 | /* |
| 340 | * The following instructions are not supported. |
| 341 | */ |
| 342 | case L0MODIFICATION: |
| 343 | case L1MODIFICATION: |
| 344 | case L4MODIFICATION: |
| 345 | case NOACTION: |
| 346 | case OUTPUT: |
| 347 | case GROUP: |
| 348 | case QUEUE: |
| 349 | case TABLE: |
| 350 | case METER: |
| 351 | case METADATA: |
| 352 | case EXTENSION: |
| 353 | return true; |
| 354 | /* |
| 355 | * Here we have the chance to optimize actions like dec mpls ttl. |
| 356 | */ |
| 357 | case L2MODIFICATION: |
| 358 | return checkL2Instructions((L2ModificationInstruction) instruction); |
| 359 | /* |
| 360 | * Here we have the chance to optimize the ttl related actions. |
| 361 | */ |
| 362 | case L3MODIFICATION: |
| 363 | return checkL3Instructions((L3ModificationInstruction) instruction); |
| 364 | |
| 365 | default: |
| 366 | throw new IntentCompilationException(UNKNOWN_INSTRUCTION); |
| 367 | |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 372 | } |