blob: 3f435980c3d155dcdd28ad3a8cd3787994d6af8a [file] [log] [blame]
alshabib1d4cace2014-09-13 19:16:26 -07001package org.onlab.onos.net.flow;
2
alshabib010c31d2014-09-26 10:01:12 -07003import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -07004import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -07005import org.onlab.onos.net.flow.instructions.Instructions;
6import org.onlab.packet.IpPrefix;
7import org.onlab.packet.MacAddress;
8import org.onlab.packet.VlanId;
alshabib1d4cace2014-09-13 19:16:26 -07009import org.slf4j.Logger;
10
tom9a693fd2014-10-03 11:32:19 -070011import java.util.Collections;
12import java.util.LinkedList;
13import java.util.List;
14
15import static org.slf4j.LoggerFactory.getLogger;
16
17/**
18 * Default traffic treatment implementation.
19 */
alshabib7b2748f2014-09-16 20:21:11 -070020public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070021
22 private final List<Instruction> instructions;
23
tom9a693fd2014-10-03 11:32:19 -070024 /**
25 * Creates a new traffic treatment from the specified list of instructions.
26 *
27 * @param instructions treatment instructions
28 */
alshabib7b2748f2014-09-16 20:21:11 -070029 private DefaultTrafficTreatment(List<Instruction> instructions) {
alshabib1d4cace2014-09-13 19:16:26 -070030 this.instructions = Collections.unmodifiableList(instructions);
31 }
32
33 @Override
34 public List<Instruction> instructions() {
35 return instructions;
36 }
37
38 /**
tom9a693fd2014-10-03 11:32:19 -070039 * Returns a new traffic treatment builder.
40 *
41 * @return traffic treatment builder
42 */
43 public static TrafficTreatment.Builder builder() {
44 return new Builder();
45 }
46
47 /**
alshabib1d4cace2014-09-13 19:16:26 -070048 * Builds a list of treatments following the following order.
49 * Modifications -> Group -> Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -070050 */
tom9a693fd2014-10-03 11:32:19 -070051 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -070052
53 private final Logger log = getLogger(getClass());
54
alshabib030111e2014-09-15 15:56:42 -070055 boolean drop = false;
56
57 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070058
59 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -070060 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070061
62 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -070063 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -070064
tom9a693fd2014-10-03 11:32:19 -070065 // Creates a new builder
66 private Builder() {
67 }
68
alshabib1d4cace2014-09-13 19:16:26 -070069 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -070070 if (drop) {
71 return this;
72 }
alshabib1d4cace2014-09-13 19:16:26 -070073 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -070074 case DROP:
75 drop = true;
76 break;
77 case OUTPUT:
78 outputs.add(instruction);
79 break;
80 case L2MODIFICATION:
81 case L3MODIFICATION:
82 // TODO: enforce modification order if any
83 modifications.add(instruction);
84 break;
85 case GROUP:
86 groups.add(instruction);
87 break;
88 default:
89 log.warn("Unknown instruction type {}", instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -070090 }
91 return this;
92 }
93
94 @Override
alshabib010c31d2014-09-26 10:01:12 -070095 public void drop() {
96 add(Instructions.createDrop());
97 }
98
99 @Override
100 public Builder setOutput(PortNumber number) {
101 return add(Instructions.createOutput(number));
102 }
103
104 @Override
105 public Builder setEthSrc(MacAddress addr) {
106 return add(Instructions.modL2Src(addr));
107 }
108
109 @Override
110 public Builder setEthDst(MacAddress addr) {
111 return add(Instructions.modL2Dst(addr));
112 }
113
114 @Override
115 public Builder setVlanId(VlanId id) {
116 return add(Instructions.modVlanId(id));
117 }
118
119 @Override
120 public Builder setVlanPcp(Byte pcp) {
121 return add(Instructions.modVlanPcp(pcp));
122 }
123
124 @Override
125 public Builder setIpSrc(IpPrefix addr) {
126 return add(Instructions.modL3Src(addr));
127 }
128
129 @Override
130 public Builder setIpDst(IpPrefix addr) {
131 return add(Instructions.modL3Dst(addr));
132 }
133
134 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700135 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700136
137 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700138 List<Instruction> instructions = new LinkedList<Instruction>();
139 instructions.addAll(modifications);
140 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700141 if (!drop) {
142 instructions.addAll(outputs);
143 }
alshabib1d4cace2014-09-13 19:16:26 -0700144
145 return new DefaultTrafficTreatment(instructions);
146 }
147
148 }
149
150}