blob: 269347ae96a500c752b47aa9fa7cc1b3c1803585 [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
10import java.util.Collections;
11import java.util.LinkedList;
12import java.util.List;
13import java.util.Objects;
alshabib1d4cace2014-09-13 19:16:26 -070014
tom9a693fd2014-10-03 11:32:19 -070015/**
16 * Default traffic treatment implementation.
17 */
alshabib7b2748f2014-09-16 20:21:11 -070018public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070019
20 private final List<Instruction> instructions;
21
tom9a693fd2014-10-03 11:32:19 -070022 /**
23 * Creates a new traffic treatment from the specified list of instructions.
24 *
25 * @param instructions treatment instructions
26 */
alshabib7b2748f2014-09-16 20:21:11 -070027 private DefaultTrafficTreatment(List<Instruction> instructions) {
alshabib1d4cace2014-09-13 19:16:26 -070028 this.instructions = Collections.unmodifiableList(instructions);
29 }
30
31 @Override
32 public List<Instruction> instructions() {
33 return instructions;
34 }
35
36 /**
tom9a693fd2014-10-03 11:32:19 -070037 * 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
alshabib8ca53902014-10-07 13:11:17 -070045 //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;
tom1679e182014-10-09 13:50:45 -070058 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070059
60 }
61 return false;
62 }
63
tom9a693fd2014-10-03 11:32:19 -070064 /**
alshabib1d4cace2014-09-13 19:16:26 -070065 * Builds a list of treatments following the following order.
66 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070067 */
tom9a693fd2014-10-03 11:32:19 -070068 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070069
alshabib030111e2014-09-15 15:56:42 -070070 boolean drop = false;
71
72 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070073
74 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070075 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070076
77 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070078 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070079
tom9a693fd2014-10-03 11:32:19 -070080 // Creates a new builder
81 private Builder() {
82 }
83
alshabib8ca53902014-10-07 13:11:17 -070084 @Override
alshabib1d4cace2014-09-13 19:16:26 -070085 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -070086 if (drop) {
87 return this;
88 }
alshabib1d4cace2014-09-13 19:16:26 -070089 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -070090 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:
tom1679e182014-10-09 13:50:45 -0700105 throw new IllegalArgumentException("Unknown instruction type: " +
106 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700107 }
108 return this;
109 }
110
111 @Override
alshabib010c31d2014-09-26 10:01:12 -0700112 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
alshabib1d4cace2014-09-13 19:16:26 -0700152 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700153
154 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700155 List<Instruction> instructions = new LinkedList<Instruction>();
156 instructions.addAll(modifications);
157 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700158 if (!drop) {
159 instructions.addAll(outputs);
160 }
alshabib1d4cace2014-09-13 19:16:26 -0700161
162 return new DefaultTrafficTreatment(instructions);
163 }
164
165 }
166
167}