blob: 6064263967e2e341f1c21bfb6e1ba841a2b1f446 [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;
56 case MODIFICATION:
57 // 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}