blob: 32635cba665833aea9537e5063f5ad8f4b36b502 [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;
Andrea Campanellacbd16942021-06-01 16:33:14 +020021import org.onosproject.net.AbstractAnnotated;
22import org.onosproject.net.Annotations;
Saurav Das8a0732e2015-11-20 15:27:53 -080023import org.onosproject.net.flow.TrafficSelector;
alshabibfaa1e362015-04-02 15:01:54 -070024import org.onosproject.net.flow.TrafficTreatment;
25
26import java.util.Collection;
27import java.util.List;
Thomas Vachuska00f48162016-02-29 17:07:23 -080028import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070029import java.util.Optional;
Charles Chanb87d9f12018-10-31 21:00:06 -070030import java.util.stream.Collectors;
alshabibfaa1e362015-04-02 15:01:54 -070031
Charles Chan3e041292016-02-27 15:58:43 -080032import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070033import static com.google.common.base.Preconditions.checkArgument;
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Default implementation of a next objective.
38 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070039@Beta
Andrea Campanellacbd16942021-06-01 16:33:14 +020040public final class DefaultNextObjective extends AbstractAnnotated
41 implements NextObjective {
alshabibfaa1e362015-04-02 15:01:54 -070042
Charles Chanb87d9f12018-10-31 21:00:06 -070043 private final List<NextTreatment> treatments;
alshabibfaa1e362015-04-02 15:01:54 -070044 private final ApplicationId appId;
45 private final Type type;
46 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070047 private final Operation op;
48 private final Optional<ObjectiveContext> context;
Saurav Das8a0732e2015-11-20 15:27:53 -080049 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070050
Ray Milkey810d6e72015-08-14 10:35:06 -070051 private DefaultNextObjective(Builder builder) {
Andrea Campanellacbd16942021-06-01 16:33:14 +020052 super(builder.annotations);
Ray Milkey810d6e72015-08-14 10:35:06 -070053 this.treatments = builder.treatments;
54 this.appId = builder.appId;
55 this.type = builder.type;
56 this.id = builder.id;
57 this.op = builder.op;
58 this.context = Optional.ofNullable(builder.context);
Saurav Das8a0732e2015-11-20 15:27:53 -080059 this.meta = builder.meta;
alshabibfaa1e362015-04-02 15:01:54 -070060 }
61
62 @Override
63 public Collection<TrafficTreatment> next() {
Charles Chanb87d9f12018-10-31 21:00:06 -070064 return treatments.stream()
65 .filter(t -> t.type() == NextTreatment.Type.TREATMENT)
66 .map(t -> ((DefaultNextTreatment) t).treatment())
67 .collect(Collectors.toList());
68 }
69
70 @Override
71 public Collection<NextTreatment> nextTreatments() {
alshabibfaa1e362015-04-02 15:01:54 -070072 return treatments;
73 }
74
75 @Override
76 public Type type() {
77 return type;
78 }
79
80 @Override
81 public int id() {
82 return id;
83 }
84
85 @Override
86 public int priority() {
87 return 0;
88 }
89
90 @Override
91 public ApplicationId appId() {
92 return appId;
93 }
94
95 @Override
96 public int timeout() {
97 return 0;
98 }
99
100 @Override
101 public boolean permanent() {
102 return false;
103 }
104
105 @Override
106 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -0700107 return op;
108 }
109
110 @Override
111 public Optional<ObjectiveContext> context() {
112 return context;
alshabibfaa1e362015-04-02 15:01:54 -0700113 }
114
Saurav Das8a0732e2015-11-20 15:27:53 -0800115 @Override
116 public TrafficSelector meta() {
117 return meta;
118 }
119
Thomas Vachuska00f48162016-02-29 17:07:23 -0800120 @Override
121 public int hashCode() {
122 return Objects.hash(treatments, appId, type, id, op, meta);
123 }
124
125 @Override
126 public boolean equals(Object obj) {
127 if (this == obj) {
128 return true;
129 }
130 if (obj instanceof DefaultNextObjective) {
131 final DefaultNextObjective other = (DefaultNextObjective) obj;
132 return Objects.equals(this.treatments, other.treatments)
133 && Objects.equals(this.appId, other.appId)
134 && Objects.equals(this.type, other.type)
135 && Objects.equals(this.id, other.id)
136 && Objects.equals(this.op, other.op)
137 && Objects.equals(this.meta, other.meta);
138 }
139 return false;
140 }
141
Charles Chan3e041292016-02-27 15:58:43 -0800142 @Override
143 public String toString() {
144 return toStringHelper(this)
145 .add("id", id())
146 .add("type", type())
147 .add("op", op())
148 .add("priority", priority())
Charles Chanb87d9f12018-10-31 21:00:06 -0700149 .add("nextTreatments", nextTreatments())
Charles Chan3e041292016-02-27 15:58:43 -0800150 .add("meta", meta())
151 .add("appId", appId())
152 .add("permanent", permanent())
153 .add("timeout", timeout())
Andrea Campanellacbd16942021-06-01 16:33:14 +0200154 .add("annotations", annotations())
Charles Chan3e041292016-02-27 15:58:43 -0800155 .toString();
156 }
157
alshabibfaa1e362015-04-02 15:01:54 -0700158 /**
159 * Returns a new builder.
160 *
161 * @return new builder
162 */
163 public static Builder builder() {
164 return new Builder();
165 }
166
Thomas Vachuska00f48162016-02-29 17:07:23 -0800167 @Override
168 public Builder copy() {
169 return new Builder(this);
170 }
171
alshabibfaa1e362015-04-02 15:01:54 -0700172 public static final class Builder implements NextObjective.Builder {
173
174 private ApplicationId appId;
175 private Type type;
176 private Integer id;
Charles Chanb87d9f12018-10-31 21:00:06 -0700177 private List<NextTreatment> treatments;
Ray Milkey810d6e72015-08-14 10:35:06 -0700178 private Operation op;
179 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800180 private TrafficSelector meta;
Andrea Campanellacbd16942021-06-01 16:33:14 +0200181 private Annotations annotations;
alshabibfaa1e362015-04-02 15:01:54 -0700182
Charles Chanb87d9f12018-10-31 21:00:06 -0700183 private final ImmutableList.Builder<NextTreatment> listBuilder
alshabibfaa1e362015-04-02 15:01:54 -0700184 = ImmutableList.builder();
185
Thomas Vachuska00f48162016-02-29 17:07:23 -0800186 // Creates an empty builder
187 private Builder() {
188 }
189
190 // Creates a builder set to create a copy of the specified objective.
191 private Builder(NextObjective objective) {
192 this.type = objective.type();
193 this.id = objective.id();
Charles Chanb87d9f12018-10-31 21:00:06 -0700194 this.treatments = ImmutableList.copyOf(objective.nextTreatments());
195 this.listBuilder.addAll(objective.nextTreatments());
Thomas Vachuska00f48162016-02-29 17:07:23 -0800196 this.meta = objective.meta();
197 this.appId = objective.appId();
198 this.op = objective.op();
Andrea Campanellacbd16942021-06-01 16:33:14 +0200199 this.annotations = objective.annotations();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800200 }
201
alshabibfaa1e362015-04-02 15:01:54 -0700202 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700203 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700204 this.id = nextId;
205 return this;
206 }
207
208 @Override
209 public Builder withType(Type type) {
210 this.type = type;
211 return this;
212 }
213
214 @Override
215 public Builder addTreatment(TrafficTreatment treatment) {
Charles Chanb87d9f12018-10-31 21:00:06 -0700216 listBuilder.add(DefaultNextTreatment.of(treatment));
217 return this;
218 }
219
220 @Override
221 public Builder addTreatment(NextTreatment nextTreatment) {
222 listBuilder.add(nextTreatment);
alshabibfaa1e362015-04-02 15:01:54 -0700223 return this;
224 }
225
226 /**
227 * Noop. This method has no effect.
228 *
229 * @param timeout a timeout
230 * @return a next objective builder
231 */
232 @Override
233 public Builder makeTemporary(int timeout) {
234 return this;
235 }
236
237 /**
238 * Noop. This method has no effect.
239 *
240 * @return a next objective builder
241 */
242 @Override
243 public Builder makePermanent() {
244 return this;
245 }
246
247 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700248 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700249 this.appId = appId;
250 return this;
251 }
252
253 /**
254 * Noop. This method has no effect.
255 *
256 * @param priority an integer
257 * @return a next objective builder
258 */
259 @Override
260 public Builder withPriority(int priority) {
261 return this;
262 }
263
264 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800265 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800266 this.meta = meta;
267 return this;
268 }
269
270 @Override
Andrea Campanellacbd16942021-06-01 16:33:14 +0200271 public Builder withAnnotations(Annotations annotations) {
272 this.annotations = annotations;
273 return this;
274 }
275
276 @Override
alshabib2a441c62015-04-13 18:39:38 -0700277 public NextObjective add() {
Saurav Dasceccf242017-08-03 18:30:35 -0700278 return add(null);
alshabib2a441c62015-04-13 18:39:38 -0700279 }
280
281 @Override
282 public NextObjective remove() {
Saurav Dasceccf242017-08-03 18:30:35 -0700283 return remove(null);
alshabib2a441c62015-04-13 18:39:38 -0700284 }
285
286 @Override
287 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700288 treatments = listBuilder.build();
289 op = Operation.ADD;
290 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700291 checkNotNull(appId, "Must supply an application id");
292 checkNotNull(id, "id cannot be null");
293 checkNotNull(type, "The type cannot be null");
294 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
295
Ray Milkey810d6e72015-08-14 10:35:06 -0700296 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700297 }
298
299 @Override
300 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700301 treatments = listBuilder.build();
302 op = Operation.REMOVE;
303 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700304 checkNotNull(appId, "Must supply an application id");
305 checkNotNull(id, "id cannot be null");
306 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700307
Ray Milkey810d6e72015-08-14 10:35:06 -0700308 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700309 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800310
311 @Override
312 public NextObjective addToExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700313 return addToExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800314 }
315
316 @Override
317 public NextObjective removeFromExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700318 return removeFromExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800319 }
320
321 @Override
322 public NextObjective addToExisting(ObjectiveContext context) {
323 treatments = listBuilder.build();
324 op = Operation.ADD_TO_EXISTING;
325 this.context = context;
326 checkNotNull(appId, "Must supply an application id");
327 checkNotNull(id, "id cannot be null");
328 checkNotNull(type, "The type cannot be null");
329 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
330
331 return new DefaultNextObjective(this);
332 }
333
334 @Override
335 public NextObjective removeFromExisting(ObjectiveContext context) {
336 treatments = listBuilder.build();
337 op = Operation.REMOVE_FROM_EXISTING;
338 this.context = context;
339 checkNotNull(appId, "Must supply an application id");
340 checkNotNull(id, "id cannot be null");
341 checkNotNull(type, "The type cannot be null");
342
343 return new DefaultNextObjective(this);
344 }
345
Saurav Dasceccf242017-08-03 18:30:35 -0700346 @Override
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900347 public NextObjective modify() {
348 return modify(null);
349 }
350
351 @Override
352 public NextObjective modify(ObjectiveContext context) {
353 treatments = listBuilder.build();
354 op = Operation.MODIFY;
355 this.context = context;
356 checkNotNull(appId, "Must supply an application id");
357 checkNotNull(id, "id cannot be null");
358 checkNotNull(type, "The type cannot be null");
359 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
360
361 return new DefaultNextObjective(this);
362 }
363
364 @Override
Saurav Dasceccf242017-08-03 18:30:35 -0700365 public NextObjective verify() {
366 return verify(null);
367 }
368
369 @Override
370 public NextObjective verify(ObjectiveContext context) {
371 treatments = listBuilder.build();
372 op = Operation.VERIFY;
373 this.context = context;
374 checkNotNull(appId, "Must supply an application id");
375 checkNotNull(id, "id cannot be null");
376 checkNotNull(type, "The type cannot be null");
377 return new DefaultNextObjective(this);
378 }
379
alshabibfaa1e362015-04-02 15:01:54 -0700380 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800381
alshabibfaa1e362015-04-02 15:01:54 -0700382}