blob: 06c49baf89df52c53d286580593ee6ddc6bf68c8 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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;
alshabib7b808c52015-06-26 14:22:24 -070021import org.onlab.packet.EthType;
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;
Ray Milkey85571eb2015-06-25 09:45:46 -070027import org.onosproject.net.IndexedLambda;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080028import org.onosproject.net.PortNumber;
29import org.onosproject.net.flow.instructions.Instruction;
30import org.onosproject.net.flow.instructions.Instructions;
tom1679e182014-10-09 13:50:45 -070031
Brian O'Connor6b528132015-03-10 16:39:52 -070032import java.util.Collections;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080033import java.util.List;
34import java.util.Objects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070035
Ray Milkey42507352015-03-20 15:16:10 -070036import static com.google.common.base.Preconditions.checkNotNull;
37
tom9a693fd2014-10-03 11:32:19 -070038/**
39 * Default traffic treatment implementation.
40 */
alshabib7b2748f2014-09-16 20:21:11 -070041public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070042
alshabib346b5b32015-03-06 00:42:16 -080043 private final List<Instruction> immediate;
44 private final List<Instruction> deferred;
Jonathan Hart4a0ba562015-03-23 17:23:33 -070045 private final List<Instruction> all;
alshabib346b5b32015-03-06 00:42:16 -080046 private final Instructions.TableTypeTransition table;
Saurav Das86af8f12015-05-25 23:55:33 -070047 private final Instructions.MetadataInstruction meta;
alshabib346b5b32015-03-06 00:42:16 -080048
49 private final boolean hasClear;
alshabib1d4cace2014-09-13 19:16:26 -070050
Brian O'Connor6b528132015-03-10 16:39:52 -070051 private static final DefaultTrafficTreatment EMPTY
52 = new DefaultTrafficTreatment(Collections.emptyList());
53
tom9a693fd2014-10-03 11:32:19 -070054 /**
55 * Creates a new traffic treatment from the specified list of instructions.
56 *
Jonathan Hart4a0ba562015-03-23 17:23:33 -070057 * @param immediate immediate instructions
tom9a693fd2014-10-03 11:32:19 -070058 */
Jonathan Hart4a0ba562015-03-23 17:23:33 -070059 private DefaultTrafficTreatment(List<Instruction> immediate) {
60 this.immediate = ImmutableList.copyOf(checkNotNull(immediate));
alshabib346b5b32015-03-06 00:42:16 -080061 this.deferred = ImmutableList.of();
Jonathan Hart4a0ba562015-03-23 17:23:33 -070062 this.all = this.immediate;
alshabib346b5b32015-03-06 00:42:16 -080063 this.hasClear = false;
64 this.table = null;
Saurav Das86af8f12015-05-25 23:55:33 -070065 this.meta = null;
alshabib346b5b32015-03-06 00:42:16 -080066 }
67
Jonathan Hart4a0ba562015-03-23 17:23:33 -070068 /**
69 * Creates a new traffic treatment from the specified list of instructions.
70 *
71 * @param deferred deferred instructions
72 * @param immediate immediate instructions
73 * @param table table transition instruction
74 * @param clear instruction to clear the deferred actions list
75 */
alshabib346b5b32015-03-06 00:42:16 -080076 private DefaultTrafficTreatment(List<Instruction> deferred,
77 List<Instruction> immediate,
78 Instructions.TableTypeTransition table,
Saurav Das86af8f12015-05-25 23:55:33 -070079 boolean clear,
80 Instructions.MetadataInstruction meta) {
Ray Milkey42507352015-03-20 15:16:10 -070081 this.immediate = ImmutableList.copyOf(checkNotNull(immediate));
82 this.deferred = ImmutableList.copyOf(checkNotNull(deferred));
Sho SHIMIZUd4fa7fd2015-06-30 18:56:05 -070083 this.all = new ImmutableList.Builder<Instruction>()
84 .addAll(immediate)
85 .addAll(deferred)
86 .build();
alshabib346b5b32015-03-06 00:42:16 -080087 this.table = table;
Saurav Das86af8f12015-05-25 23:55:33 -070088 this.meta = meta;
alshabib346b5b32015-03-06 00:42:16 -080089 this.hasClear = clear;
alshabib1d4cace2014-09-13 19:16:26 -070090 }
91
92 @Override
alshabib346b5b32015-03-06 00:42:16 -080093 public List<Instruction> deferred() {
94 return deferred;
95 }
96
97 @Override
98 public List<Instruction> immediate() {
99 return immediate;
100 }
101
102 @Override
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700103 public List<Instruction> allInstructions() {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700104 return all;
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700105 }
106
107 @Override
alshabib346b5b32015-03-06 00:42:16 -0800108 public Instructions.TableTypeTransition tableTransition() {
109 return table;
110 }
111
112 @Override
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700113 public boolean clearedDeferred() {
alshabib346b5b32015-03-06 00:42:16 -0800114 return hasClear;
alshabib1d4cace2014-09-13 19:16:26 -0700115 }
116
Saurav Das86af8f12015-05-25 23:55:33 -0700117 @Override
118 public Instructions.MetadataInstruction writeMetadata() {
119 return meta;
120 }
121
alshabib1d4cace2014-09-13 19:16:26 -0700122 /**
tom9a693fd2014-10-03 11:32:19 -0700123 * Returns a new traffic treatment builder.
124 *
125 * @return traffic treatment builder
126 */
127 public static TrafficTreatment.Builder builder() {
128 return new Builder();
129 }
130
Jonathan Hart6e88c682014-10-21 17:05:25 -0700131 /**
Brian O'Connor6b528132015-03-10 16:39:52 -0700132 * Returns an empty traffic treatment.
133 *
134 * @return empty traffic treatment
135 */
136 public static TrafficTreatment emptyTreatment() {
137 return EMPTY;
138 }
139
140 /**
Jonathan Hart6e88c682014-10-21 17:05:25 -0700141 * Returns a new traffic treatment builder primed to produce entities
142 * patterned after the supplied treatment.
143 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -0800144 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -0700145 * @return traffic treatment builder
146 */
147 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
148 return new Builder(treatment);
149 }
150
alshabib8ca53902014-10-07 13:11:17 -0700151 //FIXME: Order of instructions may affect hashcode
152 @Override
153 public int hashCode() {
Saurav Das86af8f12015-05-25 23:55:33 -0700154 return Objects.hash(immediate, deferred, table, meta);
alshabib8ca53902014-10-07 13:11:17 -0700155 }
156
157 @Override
158 public boolean equals(Object obj) {
159 if (this == obj) {
160 return true;
161 }
162 if (obj instanceof DefaultTrafficTreatment) {
163 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
alshabib346b5b32015-03-06 00:42:16 -0800164 return Objects.equals(immediate, that.immediate) &&
165 Objects.equals(deferred, that.deferred) &&
Saurav Das86af8f12015-05-25 23:55:33 -0700166 Objects.equals(table, that.table) &&
167 Objects.equals(meta, that.meta);
alshabib8ca53902014-10-07 13:11:17 -0700168
169 }
170 return false;
171 }
172
Jonathan Hartd87aeca2014-10-21 10:42:52 -0700173 @Override
174 public String toString() {
175 return MoreObjects.toStringHelper(getClass())
alshabib346b5b32015-03-06 00:42:16 -0800176 .add("immediate", immediate)
177 .add("deferred", deferred)
178 .add("transition", table == null ? "None" : table.toString())
179 .add("cleared", hasClear)
Saurav Das86af8f12015-05-25 23:55:33 -0700180 .add("metadata", meta)
Jonathan Hartd87aeca2014-10-21 10:42:52 -0700181 .toString();
182 }
183
tom9a693fd2014-10-03 11:32:19 -0700184 /**
alshabib1d4cace2014-09-13 19:16:26 -0700185 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700186 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700187 */
tom9a693fd2014-10-03 11:32:19 -0700188 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700189
alshabib346b5b32015-03-06 00:42:16 -0800190 boolean clear = false;
alshabib1d4cace2014-09-13 19:16:26 -0700191
alshabib346b5b32015-03-06 00:42:16 -0800192 Instructions.TableTypeTransition table;
alshabib1d4cace2014-09-13 19:16:26 -0700193
Saurav Das86af8f12015-05-25 23:55:33 -0700194 Instructions.MetadataInstruction meta;
195
alshabib346b5b32015-03-06 00:42:16 -0800196 List<Instruction> deferred = Lists.newLinkedList();
197
198 List<Instruction> immediate = Lists.newLinkedList();
199
200 List<Instruction> current = immediate;
alshabib1d4cace2014-09-13 19:16:26 -0700201
tom9a693fd2014-10-03 11:32:19 -0700202 // Creates a new builder
203 private Builder() {
204 }
205
Jonathan Hart6e88c682014-10-21 17:05:25 -0700206 // Creates a new builder based off an existing treatment
207 private Builder(TrafficTreatment treatment) {
Jonathan Hart4a0ba562015-03-23 17:23:33 -0700208 deferred();
209 treatment.deferred().forEach(i -> add(i));
210
211 immediate();
212 treatment.immediate().forEach(i -> add(i));
213
214 clear = treatment.clearedDeferred();
Jonathan Hart6e88c682014-10-21 17:05:25 -0700215 }
216
alshabib8ca53902014-10-07 13:11:17 -0700217 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700218 public Builder add(Instruction instruction) {
alshabib346b5b32015-03-06 00:42:16 -0800219
alshabib1d4cace2014-09-13 19:16:26 -0700220 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700221 case DROP:
tom9a693fd2014-10-03 11:32:19 -0700222 case OUTPUT:
alshabib346b5b32015-03-06 00:42:16 -0800223 case GROUP:
Marc De Leenheer49087752014-10-23 13:54:09 -0700224 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700225 case L2MODIFICATION:
226 case L3MODIFICATION:
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700227 case L4MODIFICATION:
alshabib346b5b32015-03-06 00:42:16 -0800228 current.add(instruction);
tom9a693fd2014-10-03 11:32:19 -0700229 break;
alshabib346b5b32015-03-06 00:42:16 -0800230 case TABLE:
231 table = (Instructions.TableTypeTransition) instruction;
tom9a693fd2014-10-03 11:32:19 -0700232 break;
Saurav Das86af8f12015-05-25 23:55:33 -0700233 case METADATA:
234 meta = (Instructions.MetadataInstruction) instruction;
235 break;
tom9a693fd2014-10-03 11:32:19 -0700236 default:
tom1679e182014-10-09 13:50:45 -0700237 throw new IllegalArgumentException("Unknown instruction type: " +
238 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700239 }
alshabib346b5b32015-03-06 00:42:16 -0800240
alshabib1d4cace2014-09-13 19:16:26 -0700241 return this;
242 }
243
244 @Override
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800245 public Builder drop() {
246 return add(Instructions.createDrop());
247 }
248
249 @Override
250 public Builder punt() {
251 return add(Instructions.createOutput(PortNumber.CONTROLLER));
alshabib010c31d2014-09-26 10:01:12 -0700252 }
253
254 @Override
255 public Builder setOutput(PortNumber number) {
256 return add(Instructions.createOutput(number));
257 }
258
259 @Override
260 public Builder setEthSrc(MacAddress addr) {
261 return add(Instructions.modL2Src(addr));
262 }
263
264 @Override
265 public Builder setEthDst(MacAddress addr) {
266 return add(Instructions.modL2Dst(addr));
267 }
268
269 @Override
270 public Builder setVlanId(VlanId id) {
271 return add(Instructions.modVlanId(id));
272 }
273
274 @Override
275 public Builder setVlanPcp(Byte pcp) {
276 return add(Instructions.modVlanPcp(pcp));
277 }
278
279 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700280 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700281 return add(Instructions.modL3Src(addr));
282 }
283
284 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700285 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700286 return add(Instructions.modL3Dst(addr));
287 }
288
289 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800290 public Builder decNwTtl() {
291 return add(Instructions.decNwTtl());
292 }
293
294 @Override
295 public Builder copyTtlIn() {
296 return add(Instructions.copyTtlIn());
297 }
298
299 @Override
300 public Builder copyTtlOut() {
301 return add(Instructions.copyTtlOut());
302 }
303
304 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800305 public Builder pushMpls() {
306 return add(Instructions.pushMpls());
307 }
308
309 @Override
310 public Builder popMpls() {
311 return add(Instructions.popMpls());
312 }
313
sangho3f97a17d2015-01-29 22:56:29 -0800314 @Override
alshabib0ad43982015-05-07 13:43:13 -0700315 public Builder popMpls(int etherType) {
alshabib7b808c52015-06-26 14:22:24 -0700316 return add(Instructions.popMpls(new EthType(etherType)));
317 }
318
319 @Override
320 public Builder popMpls(EthType etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800321 return add(Instructions.popMpls(etherType));
322 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800323
324 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100325 public Builder setMpls(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800326 return add(Instructions.modMplsLabel(mplsLabel));
327 }
328
329 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800330 public Builder decMplsTtl() {
331 return add(Instructions.decMplsTtl());
332 }
333
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700334 @Deprecated
sangho3f97a17d2015-01-29 22:56:29 -0800335 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700336 public Builder setLambda(short lambda) {
Ray Milkey85571eb2015-06-25 09:45:46 -0700337 return add(Instructions.modL0Lambda(new IndexedLambda(lambda)));
Marc De Leenheer49087752014-10-23 13:54:09 -0700338 }
339
340 @Override
sangho8995ac52015-02-04 11:29:03 -0800341 public Builder group(GroupId groupId) {
342 return add(Instructions.createGroup(groupId));
343 }
344
345 @Override
Saurav Dasfbe25c52015-03-04 11:12:00 -0800346 public Builder popVlan() {
347 return add(Instructions.popVlan());
348 }
349
350 @Override
Jonathan Hart54b406b2015-03-06 16:24:14 -0800351 public Builder pushVlan() {
352 return add(Instructions.pushVlan());
353 }
354
355 @Override
alshabib346b5b32015-03-06 00:42:16 -0800356 public Builder transition(FlowRule.Type type) {
alshabibd17abc22015-04-21 18:26:35 -0700357 return add(Instructions.transition(type.ordinal()));
358 }
359
360 @Override
361 public Builder transition(Integer tableId) {
362 return add(Instructions.transition(tableId));
alshabib346b5b32015-03-06 00:42:16 -0800363 }
364
365 @Override
366 public Builder immediate() {
367 current = immediate;
368 return this;
369 }
370
371 @Override
372 public Builder deferred() {
373 current = deferred;
374 return this;
375 }
376
377 @Override
378 public Builder wipeDeferred() {
379 clear = true;
380 return this;
381 }
382
383 @Override
Saurav Das86af8f12015-05-25 23:55:33 -0700384 public Builder writeMetadata(long metadata, long metadataMask) {
385 return add(Instructions.writeMetadata(metadata, metadataMask));
386 }
387
388 @Override
Hyunsun Moona08c5d02015-07-14 17:53:00 -0700389 public Builder setTunnelId(long tunnelId) {
390 return add(Instructions.modTunnelId(tunnelId));
391 }
392
393 @Override
Hyunsun Moonc8bd97c2015-07-18 22:47:33 -0700394 public TrafficTreatment.Builder setTcpSrc(short port) {
395 return add(Instructions.modTcpSrc(port));
396 }
397
398 @Override
399 public TrafficTreatment.Builder setTcpDst(short port) {
400 return add(Instructions.modTcpDst(port));
401 }
402
403 @Override
404 public TrafficTreatment.Builder setUdpSrc(short port) {
405 return add(Instructions.modUdpSrc(port));
406 }
407
408 @Override
409 public TrafficTreatment.Builder setUdpDst(short port) {
410 return add(Instructions.modUdpDst(port));
411 }
412
413 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700414 public TrafficTreatment build() {
Srikanth Vavilapallif5b234a2015-04-21 13:04:13 -0700415 //Don't add DROP instruction by default when instruction
416 //set is empty. This will be handled in DefaultSingleTablePipeline
417 //driver.
418
419 //if (deferred.size() == 0 && immediate.size() == 0
420 // && table == null && !clear) {
421 // drop();
422 //}
Saurav Das86af8f12015-05-25 23:55:33 -0700423 return new DefaultTrafficTreatment(deferred, immediate, table, clear, meta);
alshabib1d4cace2014-09-13 19:16:26 -0700424 }
425
426 }
427
428}