blob: 9de68dce4e20d000ca1a3f2a742dda5ed34f011c [file] [log] [blame]
alshabib1d4cace2014-09-13 19:16:26 -07001package org.onlab.onos.net.flow;
2
3import static org.slf4j.LoggerFactory.getLogger;
4
5import java.util.Collections;
6import java.util.LinkedList;
7import java.util.List;
8
alshabib55a55d92014-09-16 11:59:31 -07009import org.onlab.onos.net.flow.instructions.Instruction;
alshabib1d4cace2014-09-13 19:16:26 -070010import org.slf4j.Logger;
11
alshabib7b2748f2014-09-16 20:21:11 -070012public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070013
14 private final List<Instruction> instructions;
15
alshabib7b2748f2014-09-16 20:21:11 -070016 private DefaultTrafficTreatment(List<Instruction> instructions) {
alshabib1d4cace2014-09-13 19:16:26 -070017 this.instructions = Collections.unmodifiableList(instructions);
18 }
19
20 @Override
21 public List<Instruction> instructions() {
22 return instructions;
23 }
24
25 /**
26 * Builds a list of treatments following the following order.
27 * Modifications -> Group -> Output (including drop)
28 *
29 */
30
31 public static class Builder implements TrafficTreatment.Builder {
32
33 private final Logger log = getLogger(getClass());
34
alshabib030111e2014-09-15 15:56:42 -070035 boolean drop = false;
36
37 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070038
39 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070040 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070041
42 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070043 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070044
45
alshabib1d4cace2014-09-13 19:16:26 -070046 @Override
47 public Builder add(Instruction instruction) {
48 switch (instruction.type()) {
alshabib1d4cace2014-09-13 19:16:26 -070049 case DROP:
alshabib030111e2014-09-15 15:56:42 -070050 drop = true;
51 break;
52 case OUTPUT:
alshabib1d4cace2014-09-13 19:16:26 -070053 outputs.add(instruction);
54 break;
alshabib35edb1a2014-09-16 17:44:44 -070055 case L2MODIFICATION:
56 case L3MODIFICATION:
alshabib1d4cace2014-09-13 19:16:26 -070057 // TODO: enforce modification order if any
58 modifications.add(instruction);
alshabib030111e2014-09-15 15:56:42 -070059 break;
alshabib1d4cace2014-09-13 19:16:26 -070060 case GROUP:
61 groups.add(instruction);
alshabib030111e2014-09-15 15:56:42 -070062 break;
alshabib1d4cace2014-09-13 19:16:26 -070063 default:
64 log.warn("Unknown instruction type {}", instruction.type());
65 }
66 return this;
67 }
68
69 @Override
70 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -070071
72 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -070073 List<Instruction> instructions = new LinkedList<Instruction>();
74 instructions.addAll(modifications);
75 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -070076 if (!drop) {
77 instructions.addAll(outputs);
78 }
alshabib1d4cace2014-09-13 19:16:26 -070079
80 return new DefaultTrafficTreatment(instructions);
81 }
82
83 }
84
85}