blob: 8c466c95247f48aeceec4c33fb4ed4cc5dceb34d [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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() {
Saurav Dasceccf242017-08-03 18:30:35 -0700250 return add(null);
alshabib2a441c62015-04-13 18:39:38 -0700251 }
252
253 @Override
254 public NextObjective remove() {
Saurav Dasceccf242017-08-03 18:30:35 -0700255 return remove(null);
alshabib2a441c62015-04-13 18:39:38 -0700256 }
257
258 @Override
259 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700260 treatments = listBuilder.build();
261 op = Operation.ADD;
262 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700263 checkNotNull(appId, "Must supply an application id");
264 checkNotNull(id, "id cannot be null");
265 checkNotNull(type, "The type cannot be null");
266 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
267
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 remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700273 treatments = listBuilder.build();
274 op = Operation.REMOVE;
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");
alshabib2a441c62015-04-13 18:39:38 -0700279
Ray Milkey810d6e72015-08-14 10:35:06 -0700280 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700281 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800282
283 @Override
284 public NextObjective addToExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700285 return addToExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800286 }
287
288 @Override
289 public NextObjective removeFromExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700290 return removeFromExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800291 }
292
293 @Override
294 public NextObjective addToExisting(ObjectiveContext context) {
295 treatments = listBuilder.build();
296 op = Operation.ADD_TO_EXISTING;
297 this.context = context;
298 checkNotNull(appId, "Must supply an application id");
299 checkNotNull(id, "id cannot be null");
300 checkNotNull(type, "The type cannot be null");
301 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
302
303 return new DefaultNextObjective(this);
304 }
305
306 @Override
307 public NextObjective removeFromExisting(ObjectiveContext context) {
308 treatments = listBuilder.build();
309 op = Operation.REMOVE_FROM_EXISTING;
310 this.context = context;
311 checkNotNull(appId, "Must supply an application id");
312 checkNotNull(id, "id cannot be null");
313 checkNotNull(type, "The type cannot be null");
314
315 return new DefaultNextObjective(this);
316 }
317
Saurav Dasceccf242017-08-03 18:30:35 -0700318 @Override
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900319 public NextObjective modify() {
320 return modify(null);
321 }
322
323 @Override
324 public NextObjective modify(ObjectiveContext context) {
325 treatments = listBuilder.build();
326 op = Operation.MODIFY;
327 this.context = context;
328 checkNotNull(appId, "Must supply an application id");
329 checkNotNull(id, "id cannot be null");
330 checkNotNull(type, "The type cannot be null");
331 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
332
333 return new DefaultNextObjective(this);
334 }
335
336 @Override
Saurav Dasceccf242017-08-03 18:30:35 -0700337 public NextObjective verify() {
338 return verify(null);
339 }
340
341 @Override
342 public NextObjective verify(ObjectiveContext context) {
343 treatments = listBuilder.build();
344 op = Operation.VERIFY;
345 this.context = context;
346 checkNotNull(appId, "Must supply an application id");
347 checkNotNull(id, "id cannot be null");
348 checkNotNull(type, "The type cannot be null");
349 return new DefaultNextObjective(this);
350 }
351
alshabibfaa1e362015-04-02 15:01:54 -0700352 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800353
alshabibfaa1e362015-04-02 15:01:54 -0700354}