blob: 718291685bd37a8370809a250d367ab9e22fdb4a [file] [log] [blame]
alshabib1d4cace2014-09-13 19:16:26 -07001package org.onlab.onos.net.flow;
2
alshabib8ca53902014-10-07 13:11:17 -07003import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8import java.util.Objects;
9
alshabib010c31d2014-09-26 10:01:12 -070010import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -070011import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -070012import org.onlab.onos.net.flow.instructions.Instructions;
13import org.onlab.packet.IpPrefix;
14import org.onlab.packet.MacAddress;
15import org.onlab.packet.VlanId;
alshabib1d4cace2014-09-13 19:16:26 -070016import org.slf4j.Logger;
17
tom9a693fd2014-10-03 11:32:19 -070018/**
19 * Default traffic treatment implementation.
20 */
alshabib7b2748f2014-09-16 20:21:11 -070021public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070022
23 private final List<Instruction> instructions;
24
tom9a693fd2014-10-03 11:32:19 -070025 /**
26 * Creates a new traffic treatment from the specified list of instructions.
27 *
28 * @param instructions treatment instructions
29 */
alshabib7b2748f2014-09-16 20:21:11 -070030 private DefaultTrafficTreatment(List<Instruction> instructions) {
alshabib1d4cace2014-09-13 19:16:26 -070031 this.instructions = Collections.unmodifiableList(instructions);
32 }
33
34 @Override
35 public List<Instruction> instructions() {
36 return instructions;
37 }
38
39 /**
tom9a693fd2014-10-03 11:32:19 -070040 * Returns a new traffic treatment builder.
41 *
42 * @return traffic treatment builder
43 */
44 public static TrafficTreatment.Builder builder() {
45 return new Builder();
46 }
47
alshabib8ca53902014-10-07 13:11:17 -070048 //FIXME: Order of instructions may affect hashcode
49 @Override
50 public int hashCode() {
51 return Objects.hash(instructions);
52 }
53
54 @Override
55 public boolean equals(Object obj) {
56 if (this == obj) {
57 return true;
58 }
59 if (obj instanceof DefaultTrafficTreatment) {
60 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
61 return Objects.equals(instructions, that.instructions);
62
63 }
64 return false;
65 }
66
tom9a693fd2014-10-03 11:32:19 -070067 /**
alshabib1d4cace2014-09-13 19:16:26 -070068 * Builds a list of treatments following the following order.
69 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070070 */
tom9a693fd2014-10-03 11:32:19 -070071 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070072
73 private final Logger log = getLogger(getClass());
74
alshabib030111e2014-09-15 15:56:42 -070075 boolean drop = false;
76
77 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070078
79 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070080 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070081
82 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070083 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070084
tom9a693fd2014-10-03 11:32:19 -070085 // Creates a new builder
86 private Builder() {
87 }
88
alshabib8ca53902014-10-07 13:11:17 -070089 @Override
alshabib1d4cace2014-09-13 19:16:26 -070090 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -070091 if (drop) {
92 return this;
93 }
alshabib1d4cace2014-09-13 19:16:26 -070094 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -070095 case DROP:
96 drop = true;
97 break;
98 case OUTPUT:
99 outputs.add(instruction);
100 break;
101 case L2MODIFICATION:
102 case L3MODIFICATION:
103 // TODO: enforce modification order if any
104 modifications.add(instruction);
105 break;
106 case GROUP:
107 groups.add(instruction);
108 break;
109 default:
110 log.warn("Unknown instruction type {}", instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700111 }
112 return this;
113 }
114
115 @Override
alshabib010c31d2014-09-26 10:01:12 -0700116 public void drop() {
117 add(Instructions.createDrop());
118 }
119
120 @Override
121 public Builder setOutput(PortNumber number) {
122 return add(Instructions.createOutput(number));
123 }
124
125 @Override
126 public Builder setEthSrc(MacAddress addr) {
127 return add(Instructions.modL2Src(addr));
128 }
129
130 @Override
131 public Builder setEthDst(MacAddress addr) {
132 return add(Instructions.modL2Dst(addr));
133 }
134
135 @Override
136 public Builder setVlanId(VlanId id) {
137 return add(Instructions.modVlanId(id));
138 }
139
140 @Override
141 public Builder setVlanPcp(Byte pcp) {
142 return add(Instructions.modVlanPcp(pcp));
143 }
144
145 @Override
146 public Builder setIpSrc(IpPrefix addr) {
147 return add(Instructions.modL3Src(addr));
148 }
149
150 @Override
151 public Builder setIpDst(IpPrefix addr) {
152 return add(Instructions.modL3Dst(addr));
153 }
154
155 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700156 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700157
158 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700159 List<Instruction> instructions = new LinkedList<Instruction>();
160 instructions.addAll(modifications);
161 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700162 if (!drop) {
163 instructions.addAll(outputs);
164 }
alshabib1d4cace2014-09-13 19:16:26 -0700165
166 return new DefaultTrafficTreatment(instructions);
167 }
168
169 }
170
171}