blob: 5b2343eaf7b9a7936ff909f1dc7647b3e58fa554 [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;
alshabib346b5b32015-03-06 00:42:16 -080020import com.google.common.collect.Lists;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070021import org.apache.commons.collections.ListUtils;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070022import org.onlab.packet.IpAddress;
alshabib010c31d2014-09-26 10:01:12 -070023import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010024import org.onlab.packet.MplsLabel;
alshabib010c31d2014-09-26 10:01:12 -070025import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080026import org.onosproject.core.GroupId;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080027import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.instructions.Instruction;
29import org.onosproject.net.flow.instructions.Instructions;
tom1679e182014-10-09 13:50:45 -070030
Brian O'Connor6b528132015-03-10 16:39:52 -070031import java.util.Collections;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080032import java.util.List;
33import java.util.Objects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070034
tom9a693fd2014-10-03 11:32:19 -070035/**
36 * Default traffic treatment implementation.
37 */
alshabib7b2748f2014-09-16 20:21:11 -070038public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070039
alshabib346b5b32015-03-06 00:42:16 -080040 private final List<Instruction> immediate;
41 private final List<Instruction> deferred;
42 private final Instructions.TableTypeTransition table;
43
44 private final boolean hasClear;
alshabib1d4cace2014-09-13 19:16:26 -070045
Brian O'Connor6b528132015-03-10 16:39:52 -070046 private static final DefaultTrafficTreatment EMPTY
47 = new DefaultTrafficTreatment(Collections.emptyList());
48
tom9a693fd2014-10-03 11:32:19 -070049 /**
50 * Creates a new traffic treatment from the specified list of instructions.
51 *
52 * @param instructions treatment instructions
53 */
alshabib7b2748f2014-09-16 20:21:11 -070054 private DefaultTrafficTreatment(List<Instruction> instructions) {
alshabib346b5b32015-03-06 00:42:16 -080055 this.immediate = ImmutableList.copyOf(instructions);
56 this.deferred = ImmutableList.of();
57 this.hasClear = false;
58 this.table = null;
59 }
60
61 private DefaultTrafficTreatment(List<Instruction> deferred,
62 List<Instruction> immediate,
63 Instructions.TableTypeTransition table,
64 boolean clear) {
65 this.immediate = ImmutableList.copyOf(immediate);
66 this.deferred = ImmutableList.copyOf(deferred);
67 this.table = table;
68 this.hasClear = clear;
69
alshabib1d4cace2014-09-13 19:16:26 -070070 }
71
72 @Override
73 public List<Instruction> instructions() {
alshabib346b5b32015-03-06 00:42:16 -080074 return immediate;
75 }
76
77 @Override
78 public List<Instruction> deferred() {
79 return deferred;
80 }
81
82 @Override
83 public List<Instruction> immediate() {
84 return immediate;
85 }
86
87 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070088 public List<Instruction> allInstructions() {
89 return ListUtils.union(immediate, deferred);
90 }
91
92 @Override
alshabib346b5b32015-03-06 00:42:16 -080093 public Instructions.TableTypeTransition tableTransition() {
94 return table;
95 }
96
97 @Override
98 public Boolean clearedDeferred() {
99 return hasClear;
alshabib1d4cace2014-09-13 19:16:26 -0700100 }
101
102 /**
tom9a693fd2014-10-03 11:32:19 -0700103 * Returns a new traffic treatment builder.
104 *
105 * @return traffic treatment builder
106 */
107 public static TrafficTreatment.Builder builder() {
108 return new Builder();
109 }
110
Jonathan Hart6e88c682014-10-21 17:05:25 -0700111 /**
Brian O'Connor6b528132015-03-10 16:39:52 -0700112 * Returns an empty traffic treatment.
113 *
114 * @return empty traffic treatment
115 */
116 public static TrafficTreatment emptyTreatment() {
117 return EMPTY;
118 }
119
120 /**
Jonathan Hart6e88c682014-10-21 17:05:25 -0700121 * Returns a new traffic treatment builder primed to produce entities
122 * patterned after the supplied treatment.
123 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800124 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -0700125 * @return traffic treatment builder
126 */
127 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
128 return new Builder(treatment);
129 }
130
alshabib8ca53902014-10-07 13:11:17 -0700131 //FIXME: Order of instructions may affect hashcode
132 @Override
133 public int hashCode() {
alshabib346b5b32015-03-06 00:42:16 -0800134 return Objects.hash(immediate, deferred, table);
alshabib8ca53902014-10-07 13:11:17 -0700135 }
136
137 @Override
138 public boolean equals(Object obj) {
139 if (this == obj) {
140 return true;
141 }
142 if (obj instanceof DefaultTrafficTreatment) {
143 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
alshabib346b5b32015-03-06 00:42:16 -0800144 return Objects.equals(immediate, that.immediate) &&
145 Objects.equals(deferred, that.deferred) &&
146 Objects.equals(table, that.table);
alshabib8ca53902014-10-07 13:11:17 -0700147
148 }
149 return false;
150 }
151
Jonathan Hartd87aeca2014-10-21 10:42:52 -0700152 @Override
153 public String toString() {
154 return MoreObjects.toStringHelper(getClass())
alshabib346b5b32015-03-06 00:42:16 -0800155 .add("immediate", immediate)
156 .add("deferred", deferred)
157 .add("transition", table == null ? "None" : table.toString())
158 .add("cleared", hasClear)
Jonathan Hartd87aeca2014-10-21 10:42:52 -0700159 .toString();
160 }
161
tom9a693fd2014-10-03 11:32:19 -0700162 /**
alshabib1d4cace2014-09-13 19:16:26 -0700163 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700164 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700165 */
tom9a693fd2014-10-03 11:32:19 -0700166 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700167
alshabib030111e2014-09-15 15:56:42 -0700168 boolean drop = false;
169
alshabib346b5b32015-03-06 00:42:16 -0800170 boolean clear = false;
alshabib1d4cace2014-09-13 19:16:26 -0700171
alshabib346b5b32015-03-06 00:42:16 -0800172 Instructions.TableTypeTransition table;
alshabib1d4cace2014-09-13 19:16:26 -0700173
alshabib346b5b32015-03-06 00:42:16 -0800174 List<Instruction> deferred = Lists.newLinkedList();
175
176 List<Instruction> immediate = Lists.newLinkedList();
177
178 List<Instruction> current = immediate;
alshabib1d4cace2014-09-13 19:16:26 -0700179
tom9a693fd2014-10-03 11:32:19 -0700180 // Creates a new builder
181 private Builder() {
182 }
183
Jonathan Hart6e88c682014-10-21 17:05:25 -0700184 // Creates a new builder based off an existing treatment
alshabib346b5b32015-03-06 00:42:16 -0800185 //FIXME only works for immediate instruction sets.
Jonathan Hart6e88c682014-10-21 17:05:25 -0700186 private Builder(TrafficTreatment treatment) {
187 for (Instruction instruction : treatment.instructions()) {
188 add(instruction);
189 }
190 }
191
alshabib8ca53902014-10-07 13:11:17 -0700192 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700193 public Builder add(Instruction instruction) {
alshabib346b5b32015-03-06 00:42:16 -0800194
alshabib1d4cace2014-09-13 19:16:26 -0700195 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700196 case DROP:
tom9a693fd2014-10-03 11:32:19 -0700197 case OUTPUT:
alshabib346b5b32015-03-06 00:42:16 -0800198 case GROUP:
Marc De Leenheer49087752014-10-23 13:54:09 -0700199 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700200 case L2MODIFICATION:
201 case L3MODIFICATION:
alshabib346b5b32015-03-06 00:42:16 -0800202 current.add(instruction);
tom9a693fd2014-10-03 11:32:19 -0700203 break;
alshabib346b5b32015-03-06 00:42:16 -0800204 case TABLE:
205 table = (Instructions.TableTypeTransition) instruction;
tom9a693fd2014-10-03 11:32:19 -0700206 break;
207 default:
tom1679e182014-10-09 13:50:45 -0700208 throw new IllegalArgumentException("Unknown instruction type: " +
209 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700210 }
alshabib346b5b32015-03-06 00:42:16 -0800211
alshabib1d4cace2014-09-13 19:16:26 -0700212 return this;
213 }
214
215 @Override
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800216 public Builder drop() {
217 return add(Instructions.createDrop());
218 }
219
220 @Override
221 public Builder punt() {
222 return add(Instructions.createOutput(PortNumber.CONTROLLER));
alshabib010c31d2014-09-26 10:01:12 -0700223 }
224
225 @Override
226 public Builder setOutput(PortNumber number) {
227 return add(Instructions.createOutput(number));
228 }
229
230 @Override
231 public Builder setEthSrc(MacAddress addr) {
232 return add(Instructions.modL2Src(addr));
233 }
234
235 @Override
236 public Builder setEthDst(MacAddress addr) {
237 return add(Instructions.modL2Dst(addr));
238 }
239
240 @Override
241 public Builder setVlanId(VlanId id) {
242 return add(Instructions.modVlanId(id));
243 }
244
245 @Override
246 public Builder setVlanPcp(Byte pcp) {
247 return add(Instructions.modVlanPcp(pcp));
248 }
249
250 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700251 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700252 return add(Instructions.modL3Src(addr));
253 }
254
255 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700256 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700257 return add(Instructions.modL3Dst(addr));
258 }
259
260 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800261 public Builder decNwTtl() {
262 return add(Instructions.decNwTtl());
263 }
264
265 @Override
266 public Builder copyTtlIn() {
267 return add(Instructions.copyTtlIn());
268 }
269
270 @Override
271 public Builder copyTtlOut() {
272 return add(Instructions.copyTtlOut());
273 }
274
275 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800276 public Builder pushMpls() {
277 return add(Instructions.pushMpls());
278 }
279
280 @Override
281 public Builder popMpls() {
282 return add(Instructions.popMpls());
283 }
284
sangho3f97a17d2015-01-29 22:56:29 -0800285 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100286 public Builder popMpls(Short etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800287 return add(Instructions.popMpls(etherType));
288 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800289
290 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100291 public Builder setMpls(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800292 return add(Instructions.modMplsLabel(mplsLabel));
293 }
294
295 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800296 public Builder decMplsTtl() {
297 return add(Instructions.decMplsTtl());
298 }
299
300 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700301 public Builder setLambda(short lambda) {
302 return add(Instructions.modL0Lambda(lambda));
303 }
304
305 @Override
sangho8995ac52015-02-04 11:29:03 -0800306 public Builder group(GroupId groupId) {
307 return add(Instructions.createGroup(groupId));
308 }
309
310 @Override
Saurav Dasfbe25c52015-03-04 11:12:00 -0800311 public Builder popVlan() {
312 return add(Instructions.popVlan());
313 }
314
315 @Override
Jonathan Hart54b406b2015-03-06 16:24:14 -0800316 public Builder pushVlan() {
317 return add(Instructions.pushVlan());
318 }
319
320 @Override
alshabib346b5b32015-03-06 00:42:16 -0800321 public Builder transition(FlowRule.Type type) {
322 return add(Instructions.transition(type));
323 }
324
325 @Override
326 public Builder immediate() {
327 current = immediate;
328 return this;
329 }
330
331 @Override
332 public Builder deferred() {
333 current = deferred;
334 return this;
335 }
336
337 @Override
338 public Builder wipeDeferred() {
339 clear = true;
340 return this;
341 }
342
343 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700344 public TrafficTreatment build() {
alshabib346b5b32015-03-06 00:42:16 -0800345 if (deferred.size() == 0 && immediate.size() == 0
346 && table == null && !clear) {
347 drop();
alshabib030111e2014-09-15 15:56:42 -0700348 }
alshabib346b5b32015-03-06 00:42:16 -0800349 return new DefaultTrafficTreatment(deferred, immediate, table, clear);
alshabib1d4cace2014-09-13 19:16:26 -0700350 }
351
352 }
353
354}