blob: d3fb7d9ef0370898875eb8420fe0e5ca9dc090b3 [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
alshabib8ca53902014-10-07 13:11:17 -070047 //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;
tom1679e182014-10-09 13:50:45 -070060 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070061
62 }
63 return false;
64 }
65
Jonathan Hartd87aeca2014-10-21 10:42:52 -070066 @Override
67 public String toString() {
68 return MoreObjects.toStringHelper(getClass())
69 .add("instructions", instructions)
70 .toString();
71 }
72
tom9a693fd2014-10-03 11:32:19 -070073 /**
alshabib1d4cace2014-09-13 19:16:26 -070074 * Builds a list of treatments following the following order.
75 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070076 */
tom9a693fd2014-10-03 11:32:19 -070077 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070078
alshabib030111e2014-09-15 15:56:42 -070079 boolean drop = false;
80
81 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070082
83 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070084 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070085
86 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070087 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070088
tom9a693fd2014-10-03 11:32:19 -070089 // Creates a new builder
90 private Builder() {
91 }
92
alshabib8ca53902014-10-07 13:11:17 -070093 @Override
alshabib1d4cace2014-09-13 19:16:26 -070094 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -070095 if (drop) {
96 return this;
97 }
alshabib1d4cace2014-09-13 19:16:26 -070098 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -070099 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:
tom1679e182014-10-09 13:50:45 -0700114 throw new IllegalArgumentException("Unknown instruction type: " +
115 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700116 }
117 return this;
118 }
119
120 @Override
alshabib010c31d2014-09-26 10:01:12 -0700121 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
alshabib1d4cace2014-09-13 19:16:26 -0700161 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700162
163 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700164 List<Instruction> instructions = new LinkedList<Instruction>();
165 instructions.addAll(modifications);
166 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700167 if (!drop) {
168 instructions.addAll(outputs);
169 }
alshabib1d4cace2014-09-13 19:16:26 -0700170
171 return new DefaultTrafficTreatment(instructions);
172 }
173
174 }
175
176}