blob: 37dcd06eaa001931ce6dc5501dcf5c64d233f6a9 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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;
Michele Santuari4b6019e2014-12-19 11:31:45 +010020
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070021import org.onlab.packet.IpAddress;
alshabib010c31d2014-09-26 10:01:12 -070022import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onlab.packet.MplsLabel;
alshabib010c31d2014-09-26 10:01:12 -070024import org.onlab.packet.VlanId;
sangho8995ac52015-02-04 11:29:03 -080025import org.onosproject.core.GroupId;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080026import org.onosproject.net.PortNumber;
27import org.onosproject.net.flow.instructions.Instruction;
28import org.onosproject.net.flow.instructions.Instructions;
tom1679e182014-10-09 13:50:45 -070029
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080030import java.util.LinkedList;
31import java.util.List;
32import java.util.Objects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070033
tom9a693fd2014-10-03 11:32:19 -070034/**
35 * Default traffic treatment implementation.
36 */
alshabib7b2748f2014-09-16 20:21:11 -070037public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070038
39 private final List<Instruction> instructions;
40
tom9a693fd2014-10-03 11:32:19 -070041 /**
42 * Creates a new traffic treatment from the specified list of instructions.
43 *
44 * @param instructions treatment instructions
45 */
alshabib7b2748f2014-09-16 20:21:11 -070046 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070047 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070048 }
49
50 @Override
51 public List<Instruction> instructions() {
52 return instructions;
53 }
54
55 /**
tom9a693fd2014-10-03 11:32:19 -070056 * Returns a new traffic treatment builder.
57 *
58 * @return traffic treatment builder
59 */
60 public static TrafficTreatment.Builder builder() {
61 return new Builder();
62 }
63
Jonathan Hart6e88c682014-10-21 17:05:25 -070064 /**
65 * Returns a new traffic treatment builder primed to produce entities
66 * patterned after the supplied treatment.
67 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080068 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -070069 * @return traffic treatment builder
70 */
71 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
72 return new Builder(treatment);
73 }
74
alshabib8ca53902014-10-07 13:11:17 -070075 //FIXME: Order of instructions may affect hashcode
76 @Override
77 public int hashCode() {
78 return Objects.hash(instructions);
79 }
80
81 @Override
82 public boolean equals(Object obj) {
83 if (this == obj) {
84 return true;
85 }
86 if (obj instanceof DefaultTrafficTreatment) {
87 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
tom1679e182014-10-09 13:50:45 -070088 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070089
90 }
91 return false;
92 }
93
Jonathan Hartd87aeca2014-10-21 10:42:52 -070094 @Override
95 public String toString() {
96 return MoreObjects.toStringHelper(getClass())
97 .add("instructions", instructions)
98 .toString();
99 }
100
tom9a693fd2014-10-03 11:32:19 -0700101 /**
alshabib1d4cace2014-09-13 19:16:26 -0700102 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700103 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700104 */
tom9a693fd2014-10-03 11:32:19 -0700105 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700106
alshabib030111e2014-09-15 15:56:42 -0700107 boolean drop = false;
108
109 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700110
111 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -0700112 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700113
114 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -0700115 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700116
tom9a693fd2014-10-03 11:32:19 -0700117 // Creates a new builder
118 private Builder() {
119 }
120
Jonathan Hart6e88c682014-10-21 17:05:25 -0700121 // Creates a new builder based off an existing treatment
122 private Builder(TrafficTreatment treatment) {
123 for (Instruction instruction : treatment.instructions()) {
124 add(instruction);
125 }
126 }
127
alshabib8ca53902014-10-07 13:11:17 -0700128 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700129 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -0700130 if (drop) {
131 return this;
132 }
alshabib1d4cace2014-09-13 19:16:26 -0700133 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700134 case DROP:
135 drop = true;
136 break;
137 case OUTPUT:
138 outputs.add(instruction);
139 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700140 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700141 case L2MODIFICATION:
142 case L3MODIFICATION:
143 // TODO: enforce modification order if any
144 modifications.add(instruction);
145 break;
146 case GROUP:
147 groups.add(instruction);
148 break;
149 default:
tom1679e182014-10-09 13:50:45 -0700150 throw new IllegalArgumentException("Unknown instruction type: " +
151 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700152 }
153 return this;
154 }
155
156 @Override
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800157 public Builder drop() {
158 return add(Instructions.createDrop());
159 }
160
161 @Override
162 public Builder punt() {
163 return add(Instructions.createOutput(PortNumber.CONTROLLER));
alshabib010c31d2014-09-26 10:01:12 -0700164 }
165
166 @Override
167 public Builder setOutput(PortNumber number) {
168 return add(Instructions.createOutput(number));
169 }
170
171 @Override
172 public Builder setEthSrc(MacAddress addr) {
173 return add(Instructions.modL2Src(addr));
174 }
175
176 @Override
177 public Builder setEthDst(MacAddress addr) {
178 return add(Instructions.modL2Dst(addr));
179 }
180
181 @Override
182 public Builder setVlanId(VlanId id) {
183 return add(Instructions.modVlanId(id));
184 }
185
186 @Override
187 public Builder setVlanPcp(Byte pcp) {
188 return add(Instructions.modVlanPcp(pcp));
189 }
190
191 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700192 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700193 return add(Instructions.modL3Src(addr));
194 }
195
196 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700197 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700198 return add(Instructions.modL3Dst(addr));
199 }
200
201 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800202 public Builder decNwTtl() {
203 return add(Instructions.decNwTtl());
204 }
205
206 @Override
207 public Builder copyTtlIn() {
208 return add(Instructions.copyTtlIn());
209 }
210
211 @Override
212 public Builder copyTtlOut() {
213 return add(Instructions.copyTtlOut());
214 }
215
216 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800217 public Builder pushMpls() {
218 return add(Instructions.pushMpls());
219 }
220
221 @Override
222 public Builder popMpls() {
223 return add(Instructions.popMpls());
224 }
225
sangho3f97a17d2015-01-29 22:56:29 -0800226 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100227 public Builder popMpls(Short etherType) {
sangho3f97a17d2015-01-29 22:56:29 -0800228 return add(Instructions.popMpls(etherType));
229 }
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800230
231 @Override
Michele Santuari4b6019e2014-12-19 11:31:45 +0100232 public Builder setMpls(MplsLabel mplsLabel) {
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800233 return add(Instructions.modMplsLabel(mplsLabel));
234 }
235
236 @Override
sangho3f97a17d2015-01-29 22:56:29 -0800237 public Builder decMplsTtl() {
238 return add(Instructions.decMplsTtl());
239 }
240
241 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700242 public Builder setLambda(short lambda) {
243 return add(Instructions.modL0Lambda(lambda));
244 }
245
246 @Override
sangho8995ac52015-02-04 11:29:03 -0800247 public Builder group(GroupId groupId) {
248 return add(Instructions.createGroup(groupId));
249 }
250
251 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700252 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700253
Jonathan Hart86e59352014-10-22 10:42:16 -0700254 //If we are dropping should we just return an empty list?
alshabib1d4cace2014-09-13 19:16:26 -0700255 List<Instruction> instructions = new LinkedList<Instruction>();
256 instructions.addAll(modifications);
257 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700258 if (!drop) {
259 instructions.addAll(outputs);
260 }
alshabib1d4cace2014-09-13 19:16:26 -0700261
262 return new DefaultTrafficTreatment(instructions);
263 }
264
265 }
266
267}