blob: 638f00d5b7ae39029978a9333f10c785378ae7d0 [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;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070020import org.onlab.packet.IpAddress;
alshabib010c31d2014-09-26 10:01:12 -070021import org.onlab.packet.MacAddress;
22import org.onlab.packet.VlanId;
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080023import org.onosproject.net.PortNumber;
24import org.onosproject.net.flow.instructions.Instruction;
25import org.onosproject.net.flow.instructions.Instructions;
tom1679e182014-10-09 13:50:45 -070026
Thomas Vachuskaf4df0052015-01-06 12:30:11 -080027import java.util.LinkedList;
28import java.util.List;
29import java.util.Objects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070030
tom9a693fd2014-10-03 11:32:19 -070031/**
32 * Default traffic treatment implementation.
33 */
alshabib7b2748f2014-09-16 20:21:11 -070034public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070035
36 private final List<Instruction> instructions;
37
tom9a693fd2014-10-03 11:32:19 -070038 /**
39 * Creates a new traffic treatment from the specified list of instructions.
40 *
41 * @param instructions treatment instructions
42 */
alshabib7b2748f2014-09-16 20:21:11 -070043 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070044 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070045 }
46
47 @Override
48 public List<Instruction> instructions() {
49 return instructions;
50 }
51
52 /**
tom9a693fd2014-10-03 11:32:19 -070053 * Returns a new traffic treatment builder.
54 *
55 * @return traffic treatment builder
56 */
57 public static TrafficTreatment.Builder builder() {
58 return new Builder();
59 }
60
Jonathan Hart6e88c682014-10-21 17:05:25 -070061 /**
62 * Returns a new traffic treatment builder primed to produce entities
63 * patterned after the supplied treatment.
64 *
Yuta HIGUCHI5c947272014-11-03 21:39:21 -080065 * @param treatment base treatment
Jonathan Hart6e88c682014-10-21 17:05:25 -070066 * @return traffic treatment builder
67 */
68 public static TrafficTreatment.Builder builder(TrafficTreatment treatment) {
69 return new Builder(treatment);
70 }
71
alshabib8ca53902014-10-07 13:11:17 -070072 //FIXME: Order of instructions may affect hashcode
73 @Override
74 public int hashCode() {
75 return Objects.hash(instructions);
76 }
77
78 @Override
79 public boolean equals(Object obj) {
80 if (this == obj) {
81 return true;
82 }
83 if (obj instanceof DefaultTrafficTreatment) {
84 DefaultTrafficTreatment that = (DefaultTrafficTreatment) obj;
tom1679e182014-10-09 13:50:45 -070085 return Objects.equals(instructions, that.instructions);
alshabib8ca53902014-10-07 13:11:17 -070086
87 }
88 return false;
89 }
90
Jonathan Hartd87aeca2014-10-21 10:42:52 -070091 @Override
92 public String toString() {
93 return MoreObjects.toStringHelper(getClass())
94 .add("instructions", instructions)
95 .toString();
96 }
97
tom9a693fd2014-10-03 11:32:19 -070098 /**
alshabib1d4cace2014-09-13 19:16:26 -070099 * Builds a list of treatments following the following order.
Thomas Vachuska4b420772014-10-30 16:46:17 -0700100 * Modifications -&gt; Group -&gt; Output (including drop)
alshabib1d4cace2014-09-13 19:16:26 -0700101 */
tom9a693fd2014-10-03 11:32:19 -0700102 public static final class Builder implements TrafficTreatment.Builder {
alshabib1d4cace2014-09-13 19:16:26 -0700103
alshabib030111e2014-09-15 15:56:42 -0700104 boolean drop = false;
105
106 List<Instruction> outputs = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700107
108 // TODO: should be a list of instructions based on group objects
alshabib030111e2014-09-15 15:56:42 -0700109 List<Instruction> groups = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700110
111 // TODO: should be a list of instructions based on modification objects
alshabib030111e2014-09-15 15:56:42 -0700112 List<Instruction> modifications = new LinkedList<>();
alshabib1d4cace2014-09-13 19:16:26 -0700113
tom9a693fd2014-10-03 11:32:19 -0700114 // Creates a new builder
115 private Builder() {
116 }
117
Jonathan Hart6e88c682014-10-21 17:05:25 -0700118 // Creates a new builder based off an existing treatment
119 private Builder(TrafficTreatment treatment) {
120 for (Instruction instruction : treatment.instructions()) {
121 add(instruction);
122 }
123 }
124
alshabib8ca53902014-10-07 13:11:17 -0700125 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700126 public Builder add(Instruction instruction) {
alshabib010c31d2014-09-26 10:01:12 -0700127 if (drop) {
128 return this;
129 }
alshabib1d4cace2014-09-13 19:16:26 -0700130 switch (instruction.type()) {
tom9a693fd2014-10-03 11:32:19 -0700131 case DROP:
132 drop = true;
133 break;
134 case OUTPUT:
135 outputs.add(instruction);
136 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700137 case L0MODIFICATION:
tom9a693fd2014-10-03 11:32:19 -0700138 case L2MODIFICATION:
139 case L3MODIFICATION:
140 // TODO: enforce modification order if any
141 modifications.add(instruction);
142 break;
143 case GROUP:
144 groups.add(instruction);
145 break;
146 default:
tom1679e182014-10-09 13:50:45 -0700147 throw new IllegalArgumentException("Unknown instruction type: " +
148 instruction.type());
alshabib1d4cace2014-09-13 19:16:26 -0700149 }
150 return this;
151 }
152
153 @Override
Thomas Vachuskaf4df0052015-01-06 12:30:11 -0800154 public Builder drop() {
155 return add(Instructions.createDrop());
156 }
157
158 @Override
159 public Builder punt() {
160 return add(Instructions.createOutput(PortNumber.CONTROLLER));
alshabib010c31d2014-09-26 10:01:12 -0700161 }
162
163 @Override
164 public Builder setOutput(PortNumber number) {
165 return add(Instructions.createOutput(number));
166 }
167
168 @Override
169 public Builder setEthSrc(MacAddress addr) {
170 return add(Instructions.modL2Src(addr));
171 }
172
173 @Override
174 public Builder setEthDst(MacAddress addr) {
175 return add(Instructions.modL2Dst(addr));
176 }
177
178 @Override
179 public Builder setVlanId(VlanId id) {
180 return add(Instructions.modVlanId(id));
181 }
182
183 @Override
184 public Builder setVlanPcp(Byte pcp) {
185 return add(Instructions.modVlanPcp(pcp));
186 }
187
188 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700189 public Builder setIpSrc(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700190 return add(Instructions.modL3Src(addr));
191 }
192
193 @Override
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700194 public Builder setIpDst(IpAddress addr) {
alshabib010c31d2014-09-26 10:01:12 -0700195 return add(Instructions.modL3Dst(addr));
196 }
197
198 @Override
Praseed Balakrishnan8c67d172014-11-10 10:15:41 -0800199 public Builder pushMpls() {
200 return add(Instructions.pushMpls());
201 }
202
203 @Override
204 public Builder popMpls() {
205 return add(Instructions.popMpls());
206 }
207
208
209 @Override
210 public Builder setMpls(Integer mplsLabel) {
211 return add(Instructions.modMplsLabel(mplsLabel));
212 }
213
214 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700215 public Builder setLambda(short lambda) {
216 return add(Instructions.modL0Lambda(lambda));
217 }
218
219 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700220 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700221
Jonathan Hart86e59352014-10-22 10:42:16 -0700222 //If we are dropping should we just return an empty list?
alshabib1d4cace2014-09-13 19:16:26 -0700223 List<Instruction> instructions = new LinkedList<Instruction>();
224 instructions.addAll(modifications);
225 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700226 if (!drop) {
227 instructions.addAll(outputs);
228 }
alshabib1d4cace2014-09-13 19:16:26 -0700229
230 return new DefaultTrafficTreatment(instructions);
231 }
232
233 }
234
235}