blob: 030007913467d3f14e863d9f8a81a2c8fc8ced96 [file] [log] [blame]
Thomas Vachuska83e090e2014-10-22 14:25:35 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabib1d4cace2014-09-13 19:16:26 -070019package org.onlab.onos.net.flow;
20
Jonathan Hartd87aeca2014-10-21 10:42:52 -070021import java.util.LinkedList;
22import java.util.List;
23import java.util.Objects;
24
alshabib010c31d2014-09-26 10:01:12 -070025import org.onlab.onos.net.PortNumber;
alshabib55a55d92014-09-16 11:59:31 -070026import org.onlab.onos.net.flow.instructions.Instruction;
alshabib010c31d2014-09-26 10:01:12 -070027import org.onlab.onos.net.flow.instructions.Instructions;
28import org.onlab.packet.IpPrefix;
29import org.onlab.packet.MacAddress;
30import org.onlab.packet.VlanId;
tom1679e182014-10-09 13:50:45 -070031
Jonathan Hartd87aeca2014-10-21 10:42:52 -070032import com.google.common.base.MoreObjects;
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070033import com.google.common.collect.ImmutableList;
34
tom9a693fd2014-10-03 11:32:19 -070035/**
36 * Default traffic treatment implementation.
37 */
alshabib7b2748f2014-09-16 20:21:11 -070038public final class DefaultTrafficTreatment implements TrafficTreatment {
alshabib1d4cace2014-09-13 19:16:26 -070039
40 private final List<Instruction> instructions;
41
tom9a693fd2014-10-03 11:32:19 -070042 /**
43 * Creates a new traffic treatment from the specified list of instructions.
44 *
45 * @param instructions treatment instructions
46 */
alshabib7b2748f2014-09-16 20:21:11 -070047 private DefaultTrafficTreatment(List<Instruction> instructions) {
Yuta HIGUCHI2809bf32014-10-20 22:44:12 -070048 this.instructions = ImmutableList.copyOf(instructions);
alshabib1d4cace2014-09-13 19:16:26 -070049 }
50
51 @Override
52 public List<Instruction> instructions() {
53 return instructions;
54 }
55
56 /**
tom9a693fd2014-10-03 11:32:19 -070057 * Returns a new traffic treatment builder.
58 *
59 * @return traffic treatment builder
60 */
61 public static TrafficTreatment.Builder builder() {
62 return new Builder();
63 }
64
Jonathan Hart6e88c682014-10-21 17:05:25 -070065 /**
66 * Returns a new traffic treatment builder primed to produce entities
67 * patterned after the supplied treatment.
68 *
69 * @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.
103 * Modifications -> Group -> 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
alshabib010c31d2014-09-26 10:01:12 -0700157 public void drop() {
158 add(Instructions.createDrop());
159 }
160
161 @Override
162 public Builder setOutput(PortNumber number) {
163 return add(Instructions.createOutput(number));
164 }
165
166 @Override
167 public Builder setEthSrc(MacAddress addr) {
168 return add(Instructions.modL2Src(addr));
169 }
170
171 @Override
172 public Builder setEthDst(MacAddress addr) {
173 return add(Instructions.modL2Dst(addr));
174 }
175
176 @Override
177 public Builder setVlanId(VlanId id) {
178 return add(Instructions.modVlanId(id));
179 }
180
181 @Override
182 public Builder setVlanPcp(Byte pcp) {
183 return add(Instructions.modVlanPcp(pcp));
184 }
185
186 @Override
187 public Builder setIpSrc(IpPrefix addr) {
188 return add(Instructions.modL3Src(addr));
189 }
190
191 @Override
192 public Builder setIpDst(IpPrefix addr) {
193 return add(Instructions.modL3Dst(addr));
194 }
195
196 @Override
Marc De Leenheer49087752014-10-23 13:54:09 -0700197 public Builder setLambda(short lambda) {
198 return add(Instructions.modL0Lambda(lambda));
199 }
200
201 @Override
alshabib1d4cace2014-09-13 19:16:26 -0700202 public TrafficTreatment build() {
alshabib030111e2014-09-15 15:56:42 -0700203
204 //If we are dropping should we just return an emptry list?
alshabib1d4cace2014-09-13 19:16:26 -0700205 List<Instruction> instructions = new LinkedList<Instruction>();
206 instructions.addAll(modifications);
207 instructions.addAll(groups);
alshabib030111e2014-09-15 15:56:42 -0700208 if (!drop) {
209 instructions.addAll(outputs);
210 }
alshabib1d4cace2014-09-13 19:16:26 -0700211
212 return new DefaultTrafficTreatment(instructions);
213 }
214
215 }
216
217}