alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.net.flow; |
| 2 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 3 | import java.util.LinkedList; |
| 4 | import java.util.List; |
| 5 | import java.util.Objects; |
| 6 | |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 7 | import org.onlab.onos.net.PortNumber; |
alshabib | 55a55d9 | 2014-09-16 11:59:31 -0700 | [diff] [blame] | 8 | import org.onlab.onos.net.flow.instructions.Instruction; |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 9 | import org.onlab.onos.net.flow.instructions.Instructions; |
| 10 | import org.onlab.packet.IpPrefix; |
| 11 | import org.onlab.packet.MacAddress; |
| 12 | import org.onlab.packet.VlanId; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 13 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 14 | import com.google.common.base.MoreObjects; |
Yuta HIGUCHI | 2809bf3 | 2014-10-20 22:44:12 -0700 | [diff] [blame] | 15 | import com.google.common.collect.ImmutableList; |
| 16 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 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) { |
Yuta HIGUCHI | 2809bf3 | 2014-10-20 22:44:12 -0700 | [diff] [blame] | 30 | this.instructions = ImmutableList.copyOf(instructions); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 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 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 47 | /** |
| 48 | * Returns a new traffic treatment builder primed to produce entities |
| 49 | * patterned after the supplied treatment. |
| 50 | * |
| 51 | * @return traffic treatment builder |
| 52 | */ |
| 53 | public static TrafficTreatment.Builder builder(TrafficTreatment treatment) { |
| 54 | return new Builder(treatment); |
| 55 | } |
| 56 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 57 | //FIXME: Order of instructions may affect hashcode |
| 58 | @Override |
| 59 | public int hashCode() { |
| 60 | return Objects.hash(instructions); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public boolean equals(Object obj) { |
| 65 | if (this == obj) { |
| 66 | return true; |
| 67 | } |
| 68 | if (obj instanceof DefaultTrafficTreatment) { |
| 69 | DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj; |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 70 | return Objects.equals(instructions, that.instructions); |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 71 | |
| 72 | } |
| 73 | return false; |
| 74 | } |
| 75 | |
Jonathan Hart | d87aeca | 2014-10-21 10:42:52 -0700 | [diff] [blame] | 76 | @Override |
| 77 | public String toString() { |
| 78 | return MoreObjects.toStringHelper(getClass()) |
| 79 | .add("instructions", instructions) |
| 80 | .toString(); |
| 81 | } |
| 82 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 83 | /** |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 84 | * Builds a list of treatments following the following order. |
| 85 | * Modifications -> Group -> Output (including drop) |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 86 | */ |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 87 | public static final class Builder implements TrafficTreatment.Builder { |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 88 | |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 89 | boolean drop = false; |
| 90 | |
| 91 | List<Instruction> outputs = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 92 | |
| 93 | // TODO: should be a list of instructions based on group objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 94 | List<Instruction> groups = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 95 | |
| 96 | // TODO: should be a list of instructions based on modification objects |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 97 | List<Instruction> modifications = new LinkedList<>(); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 98 | |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 99 | // Creates a new builder |
| 100 | private Builder() { |
| 101 | } |
| 102 | |
Jonathan Hart | 6e88c68 | 2014-10-21 17:05:25 -0700 | [diff] [blame^] | 103 | // Creates a new builder based off an existing treatment |
| 104 | private Builder(TrafficTreatment treatment) { |
| 105 | for (Instruction instruction : treatment.instructions()) { |
| 106 | add(instruction); |
| 107 | } |
| 108 | } |
| 109 | |
alshabib | 8ca5390 | 2014-10-07 13:11:17 -0700 | [diff] [blame] | 110 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 111 | public Builder add(Instruction instruction) { |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 112 | if (drop) { |
| 113 | return this; |
| 114 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 115 | switch (instruction.type()) { |
tom | 9a693fd | 2014-10-03 11:32:19 -0700 | [diff] [blame] | 116 | case DROP: |
| 117 | drop = true; |
| 118 | break; |
| 119 | case OUTPUT: |
| 120 | outputs.add(instruction); |
| 121 | break; |
| 122 | case L2MODIFICATION: |
| 123 | case L3MODIFICATION: |
| 124 | // TODO: enforce modification order if any |
| 125 | modifications.add(instruction); |
| 126 | break; |
| 127 | case GROUP: |
| 128 | groups.add(instruction); |
| 129 | break; |
| 130 | default: |
tom | 1679e18 | 2014-10-09 13:50:45 -0700 | [diff] [blame] | 131 | throw new IllegalArgumentException("Unknown instruction type: " + |
| 132 | instruction.type()); |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 133 | } |
| 134 | return this; |
| 135 | } |
| 136 | |
| 137 | @Override |
alshabib | 010c31d | 2014-09-26 10:01:12 -0700 | [diff] [blame] | 138 | public void drop() { |
| 139 | add(Instructions.createDrop()); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public Builder setOutput(PortNumber number) { |
| 144 | return add(Instructions.createOutput(number)); |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Builder setEthSrc(MacAddress addr) { |
| 149 | return add(Instructions.modL2Src(addr)); |
| 150 | } |
| 151 | |
| 152 | @Override |
| 153 | public Builder setEthDst(MacAddress addr) { |
| 154 | return add(Instructions.modL2Dst(addr)); |
| 155 | } |
| 156 | |
| 157 | @Override |
| 158 | public Builder setVlanId(VlanId id) { |
| 159 | return add(Instructions.modVlanId(id)); |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Builder setVlanPcp(Byte pcp) { |
| 164 | return add(Instructions.modVlanPcp(pcp)); |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public Builder setIpSrc(IpPrefix addr) { |
| 169 | return add(Instructions.modL3Src(addr)); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public Builder setIpDst(IpPrefix addr) { |
| 174 | return add(Instructions.modL3Dst(addr)); |
| 175 | } |
| 176 | |
| 177 | @Override |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 178 | public TrafficTreatment build() { |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 179 | |
| 180 | //If we are dropping should we just return an emptry list? |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 181 | List<Instruction> instructions = new LinkedList<Instruction>(); |
| 182 | instructions.addAll(modifications); |
| 183 | instructions.addAll(groups); |
alshabib | 030111e | 2014-09-15 15:56:42 -0700 | [diff] [blame] | 184 | if (!drop) { |
| 185 | instructions.addAll(outputs); |
| 186 | } |
alshabib | 1d4cace | 2014-09-13 19:16:26 -0700 | [diff] [blame] | 187 | |
| 188 | return new DefaultTrafficTreatment(instructions); |
| 189 | } |
| 190 | |
| 191 | } |
| 192 | |
| 193 | } |