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 | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 10 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 11 | import org.onlab.onos.net.flow.instructions.Instructions; |
| 12 | import org.onlab.packet.IpPrefix; |
| 13 | import org.onlab.packet.MacAddress; |
| 14 | import org.onlab.packet.VlanId; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 15 | import org.slf4j.Logger; |
| 16 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 17 | public final class DefaultTrafficTreatment implements TrafficTreatment { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 18 | |
| 19 | private final List<Instruction> instructions; |
| 20 | |
alshabib | 7b2748f | 2014-09-16 20:21:11 -0700 | [diff] [blame] | 21 | private DefaultTrafficTreatment(List<Instruction> instructions) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 22 | this.instructions = Collections.unmodifiableList(instructions); |
| 23 | } |
| 24 | |
| 25 | @Override |
| 26 | public List<Instruction> instructions() { |
| 27 | return instructions; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Builds a list of treatments following the following order. |
| 32 | * Modifications -> Group -> Output (including drop) |
| 33 | * |
| 34 | */ |
| 35 | |
| 36 | public static class Builder implements TrafficTreatment.Builder { |
| 37 | |
| 38 | private final Logger log = getLogger(getClass()); |
| 39 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 40 | boolean drop = false; |
| 41 | |
| 42 | List<Instruction> outputs = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 43 | |
| 44 | // TODO: should be a list of instructions based on group objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 45 | List<Instruction> groups = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 46 | |
| 47 | // TODO: should be a list of instructions based on modification objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 48 | List<Instruction> modifications = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 49 | |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 50 | public Builder add(Instruction instruction) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 51 | if (drop) { |
| 52 | return this; |
| 53 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 54 | switch (instruction.type()) { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 55 | case DROP: |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 56 | drop = true; |
| 57 | break; |
| 58 | case OUTPUT: |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 59 | outputs.add(instruction); |
| 60 | break; |
alshabib | 35edb1a | 2014-09-16 17:44:44 -0700 | [diff] [blame] | 61 | case L2MODIFICATION: |
| 62 | case L3MODIFICATION: |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 63 | // TODO: enforce modification order if any |
| 64 | modifications.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 65 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 66 | case GROUP: |
| 67 | groups.add(instruction); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 68 | break; |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 69 | default: |
| 70 | log.warn("Unknown instruction type {}", instruction.type()); |
| 71 | } |
| 72 | return this; |
| 73 | } |
| 74 | |
| 75 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 76 | public void drop() { |
| 77 | add(Instructions.createDrop()); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public Builder setOutput(PortNumber number) { |
| 82 | return add(Instructions.createOutput(number)); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public Builder setEthSrc(MacAddress addr) { |
| 87 | return add(Instructions.modL2Src(addr)); |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public Builder setEthDst(MacAddress addr) { |
| 92 | return add(Instructions.modL2Dst(addr)); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public Builder setVlanId(VlanId id) { |
| 97 | return add(Instructions.modVlanId(id)); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Builder setVlanPcp(Byte pcp) { |
| 102 | return add(Instructions.modVlanPcp(pcp)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public Builder setIpSrc(IpPrefix addr) { |
| 107 | return add(Instructions.modL3Src(addr)); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public Builder setIpDst(IpPrefix addr) { |
| 112 | return add(Instructions.modL3Dst(addr)); |
| 113 | } |
| 114 | |
| 115 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 116 | public TrafficTreatment build() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 117 | |
| 118 | //If we are dropping should we just return an emptry list? |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 119 | List<Instruction> instructions = new LinkedList<Instruction>(); |
| 120 | instructions.addAll(modifications); |
| 121 | instructions.addAll(groups); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 122 | if (!drop) { |
| 123 | instructions.addAll(outputs); |
| 124 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 125 | |
| 126 | return new DefaultTrafficTreatment(instructions); |
| 127 | } |
| 128 | |
| 129 | } |
| 130 | |
| 131 | } |