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