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; |
| 56 | case MODIFICATION: |
| 57 | // TODO: enforce modification order if any |
| 58 | modifications.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 59 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 60 | case GROUP: |
| 61 | groups.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 62 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 63 | default: |
| 64 | log.warn("Unknown instruction type {}", instruction.type()); |
| 65 | } |
| 66 | return this; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public TrafficTreatment build() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 71 | |
| 72 | //If we are dropping should we just return an emptry list? |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 73 | List<Instruction> instructions = new LinkedList<Instruction>(); |
| 74 | instructions.addAll(modifications); |
| 75 | instructions.addAll(groups); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 76 | if (!drop) { |
| 77 | instructions.addAll(outputs); |
| 78 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 79 | |
| 80 | return new DefaultTrafficTreatment(instructions); |
| 81 | } |
| 82 | |
| 83 | } |
| 84 | |
| 85 | } |