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