blob: d202217434a51c4cc919914767dee91e1b2605d2 [file] [log] [blame]
alshabib1d4cace2014-09-13 19:16:26 -07001package org.onlab.onos.net.flow;
2
alshabib010c31d2014-09-26 10:01:12 -07003import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -07004import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -07005import org.onlab.onos.net.flow.instructions.Instructions;
6import org.onlab.packet.IpPrefix;
7import org.onlab.packet.MacAddress;
8import org.onlab.packet.VlanId;
tom1679e182014-10-09 13:50:45 -07009
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070010import com.google.common.collect.ImmutableList;
11
tom1679e182014-10-09 13:50:45 -070012import java.util.LinkedList;
13import java.util.List;
14import java.util.Objects;
alshabib1d4cace2014-09-13 19:16:26 -070015
tom9a693fd2014-10-03 11:32:19 -070016/**
17 * Default traffic treatment implementation.
18 */
alshabib7b2748f2014-09-16 20:21:11 -070019public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070020
21 private final List<Instruction> instructions;
22
tom9a693fd2014-10-03 11:32:19 -070023 /**
24 * Creates a new traffic treatment from the specified list of instructions.
25 *
26 * @param instructions treatment instructions
27 */
alshabib7b2748f2014-09-16 20:21:11 -070028 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070029 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070030 }
31
32 @Override
33 public List<Instruction> instructions() {
34 return instructions;
35 }
36
37 /**
tom9a693fd2014-10-03 11:32:19 -070038 * 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
alshabib8ca53902014-10-07 13:11:17 -070046 //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;
tom1679e182014-10-09 13:50:45 -070059 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070060
61 }
62 return false;
63 }
64
tom9a693fd2014-10-03 11:32:19 -070065 /**
alshabib1d4cace2014-09-13 19:16:26 -070066 * Builds a list of treatments following the following order.
67 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070068 */
tom9a693fd2014-10-03 11:32:19 -070069 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070070
alshabib030111e2014-09-15 15:56:42 -070071 boolean drop = false;
72
73 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070074
75 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070076 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070077
78 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070079 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070080
tom9a693fd2014-10-03 11:32:19 -070081 // Creates a new builder
82 private Builder() {
83 }
84
alshabib8ca53902014-10-07 13:11:17 -070085 @Override
alshabib1d4cace2014-09-13 19:16:26 -070086 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -070087 if (drop) {
88 return this;
89 }
alshabib1d4cace2014-09-13 19:16:26 -070090 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -070091 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:
tom1679e182014-10-09 13:50:45 -0700106 throw new IllegalArgumentException("Unknown instruction type: " +
107 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700108 }
109 return this;
110 }
111
112 @Override
alshabib010c31d2014-09-26 10:01:12 -0700113 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
alshabib1d4cace2014-09-13 19:16:26 -0700153 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700154
155 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700156 List<Instruction> instructions = new LinkedList<Instruction>();
157 instructions.addAll(modifications);
158 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700159 if (!drop) {
160 instructions.addAll(outputs);
161 }
alshabib1d4cace2014-09-13 19:16:26 -0700162
163 return new DefaultTrafficTreatment(instructions);
164 }
165
166 }
167
168}