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