blob: 0bf4ea80d5ac1a852b7f64a5df809f4d6ae76fb7 [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
12@SuppressWarnings("rawtypes")
13public class DefaultTrafficTreatment implements TrafficTreatment {
14
15 private final List<Instruction> instructions;
16
17 public DefaultTrafficTreatment(List<Instruction> instructions) {
18 this.instructions = Collections.unmodifiableList(instructions);
19 }
20
21 @Override
22 public List<Instruction> instructions() {
23 return instructions;
24 }
25
26 /**
27 * Builds a list of treatments following the following order.
28 * Modifications -> Group -> Output (including drop)
29 *
30 */
31
32 public static class Builder implements TrafficTreatment.Builder {
33
34 private final Logger log = getLogger(getClass());
35
alshabib030111e2014-09-15 15:56:42 -070036 boolean drop = false;
37
38 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070039
40 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070041 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070042
43 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070044 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070045
46
alshabib1d4cace2014-09-13 19:16:26 -070047 @Override
48 public Builder add(Instruction instruction) {
49 switch (instruction.type()) {
alshabib1d4cace2014-09-13 19:16:26 -070050 case DROP:
alshabib030111e2014-09-15 15:56:42 -070051 drop = true;
52 break;
53 case OUTPUT:
alshabib1d4cace2014-09-13 19:16:26 -070054 outputs.add(instruction);
55 break;
alshabib35edb1a2014-09-16 17:44:44 -070056 case L2MODIFICATION:
57 case L3MODIFICATION:
alshabib1d4cace2014-09-13 19:16:26 -070058 // TODO: enforce modification order if any
59 modifications.add(instruction);
alshabib030111e2014-09-15 15:56:42 -070060 break;
alshabib1d4cace2014-09-13 19:16:26 -070061 case GROUP:
62 groups.add(instruction);
alshabib030111e2014-09-15 15:56:42 -070063 break;
alshabib1d4cace2014-09-13 19:16:26 -070064 default:
65 log.warn("Unknown instruction type {}", instruction.type());
66 }
67 return this;
68 }
69
70 @Override
71 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -070072
73 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -070074 List<Instruction> instructions = new LinkedList<Instruction>();
75 instructions.addAll(modifications);
76 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -070077 if (!drop) {
78 instructions.addAll(outputs);
79 }
alshabib1d4cace2014-09-13 19:16:26 -070080
81 return new DefaultTrafficTreatment(instructions);
82 }
83
84 }
85
86}