blob: 54c09721ad15dd9854dc60b12ce7aa5212e91186 [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 */
alshabib1d4cace2014-09-13 19:16:26 -070016package org.onlab.onos.net.flow;
17
Jonathan Hartd87aeca2014-10-21 10:42:52 -070018import java.util.LinkedList;
19import java.util.List;
20import java.util.Objects;
21
alshabib010c31d2014-09-26 10:01:12 -070022import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -070023import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -070024import org.onlab.onos.net.flow.instructions.Instructions;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070025import org.onlab.packet.IpAddress;
alshabib010c31d2014-09-26 10:01:12 -070026import org.onlab.packet.MacAddress;
27import org.onlab.packet.VlanId;
tom1679e182014-10-09 13:50:45 -070028
Jonathan Hartd87aeca2014-10-21 10:42:52 -070029import com.google.common.base.MoreObjects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070030import com.google.common.collect.ImmutableList;
31
tom9a693fd2014-10-03 11:32:19 -070032/**
33 * Default traffic treatment implementation.
34 */
alshabib7b2748f2014-09-16 20:21:11 -070035public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070036
37 private final List<Instruction> instructions;
38
tom9a693fd2014-10-03 11:32:19 -070039 /**
40 * Creates a new traffic treatment from the specified list of instructions.
41 *
42 * @param instructions treatment instructions
43 */
alshabib7b2748f2014-09-16 20:21:11 -070044 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070045 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070046 }
47
48 @Override
49 public List<Instruction> instructions() {
50 return instructions;
51 }
52
53 /**
tom9a693fd2014-10-03 11:32:19 -070054 * Returns a new traffic treatment builder.
55 *
56 * @return traffic treatment builder
57 */
58 public static TrafficTreatment.Builder builder() {
59 return new Builder();
60 }
61
Jonathan Hart6e88c682014-10-21 17:05:25 -070062 /**
63 * Returns a new traffic treatment builder primed to produce entities
64 * patterned after the supplied treatment.
65 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080066 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -070067 * @return traffic treatment builder
68 */
69 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
70 return new Builder(treatment);
71 }
72
alshabib8ca53902014-10-07 13:11:17 -070073 //FIXME: Order of instructions may affect hashcode
74 @Override
75 public int hashCode() {
76 return Objects.hash(instructions);
77 }
78
79 @Override
80 public boolean equals(Object obj) {
81 if (this == obj) {
82 return true;
83 }
84 if (obj instanceof DefaultTrafficTreatment) {
85 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
tom1679e182014-10-09 13:50:45 -070086 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070087
88 }
89 return false;
90 }
91
Jonathan Hartd87aeca2014-10-21 10:42:52 -070092 @Override
93 public String toString() {
94 return MoreObjects.toStringHelper(getClass())
95 .add("instructions", instructions)
96 .toString();
97 }
98
tom9a693fd2014-10-03 11:32:19 -070099 /**
alshabib1d4cace2014-09-13 19:16:26 -0700100 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700101 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700102 */
tom9a693fd2014-10-03 11:32:19 -0700103 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700104
alshabib030111e2014-09-15 15:56:42 -0700105 boolean drop = false;
106
107 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700108
109 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -0700110 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700111
112 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -0700113 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700114
tom9a693fd2014-10-03 11:32:19 -0700115 // Creates a new builder
116 private Builder() {
117 }
118
Jonathan Hart6e88c682014-10-21 17:05:25 -0700119 // Creates a new builder based off an existing treatment
120 private Builder(TrafficTreatment treatment) {
121 for (Instruction instruction : treatment.instructions()) {
122 add(instruction);
123 }
124 }
125
alshabib8ca53902014-10-07 13:11:17 -0700126 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700127 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -0700128 if (drop) {
129 return this;
130 }
alshabib1d4cace2014-09-13 19:16:26 -0700131 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700132 case DROP:
133 drop = true;
134 break;
135 case OUTPUT:
136 outputs.add(instruction);
137 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700138 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700139 case L2MODIFICATION:
140 case L3MODIFICATION:
141 // TODO: enforce modification order if any
142 modifications.add(instruction);
143 break;
144 case GROUP:
145 groups.add(instruction);
146 break;
147 default:
tom1679e182014-10-09 13:50:45 -0700148 throw new IllegalArgumentException("Unknown instruction type: " +
149 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700150 }
151 return this;
152 }
153
154 @Override
alshabib010c31d2014-09-26 10:01:12 -0700155 public void drop() {
156 add(Instructions.createDrop());
157 }
158
159 @Override
160 public Builder setOutput(PortNumber number) {
161 return add(Instructions.createOutput(number));
162 }
163
164 @Override
165 public Builder setEthSrc(MacAddress addr) {
166 return add(Instructions.modL2Src(addr));
167 }
168
169 @Override
170 public Builder setEthDst(MacAddress addr) {
171 return add(Instructions.modL2Dst(addr));
172 }
173
174 @Override
175 public Builder setVlanId(VlanId id) {
176 return add(Instructions.modVlanId(id));
177 }
178
179 @Override
180 public Builder setVlanPcp(Byte pcp) {
181 return add(Instructions.modVlanPcp(pcp));
182 }
183
184 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700185 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700186 return add(Instructions.modL3Src(addr));
187 }
188
189 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700190 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700191 return add(Instructions.modL3Dst(addr));
192 }
193
194 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800195 public Builder pushMpls() {
196 return add(Instructions.pushMpls());
197 }
198
199 @Override
200 public Builder popMpls() {
201 return add(Instructions.popMpls());
202 }
203
204
205 @Override
206 public Builder setMpls(Integer mplsLabel) {
207 return add(Instructions.modMplsLabel(mplsLabel));
208 }
209
210 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700211 public Builder setLambda(short lambda) {
212 return add(Instructions.modL0Lambda(lambda));
213 }
214
215 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700216 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700217
Jonathan Hart86e59352014-10-22 10:42:16 -0700218 //If we are dropping should we just return an empty list?
alshabib1d4cace2014-09-13 19:16:26 -0700219 List<Instruction> instructions = new LinkedList<Instruction>();
220 instructions.addAll(modifications);
221 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700222 if (!drop) {
223 instructions.addAll(outputs);
224 }
alshabib1d4cace2014-09-13 19:16:26 -0700225
226 return new DefaultTrafficTreatment(instructions);
227 }
228
229 }
230
231}