alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow; |
| 2 | |
| 3 | import static org.slf4j.LoggerFactory.getLogger; |
| 4 | |
| 5 | import java.util.Collections; |
| 6 | import java.util.LinkedList; |
| 7 | import java.util.List; |
| 8 | |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 10 | import org.slf4j.Logger; |
| 11 | |
| 12 | @SuppressWarnings("rawtypes") |
| 13 | public class DefaultTrafficTreatment implements TrafficTreatment { |
| 14 | |
| 15 | private final List<Instruction> instructions; |
| 16 | |
| 17 | public DefaultTrafficTreatment(List<Instruction> instructions) { |
| 18 | this.instructions = Collections.unmodifiableList(instructions); |
| 19 | } |
| 20 | |
| 21 | @Override |
| 22 | public List<Instruction> instructions() { |
| 23 | return instructions; |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Builds a list of treatments following the following order. |
| 28 | * Modifications -> Group -> Output (including drop) |
| 29 | * |
| 30 | */ |
| 31 | |
| 32 | public static class Builder implements TrafficTreatment.Builder { |
| 33 | |
| 34 | private final Logger log = getLogger(getClass()); |
| 35 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 36 | boolean drop = false; |
| 37 | |
| 38 | List<Instruction> outputs = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 39 | |
| 40 | // TODO: should be a list of instructions based on group objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 41 | List<Instruction> groups = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 42 | |
| 43 | // TODO: should be a list of instructions based on modification objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 44 | List<Instruction> modifications = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 45 | |
| 46 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 47 | @Override |
| 48 | public Builder add(Instruction instruction) { |
| 49 | switch (instruction.type()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 50 | case DROP: |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 51 | drop = true; |
| 52 | break; |
| 53 | case OUTPUT: |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 54 | outputs.add(instruction); |
| 55 | break; |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 56 | case L2MODIFICATION: |
| 57 | case L3MODIFICATION: |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 58 | // TODO: enforce modification order if any |
| 59 | modifications.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 60 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 61 | case GROUP: |
| 62 | groups.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 63 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 64 | default: |
| 65 | log.warn("Unknown instruction type {}", instruction.type()); |
| 66 | } |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public TrafficTreatment build() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 72 | |
| 73 | //If we are dropping should we just return an emptry list? |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 74 | List<Instruction> instructions = new LinkedList<Instruction>(); |
| 75 | instructions.addAll(modifications); |
| 76 | instructions.addAll(groups); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 77 | if (!drop) { |
| 78 | instructions.addAll(outputs); |
| 79 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 80 | |
| 81 | return new DefaultTrafficTreatment(instructions); |
| 82 | } |
| 83 | |
| 84 | } |
| 85 | |
| 86 | } |