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