blob: 82794248c494904df48d7b10a24f2200889ad96c [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
alshabibfaa1e362015-04-02 15:01:54 -07003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.net.flowobjective;
17
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070018import com.google.common.annotations.Beta;
alshabibfaa1e362015-04-02 15:01:54 -070019import com.google.common.collect.ImmutableList;
20import org.onosproject.core.ApplicationId;
Saurav Das8a0732e2015-11-20 15:27:53 -080021import org.onosproject.net.flow.TrafficSelector;
alshabibfaa1e362015-04-02 15:01:54 -070022import org.onosproject.net.flow.TrafficTreatment;
23
24import java.util.Collection;
25import java.util.List;
Thomas Vachuska00f48162016-02-29 17:07:23 -080026import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070027import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070028
Charles Chan3e041292016-02-27 15:58:43 -080029import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070030import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Default implementation of a next objective.
35 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070036@Beta
alshabibfaa1e362015-04-02 15:01:54 -070037public final class DefaultNextObjective implements NextObjective {
38
39 private final List<TrafficTreatment> treatments;
40 private final ApplicationId appId;
41 private final Type type;
42 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070043 private final Operation op;
44 private final Optional<ObjectiveContext> context;
Saurav Das8a0732e2015-11-20 15:27:53 -080045 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070046
Ray Milkey810d6e72015-08-14 10:35:06 -070047 private DefaultNextObjective(Builder builder) {
48 this.treatments = builder.treatments;
49 this.appId = builder.appId;
50 this.type = builder.type;
51 this.id = builder.id;
52 this.op = builder.op;
53 this.context = Optional.ofNullable(builder.context);
Saurav Das8a0732e2015-11-20 15:27:53 -080054 this.meta = builder.meta;
alshabibfaa1e362015-04-02 15:01:54 -070055 }
56
57 @Override
58 public Collection<TrafficTreatment> next() {
59 return treatments;
60 }
61
62 @Override
63 public Type type() {
64 return type;
65 }
66
67 @Override
68 public int id() {
69 return id;
70 }
71
72 @Override
73 public int priority() {
74 return 0;
75 }
76
77 @Override
78 public ApplicationId appId() {
79 return appId;
80 }
81
82 @Override
83 public int timeout() {
84 return 0;
85 }
86
87 @Override
88 public boolean permanent() {
89 return false;
90 }
91
92 @Override
93 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -070094 return op;
95 }
96
97 @Override
98 public Optional<ObjectiveContext> context() {
99 return context;
alshabibfaa1e362015-04-02 15:01:54 -0700100 }
101
Saurav Das8a0732e2015-11-20 15:27:53 -0800102 @Override
103 public TrafficSelector meta() {
104 return meta;
105 }
106
Thomas Vachuska00f48162016-02-29 17:07:23 -0800107 @Override
108 public int hashCode() {
109 return Objects.hash(treatments, appId, type, id, op, meta);
110 }
111
112 @Override
113 public boolean equals(Object obj) {
114 if (this == obj) {
115 return true;
116 }
117 if (obj instanceof DefaultNextObjective) {
118 final DefaultNextObjective other = (DefaultNextObjective) obj;
119 return Objects.equals(this.treatments, other.treatments)
120 && Objects.equals(this.appId, other.appId)
121 && Objects.equals(this.type, other.type)
122 && Objects.equals(this.id, other.id)
123 && Objects.equals(this.op, other.op)
124 && Objects.equals(this.meta, other.meta);
125 }
126 return false;
127 }
128
Charles Chan3e041292016-02-27 15:58:43 -0800129 @Override
130 public String toString() {
131 return toStringHelper(this)
132 .add("id", id())
133 .add("type", type())
134 .add("op", op())
135 .add("priority", priority())
136 .add("next", next())
137 .add("meta", meta())
138 .add("appId", appId())
139 .add("permanent", permanent())
140 .add("timeout", timeout())
Charles Chan3e041292016-02-27 15:58:43 -0800141 .toString();
142 }
143
alshabibfaa1e362015-04-02 15:01:54 -0700144 /**
145 * Returns a new builder.
146 *
147 * @return new builder
148 */
149 public static Builder builder() {
150 return new Builder();
151 }
152
Thomas Vachuska00f48162016-02-29 17:07:23 -0800153 @Override
154 public Builder copy() {
155 return new Builder(this);
156 }
157
alshabibfaa1e362015-04-02 15:01:54 -0700158 public static final class Builder implements NextObjective.Builder {
159
160 private ApplicationId appId;
161 private Type type;
162 private Integer id;
Ray Milkey810d6e72015-08-14 10:35:06 -0700163 private List<TrafficTreatment> treatments;
164 private Operation op;
165 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800166 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700167
168 private final ImmutableList.Builder<TrafficTreatment> listBuilder
169 = ImmutableList.builder();
170
Thomas Vachuska00f48162016-02-29 17:07:23 -0800171 // Creates an empty builder
172 private Builder() {
173 }
174
175 // Creates a builder set to create a copy of the specified objective.
176 private Builder(NextObjective objective) {
177 this.type = objective.type();
178 this.id = objective.id();
179 this.treatments = ImmutableList.copyOf(objective.next());
Thomas Vachuska160c1492016-03-29 11:11:04 -0700180 this.listBuilder.addAll(objective.next());
Thomas Vachuska00f48162016-02-29 17:07:23 -0800181 this.meta = objective.meta();
182 this.appId = objective.appId();
183 this.op = objective.op();
184 }
185
alshabibfaa1e362015-04-02 15:01:54 -0700186 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700187 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700188 this.id = nextId;
189 return this;
190 }
191
192 @Override
193 public Builder withType(Type type) {
194 this.type = type;
195 return this;
196 }
197
198 @Override
199 public Builder addTreatment(TrafficTreatment treatment) {
200 listBuilder.add(treatment);
201 return this;
202 }
203
204 /**
205 * Noop. This method has no effect.
206 *
207 * @param timeout a timeout
208 * @return a next objective builder
209 */
210 @Override
211 public Builder makeTemporary(int timeout) {
212 return this;
213 }
214
215 /**
216 * Noop. This method has no effect.
217 *
218 * @return a next objective builder
219 */
220 @Override
221 public Builder makePermanent() {
222 return this;
223 }
224
225 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700226 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700227 this.appId = appId;
228 return this;
229 }
230
231 /**
232 * Noop. This method has no effect.
233 *
234 * @param priority an integer
235 * @return a next objective builder
236 */
237 @Override
238 public Builder withPriority(int priority) {
239 return this;
240 }
241
242 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800243 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800244 this.meta = meta;
245 return this;
246 }
247
248 @Override
alshabib2a441c62015-04-13 18:39:38 -0700249 public NextObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700250 treatments = listBuilder.build();
251 op = Operation.ADD;
alshabibfaa1e362015-04-02 15:01:54 -0700252 checkNotNull(appId, "Must supply an application id");
253 checkNotNull(id, "id cannot be null");
254 checkNotNull(type, "The type cannot be null");
255 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
256
Ray Milkey810d6e72015-08-14 10:35:06 -0700257 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700258 }
259
260 @Override
261 public NextObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700262 treatments = listBuilder.build();
263 op = Operation.REMOVE;
alshabib2a441c62015-04-13 18:39:38 -0700264 checkNotNull(appId, "Must supply an application id");
265 checkNotNull(id, "id cannot be null");
266 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700267
Ray Milkey810d6e72015-08-14 10:35:06 -0700268 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700269 }
270
271 @Override
272 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700273 treatments = listBuilder.build();
274 op = Operation.ADD;
275 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700276 checkNotNull(appId, "Must supply an application id");
277 checkNotNull(id, "id cannot be null");
278 checkNotNull(type, "The type cannot be null");
279 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
280
Ray Milkey810d6e72015-08-14 10:35:06 -0700281 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700282 }
283
284 @Override
285 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700286 treatments = listBuilder.build();
287 op = Operation.REMOVE;
288 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700289 checkNotNull(appId, "Must supply an application id");
290 checkNotNull(id, "id cannot be null");
291 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700292
Ray Milkey810d6e72015-08-14 10:35:06 -0700293 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700294 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800295
296 @Override
297 public NextObjective addToExisting() {
298 treatments = listBuilder.build();
299 op = Operation.ADD_TO_EXISTING;
300 checkNotNull(appId, "Must supply an application id");
301 checkNotNull(id, "id cannot be null");
302 checkNotNull(type, "The type cannot be null");
303 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
304
305 return new DefaultNextObjective(this);
306 }
307
308 @Override
309 public NextObjective removeFromExisting() {
310 treatments = listBuilder.build();
311 op = Operation.REMOVE_FROM_EXISTING;
312 checkNotNull(appId, "Must supply an application id");
313 checkNotNull(id, "id cannot be null");
314 checkNotNull(type, "The type cannot be null");
315
316 return new DefaultNextObjective(this);
317 }
318
319 @Override
320 public NextObjective addToExisting(ObjectiveContext context) {
321 treatments = listBuilder.build();
322 op = Operation.ADD_TO_EXISTING;
323 this.context = context;
324 checkNotNull(appId, "Must supply an application id");
325 checkNotNull(id, "id cannot be null");
326 checkNotNull(type, "The type cannot be null");
327 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
328
329 return new DefaultNextObjective(this);
330 }
331
332 @Override
333 public NextObjective removeFromExisting(ObjectiveContext context) {
334 treatments = listBuilder.build();
335 op = Operation.REMOVE_FROM_EXISTING;
336 this.context = context;
337 checkNotNull(appId, "Must supply an application id");
338 checkNotNull(id, "id cannot be null");
339 checkNotNull(type, "The type cannot be null");
340
341 return new DefaultNextObjective(this);
342 }
343
alshabibfaa1e362015-04-02 15:01:54 -0700344 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800345
alshabibfaa1e362015-04-02 15:01:54 -0700346}