blob: 36656c481e75c89d5070be7886d8be778a2baf2c [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;
Charles Chanb87d9f12018-10-31 21:00:06 -070028import java.util.stream.Collectors;
alshabibfaa1e362015-04-02 15:01:54 -070029
Charles Chan3e041292016-02-27 15:58:43 -080030import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070031import static com.google.common.base.Preconditions.checkArgument;
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Default implementation of a next objective.
36 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070037@Beta
alshabibfaa1e362015-04-02 15:01:54 -070038public final class DefaultNextObjective implements NextObjective {
39
Charles Chanb87d9f12018-10-31 21:00:06 -070040 private final List<NextTreatment> treatments;
alshabibfaa1e362015-04-02 15:01:54 -070041 private final ApplicationId appId;
42 private final Type type;
43 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070044 private final Operation op;
45 private final Optional<ObjectiveContext> context;
Saurav Das8a0732e2015-11-20 15:27:53 -080046 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070047
Ray Milkey810d6e72015-08-14 10:35:06 -070048 private DefaultNextObjective(Builder builder) {
49 this.treatments = builder.treatments;
50 this.appId = builder.appId;
51 this.type = builder.type;
52 this.id = builder.id;
53 this.op = builder.op;
54 this.context = Optional.ofNullable(builder.context);
Saurav Das8a0732e2015-11-20 15:27:53 -080055 this.meta = builder.meta;
alshabibfaa1e362015-04-02 15:01:54 -070056 }
57
58 @Override
59 public Collection<TrafficTreatment> next() {
Charles Chanb87d9f12018-10-31 21:00:06 -070060 return treatments.stream()
61 .filter(t -> t.type() == NextTreatment.Type.TREATMENT)
62 .map(t -> ((DefaultNextTreatment) t).treatment())
63 .collect(Collectors.toList());
64 }
65
66 @Override
67 public Collection<NextTreatment> nextTreatments() {
alshabibfaa1e362015-04-02 15:01:54 -070068 return treatments;
69 }
70
71 @Override
72 public Type type() {
73 return type;
74 }
75
76 @Override
77 public int id() {
78 return id;
79 }
80
81 @Override
82 public int priority() {
83 return 0;
84 }
85
86 @Override
87 public ApplicationId appId() {
88 return appId;
89 }
90
91 @Override
92 public int timeout() {
93 return 0;
94 }
95
96 @Override
97 public boolean permanent() {
98 return false;
99 }
100
101 @Override
102 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -0700103 return op;
104 }
105
106 @Override
107 public Optional<ObjectiveContext> context() {
108 return context;
alshabibfaa1e362015-04-02 15:01:54 -0700109 }
110
Saurav Das8a0732e2015-11-20 15:27:53 -0800111 @Override
112 public TrafficSelector meta() {
113 return meta;
114 }
115
Thomas Vachuska00f48162016-02-29 17:07:23 -0800116 @Override
117 public int hashCode() {
118 return Objects.hash(treatments, appId, type, id, op, meta);
119 }
120
121 @Override
122 public boolean equals(Object obj) {
123 if (this == obj) {
124 return true;
125 }
126 if (obj instanceof DefaultNextObjective) {
127 final DefaultNextObjective other = (DefaultNextObjective) obj;
128 return Objects.equals(this.treatments, other.treatments)
129 && Objects.equals(this.appId, other.appId)
130 && Objects.equals(this.type, other.type)
131 && Objects.equals(this.id, other.id)
132 && Objects.equals(this.op, other.op)
133 && Objects.equals(this.meta, other.meta);
134 }
135 return false;
136 }
137
Charles Chan3e041292016-02-27 15:58:43 -0800138 @Override
139 public String toString() {
140 return toStringHelper(this)
141 .add("id", id())
142 .add("type", type())
143 .add("op", op())
144 .add("priority", priority())
Charles Chanb87d9f12018-10-31 21:00:06 -0700145 .add("nextTreatments", nextTreatments())
Charles Chan3e041292016-02-27 15:58:43 -0800146 .add("meta", meta())
147 .add("appId", appId())
148 .add("permanent", permanent())
149 .add("timeout", timeout())
Charles Chan3e041292016-02-27 15:58:43 -0800150 .toString();
151 }
152
alshabibfaa1e362015-04-02 15:01:54 -0700153 /**
154 * Returns a new builder.
155 *
156 * @return new builder
157 */
158 public static Builder builder() {
159 return new Builder();
160 }
161
Thomas Vachuska00f48162016-02-29 17:07:23 -0800162 @Override
163 public Builder copy() {
164 return new Builder(this);
165 }
166
alshabibfaa1e362015-04-02 15:01:54 -0700167 public static final class Builder implements NextObjective.Builder {
168
169 private ApplicationId appId;
170 private Type type;
171 private Integer id;
Charles Chanb87d9f12018-10-31 21:00:06 -0700172 private List<NextTreatment> treatments;
Ray Milkey810d6e72015-08-14 10:35:06 -0700173 private Operation op;
174 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800175 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700176
Charles Chanb87d9f12018-10-31 21:00:06 -0700177 private final ImmutableList.Builder<NextTreatment> listBuilder
alshabibfaa1e362015-04-02 15:01:54 -0700178 = ImmutableList.builder();
179
Thomas Vachuska00f48162016-02-29 17:07:23 -0800180 // Creates an empty builder
181 private Builder() {
182 }
183
184 // Creates a builder set to create a copy of the specified objective.
185 private Builder(NextObjective objective) {
186 this.type = objective.type();
187 this.id = objective.id();
Charles Chanb87d9f12018-10-31 21:00:06 -0700188 this.treatments = ImmutableList.copyOf(objective.nextTreatments());
189 this.listBuilder.addAll(objective.nextTreatments());
Thomas Vachuska00f48162016-02-29 17:07:23 -0800190 this.meta = objective.meta();
191 this.appId = objective.appId();
192 this.op = objective.op();
193 }
194
alshabibfaa1e362015-04-02 15:01:54 -0700195 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700196 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700197 this.id = nextId;
198 return this;
199 }
200
201 @Override
202 public Builder withType(Type type) {
203 this.type = type;
204 return this;
205 }
206
207 @Override
208 public Builder addTreatment(TrafficTreatment treatment) {
Charles Chanb87d9f12018-10-31 21:00:06 -0700209 listBuilder.add(DefaultNextTreatment.of(treatment));
210 return this;
211 }
212
213 @Override
214 public Builder addTreatment(NextTreatment nextTreatment) {
215 listBuilder.add(nextTreatment);
alshabibfaa1e362015-04-02 15:01:54 -0700216 return this;
217 }
218
219 /**
220 * Noop. This method has no effect.
221 *
222 * @param timeout a timeout
223 * @return a next objective builder
224 */
225 @Override
226 public Builder makeTemporary(int timeout) {
227 return this;
228 }
229
230 /**
231 * Noop. This method has no effect.
232 *
233 * @return a next objective builder
234 */
235 @Override
236 public Builder makePermanent() {
237 return this;
238 }
239
240 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700241 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700242 this.appId = appId;
243 return this;
244 }
245
246 /**
247 * Noop. This method has no effect.
248 *
249 * @param priority an integer
250 * @return a next objective builder
251 */
252 @Override
253 public Builder withPriority(int priority) {
254 return this;
255 }
256
257 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800258 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800259 this.meta = meta;
260 return this;
261 }
262
263 @Override
alshabib2a441c62015-04-13 18:39:38 -0700264 public NextObjective add() {
Saurav Dasceccf242017-08-03 18:30:35 -0700265 return add(null);
alshabib2a441c62015-04-13 18:39:38 -0700266 }
267
268 @Override
269 public NextObjective remove() {
Saurav Dasceccf242017-08-03 18:30:35 -0700270 return remove(null);
alshabib2a441c62015-04-13 18:39:38 -0700271 }
272
273 @Override
274 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700275 treatments = listBuilder.build();
276 op = Operation.ADD;
277 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700278 checkNotNull(appId, "Must supply an application id");
279 checkNotNull(id, "id cannot be null");
280 checkNotNull(type, "The type cannot be null");
281 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
282
Ray Milkey810d6e72015-08-14 10:35:06 -0700283 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700284 }
285
286 @Override
287 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700288 treatments = listBuilder.build();
289 op = Operation.REMOVE;
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");
alshabib2a441c62015-04-13 18:39:38 -0700294
Ray Milkey810d6e72015-08-14 10:35:06 -0700295 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700296 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800297
298 @Override
299 public NextObjective addToExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700300 return addToExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800301 }
302
303 @Override
304 public NextObjective removeFromExisting() {
Saurav Dasceccf242017-08-03 18:30:35 -0700305 return removeFromExisting(null);
Saurav Das8a0732e2015-11-20 15:27:53 -0800306 }
307
308 @Override
309 public NextObjective addToExisting(ObjectiveContext context) {
310 treatments = listBuilder.build();
311 op = Operation.ADD_TO_EXISTING;
312 this.context = context;
313 checkNotNull(appId, "Must supply an application id");
314 checkNotNull(id, "id cannot be null");
315 checkNotNull(type, "The type cannot be null");
316 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
317
318 return new DefaultNextObjective(this);
319 }
320
321 @Override
322 public NextObjective removeFromExisting(ObjectiveContext context) {
323 treatments = listBuilder.build();
324 op = Operation.REMOVE_FROM_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
330 return new DefaultNextObjective(this);
331 }
332
Saurav Dasceccf242017-08-03 18:30:35 -0700333 @Override
Jonghwan Hyunf810a7a2018-02-12 16:43:45 +0900334 public NextObjective modify() {
335 return modify(null);
336 }
337
338 @Override
339 public NextObjective modify(ObjectiveContext context) {
340 treatments = listBuilder.build();
341 op = Operation.MODIFY;
342 this.context = context;
343 checkNotNull(appId, "Must supply an application id");
344 checkNotNull(id, "id cannot be null");
345 checkNotNull(type, "The type cannot be null");
346 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
347
348 return new DefaultNextObjective(this);
349 }
350
351 @Override
Saurav Dasceccf242017-08-03 18:30:35 -0700352 public NextObjective verify() {
353 return verify(null);
354 }
355
356 @Override
357 public NextObjective verify(ObjectiveContext context) {
358 treatments = listBuilder.build();
359 op = Operation.VERIFY;
360 this.context = context;
361 checkNotNull(appId, "Must supply an application id");
362 checkNotNull(id, "id cannot be null");
363 checkNotNull(type, "The type cannot be null");
364 return new DefaultNextObjective(this);
365 }
366
alshabibfaa1e362015-04-02 15:01:54 -0700367 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800368
alshabibfaa1e362015-04-02 15:01:54 -0700369}