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 |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 130 | boolean optimizeTreatments() { |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | @Override |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 135 | protected List<FlowRule> createRules(LinkCollectionIntent intent, |
| 136 | DeviceId deviceId, |
| 137 | Set<PortNumber> inPorts, |
| 138 | Set<PortNumber> outPorts, |
| 139 | Map<ConnectPoint, Identifier<?>> labels) { |
Brian O'Connor | 406e264 | 2016-04-18 11:45:35 -0700 | [diff] [blame] | 140 | |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 141 | List<FlowRule> rules = new ArrayList<>(inPorts.size()); |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Looking for the encapsulation constraint |
| 144 | */ |
| 145 | Optional<EncapsulationConstraint> encapConstraint = this.getIntentEncapConstraint(intent); |
Nicholas Dean | 126b8af | 2016-07-18 14:43:13 -0700 | [diff] [blame] | 146 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 147 | inPorts.forEach(inport -> { |
Pier Ventre | 766995d | 2016-10-05 22:15:56 -0700 | [diff] [blame] | 148 | |
| 149 | ForwardingInstructions instructions = this.createForwardingInstruction( |
| 150 | encapConstraint, |
| 151 | intent, |
| 152 | inport, |
| 153 | outPorts, |
| 154 | deviceId, |
| 155 | labels |
| 156 | ); |
| 157 | |
Yi Tseng | 84c5a3d | 2017-04-14 16:42:59 -0700 | [diff] [blame] | 158 | if (optimizeInstructions) { |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 159 | TrafficTreatment compactedTreatment = compactActions(instructions.treatment()); |
| 160 | instructions = new ForwardingInstructions(compactedTreatment, instructions.selector()); |
| 161 | } |
| 162 | |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 163 | FlowRule rule = DefaultFlowRule.builder() |
| 164 | .forDevice(deviceId) |
| 165 | .withSelector(instructions.selector()) |
| 166 | .withTreatment(instructions.treatment()) |
| 167 | .withPriority(intent.priority()) |
| 168 | .fromApp(appId) |
| 169 | .makePermanent() |
| 170 | .build(); |
| 171 | rules.add(rule); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 172 | } |
Pier Ventre | d48320e | 2016-08-17 16:25:47 -0700 | [diff] [blame] | 173 | ); |
Sho SHIMIZU | ee2aa65 | 2015-02-25 18:56:43 -0800 | [diff] [blame] | 174 | |
| 175 | return rules; |
| 176 | } |
Pier Ventre | 81c47bf | 2016-11-04 07:26:22 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * This method tries to optimize the chain of actions. |
| 180 | * |
| 181 | * @param oldTreatment the list of instructions to optimize |
| 182 | * @return the optimized set of actions |
| 183 | */ |
| 184 | private TrafficTreatment compactActions(TrafficTreatment oldTreatment) { |
| 185 | |
| 186 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 187 | Instruction instruction; |
| 188 | Instruction newInstruction; |
| 189 | |
| 190 | for (int index = 0; index < oldTreatment.allInstructions().size(); index++) { |
| 191 | instruction = oldTreatment.allInstructions().get(index); |
| 192 | /* |
| 193 | * if the action is not optimizable. We simply add |
| 194 | * to the builder. |
| 195 | */ |
| 196 | if (checkInstruction(instruction)) { |
| 197 | treatmentBuilder.add(instruction); |
| 198 | continue; |
| 199 | } |
| 200 | /* |
| 201 | * We try to run an optimization; |
| 202 | */ |
| 203 | newInstruction = optimizeInstruction(index, instruction, oldTreatment.allInstructions()); |
| 204 | if (!newInstruction.type().equals(NOACTION)) { |
| 205 | treatmentBuilder.add(newInstruction); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return treatmentBuilder.build(); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Verifies if the given L2 instruction can be optimized. |
| 214 | * |
| 215 | * @param l2instruction the l2 instruction to verify |
| 216 | * @return true if the instruction cannot be optimized. False otherwise |
| 217 | */ |
| 218 | private boolean checkL2Instructions(L2ModificationInstruction l2instruction) { |
| 219 | switch (l2instruction.subtype()) { |
| 220 | /* |
| 221 | * These actions can be performed safely. |
| 222 | */ |
| 223 | case ETH_SRC: |
| 224 | case ETH_DST: |
| 225 | case VLAN_ID: |
| 226 | case VLAN_PCP: |
| 227 | case MPLS_LABEL: |
| 228 | case MPLS_BOS: |
| 229 | case TUNNEL_ID: |
| 230 | case VLAN_PUSH: |
| 231 | case VLAN_POP: |
| 232 | case MPLS_PUSH: |
| 233 | case MPLS_POP: |
| 234 | return true; |
| 235 | /* |
| 236 | * We should avoid dec mpls ttl multiple |
| 237 | * times. |
| 238 | */ |
| 239 | case DEC_MPLS_TTL: |
| 240 | return false; |
| 241 | |
| 242 | default: |
| 243 | throw new IntentCompilationException(String.format(UNSUPPORTED_INSTRUCTION, "L2")); |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Verifies if the given L3 instruction can be optimized. |
| 250 | * |
| 251 | * @param l3instruction the l3 instruction to verify |
| 252 | * @return true if the instruction cannot be optimized. False otherwise |
| 253 | */ |
| 254 | private boolean checkL3Instructions(L3ModificationInstruction l3instruction) { |
| 255 | switch (l3instruction.subtype()) { |
| 256 | /* |
| 257 | * These actions can be performed several times. |
| 258 | */ |
| 259 | case IPV4_SRC: |
| 260 | case IPV4_DST: |
| 261 | case IPV6_SRC: |
| 262 | case IPV6_DST: |
| 263 | case IPV6_FLABEL: |
| 264 | case ARP_SPA: |
| 265 | case ARP_SHA: |
| 266 | case ARP_OP: |
| 267 | case TTL_OUT: |
| 268 | case TTL_IN: |
| 269 | return true; |
| 270 | /* |
| 271 | * This action should be executed one time; |
| 272 | */ |
| 273 | case DEC_TTL: |
| 274 | return false; |
| 275 | default: |
| 276 | throw new IntentCompilationException(String.format(UNSUPPORTED_INSTRUCTION, "L3")); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Helper method to handle the optimization of the ttl instructions. |
| 282 | * |
| 283 | * @param index the index of the instruction |
| 284 | * @param instruction the instruction to optimize |
| 285 | * @param instructions the list of instructions to optimize |
| 286 | * @return no action if the action can be removed. The same instruction |
| 287 | * if we have to perform it |
| 288 | */ |
| 289 | private Instruction optimizeTtlInstructions(int index, Instruction instruction, List<Instruction> instructions) { |
| 290 | /** |
| 291 | * Here we handle the optimization of decrement mpls ttl. The optimization |
| 292 | * is to come back to the start of the list looking for the same |
| 293 | * action. If we find the same action, we can optimize. |
| 294 | */ |
| 295 | Instruction currentInstruction; |
| 296 | for (int i = index - 1; i >= 0; i--) { |
| 297 | currentInstruction = instructions.get(i); |
| 298 | if (currentInstruction.equals(instruction)) { |
| 299 | return Instructions.createNoAction(); |
| 300 | |
| 301 | } |
| 302 | } |
| 303 | return instruction; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Helper method to handle the optimization of the instructions. |
| 308 | * |
| 309 | * @param index the index of the instruction |
| 310 | * @param instruction the instruction to optimize |
| 311 | * @param instructions the list of instructions to optimize |
| 312 | * @return no action if the action can be removed. The same instruction |
| 313 | * if we have to perform it |
| 314 | */ |
| 315 | private Instruction optimizeInstruction(int index, Instruction instruction, List<Instruction> instructions) { |
| 316 | |
| 317 | switch (instruction.type()) { |
| 318 | /* |
| 319 | * Here we have the chance to optimize the dec mpls ttl action. |
| 320 | */ |
| 321 | case L2MODIFICATION: |
| 322 | /* |
| 323 | * Here we have the chance to optimize the ttl related actions. |
| 324 | */ |
| 325 | case L3MODIFICATION: |
| 326 | return optimizeTtlInstructions(index, instruction, instructions); |
| 327 | |
| 328 | default: |
| 329 | throw new IntentCompilationException(UNKNOWN_INSTRUCTION); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Helper method to verify if the instruction can be optimized. |
| 337 | * |
| 338 | * @param instruction the instruction to verify |
| 339 | * @return true if the action can be optimized. False otherwise. |
| 340 | */ |
| 341 | private boolean checkInstruction(Instruction instruction) { |
| 342 | |
| 343 | switch (instruction.type()) { |
| 344 | /* |
| 345 | * The following instructions are not supported. |
| 346 | */ |
| 347 | case L0MODIFICATION: |
| 348 | case L1MODIFICATION: |
| 349 | case L4MODIFICATION: |
| 350 | case NOACTION: |
| 351 | case OUTPUT: |
| 352 | case GROUP: |
| 353 | case QUEUE: |
| 354 | case TABLE: |
| 355 | case METER: |
| 356 | case METADATA: |
| 357 | case EXTENSION: |
| 358 | return true; |
| 359 | /* |
| 360 | * Here we have the chance to optimize actions like dec mpls ttl. |
| 361 | */ |
| 362 | case L2MODIFICATION: |
| 363 | return checkL2Instructions((L2ModificationInstruction) instruction); |
| 364 | /* |
| 365 | * Here we have the chance to optimize the ttl related actions. |
| 366 | */ |
| 367 | case L3MODIFICATION: |
| 368 | return checkL3Instructions((L3ModificationInstruction) instruction); |
| 369 | |
| 370 | default: |
| 371 | throw new IntentCompilationException(UNKNOWN_INSTRUCTION); |
| 372 | |
| 373 | } |
| 374 | |
| 375 | } |
| 376 | |
Sho SHIMIZU | a09e1bb | 2016-08-01 14:25:25 -0700 | [diff] [blame] | 377 | } |