blob: dbb16a5cd4b2747ec8e1152ad18c797962f787f3 [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())
141 .add("context", context())
142 .toString();
143 }
144
alshabibfaa1e362015-04-02 15:01:54 -0700145 /**
146 * Returns a new builder.
147 *
148 * @return new builder
149 */
150 public static Builder builder() {
151 return new Builder();
152 }
153
Thomas Vachuska00f48162016-02-29 17:07:23 -0800154 @Override
155 public Builder copy() {
156 return new Builder(this);
157 }
158
alshabibfaa1e362015-04-02 15:01:54 -0700159 public static final class Builder implements NextObjective.Builder {
160
161 private ApplicationId appId;
162 private Type type;
163 private Integer id;
Ray Milkey810d6e72015-08-14 10:35:06 -0700164 private List<TrafficTreatment> treatments;
165 private Operation op;
166 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800167 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700168
169 private final ImmutableList.Builder<TrafficTreatment> listBuilder
170 = ImmutableList.builder();
171
Thomas Vachuska00f48162016-02-29 17:07:23 -0800172 // Creates an empty builder
173 private Builder() {
174 }
175
176 // Creates a builder set to create a copy of the specified objective.
177 private Builder(NextObjective objective) {
178 this.type = objective.type();
179 this.id = objective.id();
180 this.treatments = ImmutableList.copyOf(objective.next());
Thomas Vachuska160c1492016-03-29 11:11:04 -0700181 this.listBuilder.addAll(objective.next());
Thomas Vachuska00f48162016-02-29 17:07:23 -0800182 this.meta = objective.meta();
183 this.appId = objective.appId();
184 this.op = objective.op();
185 }
186
alshabibfaa1e362015-04-02 15:01:54 -0700187 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700188 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700189 this.id = nextId;
190 return this;
191 }
192
193 @Override
194 public Builder withType(Type type) {
195 this.type = type;
196 return this;
197 }
198
199 @Override
200 public Builder addTreatment(TrafficTreatment treatment) {
201 listBuilder.add(treatment);
202 return this;
203 }
204
205 /**
206 * Noop. This method has no effect.
207 *
208 * @param timeout a timeout
209 * @return a next objective builder
210 */
211 @Override
212 public Builder makeTemporary(int timeout) {
213 return this;
214 }
215
216 /**
217 * Noop. This method has no effect.
218 *
219 * @return a next objective builder
220 */
221 @Override
222 public Builder makePermanent() {
223 return this;
224 }
225
226 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700227 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700228 this.appId = appId;
229 return this;
230 }
231
232 /**
233 * Noop. This method has no effect.
234 *
235 * @param priority an integer
236 * @return a next objective builder
237 */
238 @Override
239 public Builder withPriority(int priority) {
240 return this;
241 }
242
243 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800244 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800245 this.meta = meta;
246 return this;
247 }
248
249 @Override
alshabib2a441c62015-04-13 18:39:38 -0700250 public NextObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700251 treatments = listBuilder.build();
252 op = Operation.ADD;
alshabibfaa1e362015-04-02 15:01:54 -0700253 checkNotNull(appId, "Must supply an application id");
254 checkNotNull(id, "id cannot be null");
255 checkNotNull(type, "The type cannot be null");
256 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
257
Ray Milkey810d6e72015-08-14 10:35:06 -0700258 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700259 }
260
261 @Override
262 public NextObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700263 treatments = listBuilder.build();
264 op = Operation.REMOVE;
alshabib2a441c62015-04-13 18:39:38 -0700265 checkNotNull(appId, "Must supply an application id");
266 checkNotNull(id, "id cannot be null");
267 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700268
Ray Milkey810d6e72015-08-14 10:35:06 -0700269 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700270 }
271
272 @Override
273 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700274 treatments = listBuilder.build();
275 op = Operation.ADD;
276 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700277 checkNotNull(appId, "Must supply an application id");
278 checkNotNull(id, "id cannot be null");
279 checkNotNull(type, "The type cannot be null");
280 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
281
Ray Milkey810d6e72015-08-14 10:35:06 -0700282 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700283 }
284
285 @Override
286 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700287 treatments = listBuilder.build();
288 op = Operation.REMOVE;
289 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700290 checkNotNull(appId, "Must supply an application id");
291 checkNotNull(id, "id cannot be null");
292 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700293
Ray Milkey810d6e72015-08-14 10:35:06 -0700294 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700295 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800296
297 @Override
298 public NextObjective addToExisting() {
299 treatments = listBuilder.build();
300 op = Operation.ADD_TO_EXISTING;
301 checkNotNull(appId, "Must supply an application id");
302 checkNotNull(id, "id cannot be null");
303 checkNotNull(type, "The type cannot be null");
304 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
305
306 return new DefaultNextObjective(this);
307 }
308
309 @Override
310 public NextObjective removeFromExisting() {
311 treatments = listBuilder.build();
312 op = Operation.REMOVE_FROM_EXISTING;
313 checkNotNull(appId, "Must supply an application id");
314 checkNotNull(id, "id cannot be null");
315 checkNotNull(type, "The type cannot be null");
316
317 return new DefaultNextObjective(this);
318 }
319
320 @Override
321 public NextObjective addToExisting(ObjectiveContext context) {
322 treatments = listBuilder.build();
323 op = Operation.ADD_TO_EXISTING;
324 this.context = context;
325 checkNotNull(appId, "Must supply an application id");
326 checkNotNull(id, "id cannot be null");
327 checkNotNull(type, "The type cannot be null");
328 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
329
330 return new DefaultNextObjective(this);
331 }
332
333 @Override
334 public NextObjective removeFromExisting(ObjectiveContext context) {
335 treatments = listBuilder.build();
336 op = Operation.REMOVE_FROM_EXISTING;
337 this.context = context;
338 checkNotNull(appId, "Must supply an application id");
339 checkNotNull(id, "id cannot be null");
340 checkNotNull(type, "The type cannot be null");
341
342 return new DefaultNextObjective(this);
343 }
344
alshabibfaa1e362015-04-02 15:01:54 -0700345 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800346
alshabibfaa1e362015-04-02 15:01:54 -0700347}