blob: 5e64c6422b43a5c5f854ad62d1ab212bcfb8de3b [file] [log] [blame]
alshabib1d4cace2014-09-13 19:16:26 -07001package org.onlab.onos.net.flow;
2
Jonathan Hartd87aeca2014-10-21 10:42:52 -07003import java.util.LinkedList;
4import java.util.List;
5import java.util.Objects;
6
alshabib010c31d2014-09-26 10:01:12 -07007import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -07008import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -07009import org.onlab.onos.net.flow.instructions.Instructions;
10import org.onlab.packet.IpPrefix;
11import org.onlab.packet.MacAddress;
12import org.onlab.packet.VlanId;
tom1679e182014-10-09 13:50:45 -070013
Jonathan Hartd87aeca2014-10-21 10:42:52 -070014import com.google.common.base.MoreObjects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070015import com.google.common.collect.ImmutableList;
16
tom9a693fd2014-10-03 11:32:19 -070017/**
18 * Default traffic treatment implementation.
19 */
alshabib7b2748f2014-09-16 20:21:11 -070020public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070021
22 private final List<Instruction> instructions;
23
tom9a693fd2014-10-03 11:32:19 -070024 /**
25 * Creates a new traffic treatment from the specified list of instructions.
26 *
27 * @param instructions treatment instructions
28 */
alshabib7b2748f2014-09-16 20:21:11 -070029 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070030 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070031 }
32
33 @Override
34 public List<Instruction> instructions() {
35 return instructions;
36 }
37
38 /**
tom9a693fd2014-10-03 11:32:19 -070039 * 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 Hart6e88c682014-10-21 17:05:25 -070047 /**
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
alshabib8ca53902014-10-07 13:11:17 -070057 //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;
tom1679e182014-10-09 13:50:45 -070070 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070071
72 }
73 return false;
74 }
75
Jonathan Hartd87aeca2014-10-21 10:42:52 -070076 @Override
77 public String toString() {
78 return MoreObjects.toStringHelper(getClass())
79 .add("instructions", instructions)
80 .toString();
81 }
82
tom9a693fd2014-10-03 11:32:19 -070083 /**
alshabib1d4cace2014-09-13 19:16:26 -070084 * Builds a list of treatments following the following order.
85 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070086 */
tom9a693fd2014-10-03 11:32:19 -070087 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070088
alshabib030111e2014-09-15 15:56:42 -070089 boolean drop = false;
90
91 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070092
93 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070094 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070095
96 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070097 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070098
tom9a693fd2014-10-03 11:32:19 -070099 // Creates a new builder
100 private Builder() {
101 }
102
Jonathan Hart6e88c682014-10-21 17:05:25 -0700103 // 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
alshabib8ca53902014-10-07 13:11:17 -0700110 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700111 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -0700112 if (drop) {
113 return this;
114 }
alshabib1d4cace2014-09-13 19:16:26 -0700115 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700116 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:
tom1679e182014-10-09 13:50:45 -0700131 throw new IllegalArgumentException("Unknown instruction type: " +
132 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700133 }
134 return this;
135 }
136
137 @Override
alshabib010c31d2014-09-26 10:01:12 -0700138 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
alshabib1d4cace2014-09-13 19:16:26 -0700178 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700179
180 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700181 List<Instruction> instructions = new LinkedList<Instruction>();
182 instructions.addAll(modifications);
183 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700184 if (!drop) {
185 instructions.addAll(outputs);
186 }
alshabib1d4cace2014-09-13 19:16:26 -0700187
188 return new DefaultTrafficTreatment(instructions);
189 }
190
191 }
192
193}