blob: a8880b083f9c8f16cada753a85c0e440245a7133 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska83e090e2014-10-22 14:25:35 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska83e090e2014-10-22 14:25:35 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska83e090e2014-10-22 14:25:35 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.flow;
alshabib1d4cace2014-09-13 19:16:26 -070017
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080018import com.google.common.base.MoreObjects;
19import com.google.common.collect.ImmutableList;
Michele Santuari4b6019e2014-12-19 11:31:45 +010020
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070021import org.onlab.packet.IpAddress;
alshabib010c31d2014-09-26 10:01:12 -070022import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onlab.packet.MplsLabel;
alshabib010c31d2014-09-26 10:01:12 -070024import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080025import org.onosproject.core.GroupId;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080026import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.instructions.Instruction;
28import org.onosproject.net.flow.instructions.Instructions;
tom1679e182014-10-09 13:50:45 -070029
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080030import java.util.LinkedList;
31import java.util.List;
32import java.util.Objects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070033
tom9a693fd2014-10-03 11:32:19 -070034/**
35 * Default traffic treatment implementation.
36 */
alshabib7b2748f2014-09-16 20:21:11 -070037public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070038
39 private final List<Instruction> instructions;
40
tom9a693fd2014-10-03 11:32:19 -070041 /**
42 * Creates a new traffic treatment from the specified list of instructions.
43 *
44 * @param instructions treatment instructions
45 */
alshabib7b2748f2014-09-16 20:21:11 -070046 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070047 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070048 }
49
50 @Override
51 public List<Instruction> instructions() {
52 return instructions;
53 }
54
55 /**
tom9a693fd2014-10-03 11:32:19 -070056 * Returns a new traffic treatment builder.
57 *
58 * @return traffic treatment builder
59 */
60 public static TrafficTreatment.Builder builder() {
61 return new Builder();
62 }
63
Jonathan Hart6e88c682014-10-21 17:05:25 -070064 /**
65 * Returns a new traffic treatment builder primed to produce entities
66 * patterned after the supplied treatment.
67 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080068 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -070069 * @return traffic treatment builder
70 */
71 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
72 return new Builder(treatment);
73 }
74
alshabib8ca53902014-10-07 13:11:17 -070075 //FIXME: Order of instructions may affect hashcode
76 @Override
77 public int hashCode() {
78 return Objects.hash(instructions);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj instanceof DefaultTrafficTreatment) {
87 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
tom1679e182014-10-09 13:50:45 -070088 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070089
90 }
91 return false;
92 }
93
Jonathan Hartd87aeca2014-10-21 10:42:52 -070094 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .add("instructions", instructions)
98 .toString();
99 }
100
tom9a693fd2014-10-03 11:32:19 -0700101 /**
alshabib1d4cace2014-09-13 19:16:26 -0700102 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700103 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700104 */
tom9a693fd2014-10-03 11:32:19 -0700105 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700106
alshabib030111e2014-09-15 15:56:42 -0700107 boolean drop = false;
108
109 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700110
111 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -0700112 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700113
114 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -0700115 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700116
tom9a693fd2014-10-03 11:32:19 -0700117 // Creates a new builder
118 private Builder() {
119 }
120
Jonathan Hart6e88c682014-10-21 17:05:25 -0700121 // Creates a new builder based off an existing treatment
122 private Builder(TrafficTreatment treatment) {
123 for (Instruction instruction : treatment.instructions()) {
124 add(instruction);
125 }
126 }
127
alshabib8ca53902014-10-07 13:11:17 -0700128 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700129 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -0700130 if (drop) {
131 return this;
132 }
alshabib1d4cace2014-09-13 19:16:26 -0700133 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700134 case DROP:
135 drop = true;
136 break;
alshabib10580802015-02-18 18:30:33 -0800137 case TABLE:
tom9a693fd2014-10-03 11:32:19 -0700138 case OUTPUT:
139 outputs.add(instruction);
140 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700141 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700142 case L2MODIFICATION:
143 case L3MODIFICATION:
144 // TODO: enforce modification order if any
145 modifications.add(instruction);
146 break;
147 case GROUP:
148 groups.add(instruction);
149 break;
150 default:
tom1679e182014-10-09 13:50:45 -0700151 throw new IllegalArgumentException("Unknown instruction type: " +
152 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700153 }
154 return this;
155 }
156
157 @Override
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800158 public Builder drop() {
159 return add(Instructions.createDrop());
160 }
161
162 @Override
163 public Builder punt() {
164 return add(Instructions.createOutput(PortNumber.CONTROLLER));
alshabib010c31d2014-09-26 10:01:12 -0700165 }
166
167 @Override
168 public Builder setOutput(PortNumber number) {
169 return add(Instructions.createOutput(number));
170 }
171
172 @Override
173 public Builder setEthSrc(MacAddress addr) {
174 return add(Instructions.modL2Src(addr));
175 }
176
177 @Override
178 public Builder setEthDst(MacAddress addr) {
179 return add(Instructions.modL2Dst(addr));
180 }
181
182 @Override
183 public Builder setVlanId(VlanId id) {
184 return add(Instructions.modVlanId(id));
185 }
186
187 @Override
188 public Builder setVlanPcp(Byte pcp) {
189 return add(Instructions.modVlanPcp(pcp));
190 }
191
192 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700193 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700194 return add(Instructions.modL3Src(addr));
195 }
196
197 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700198 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700199 return add(Instructions.modL3Dst(addr));
200 }
201
202 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800203 public Builder decNwTtl() {
204 return add(Instructions.decNwTtl());
205 }
206
207 @Override
208 public Builder copyTtlIn() {
209 return add(Instructions.copyTtlIn());
210 }
211
212 @Override
213 public Builder copyTtlOut() {
214 return add(Instructions.copyTtlOut());
215 }
216
217 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800218 public Builder pushMpls() {
219 return add(Instructions.pushMpls());
220 }
221
222 @Override
223 public Builder popMpls() {
224 return add(Instructions.popMpls());
225 }
226
sangho3f97a17d2015-01-29 22:56:29 -0800227 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100228 public Builder popMpls(Short etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800229 return add(Instructions.popMpls(etherType));
230 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800231
232 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100233 public Builder setMpls(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800234 return add(Instructions.modMplsLabel(mplsLabel));
235 }
236
237 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800238 public Builder decMplsTtl() {
239 return add(Instructions.decMplsTtl());
240 }
241
242 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700243 public Builder setLambda(short lambda) {
244 return add(Instructions.modL0Lambda(lambda));
245 }
246
247 @Override
sangho8995ac52015-02-04 11:29:03 -0800248 public Builder group(GroupId groupId) {
249 return add(Instructions.createGroup(groupId));
250 }
251
252 @Override
alshabib9af70072015-02-09 14:34:16 -0800253 public TrafficTreatment.Builder transition(FlowRule.Type type) {
254 return add(Instructions.transition(type));
255 }
256
257 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700258 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700259
Jonathan Hart86e59352014-10-22 10:42:16 -0700260 //If we are dropping should we just return an empty list?
alshabib1d4cace2014-09-13 19:16:26 -0700261 List<Instruction> instructions = new LinkedList<Instruction>();
262 instructions.addAll(modifications);
263 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700264 if (!drop) {
265 instructions.addAll(outputs);
266 }
alshabib1d4cace2014-09-13 19:16:26 -0700267
268 return new DefaultTrafficTreatment(instructions);
269 }
270
271 }
272
273}