blob: 8010659e5a7b20a70315834995102a38ec364dd9 [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Default implementation of a next objective.
34 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070035@Beta
alshabibfaa1e362015-04-02 15:01:54 -070036public final class DefaultNextObjective implements NextObjective {
37
38 private final List<TrafficTreatment> treatments;
39 private final ApplicationId appId;
40 private final Type type;
41 private final Integer id;
alshabib2a441c62015-04-13 18:39:38 -070042 private final Operation op;
43 private final Optional<ObjectiveContext> context;
Saurav Das8a0732e2015-11-20 15:27:53 -080044 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070045
Ray Milkey810d6e72015-08-14 10:35:06 -070046 private DefaultNextObjective(Builder builder) {
47 this.treatments = builder.treatments;
48 this.appId = builder.appId;
49 this.type = builder.type;
50 this.id = builder.id;
51 this.op = builder.op;
52 this.context = Optional.ofNullable(builder.context);
Saurav Das8a0732e2015-11-20 15:27:53 -080053 this.meta = builder.meta;
alshabibfaa1e362015-04-02 15:01:54 -070054 }
55
56 @Override
57 public Collection<TrafficTreatment> next() {
58 return treatments;
59 }
60
61 @Override
62 public Type type() {
63 return type;
64 }
65
66 @Override
67 public int id() {
68 return id;
69 }
70
71 @Override
72 public int priority() {
73 return 0;
74 }
75
76 @Override
77 public ApplicationId appId() {
78 return appId;
79 }
80
81 @Override
82 public int timeout() {
83 return 0;
84 }
85
86 @Override
87 public boolean permanent() {
88 return false;
89 }
90
91 @Override
92 public Operation op() {
alshabib2a441c62015-04-13 18:39:38 -070093 return op;
94 }
95
96 @Override
97 public Optional<ObjectiveContext> context() {
98 return context;
alshabibfaa1e362015-04-02 15:01:54 -070099 }
100
Saurav Das8a0732e2015-11-20 15:27:53 -0800101 @Override
102 public TrafficSelector meta() {
103 return meta;
104 }
105
Thomas Vachuska00f48162016-02-29 17:07:23 -0800106 @Override
107 public int hashCode() {
108 return Objects.hash(treatments, appId, type, id, op, meta);
109 }
110
111 @Override
112 public boolean equals(Object obj) {
113 if (this == obj) {
114 return true;
115 }
116 if (obj instanceof DefaultNextObjective) {
117 final DefaultNextObjective other = (DefaultNextObjective) obj;
118 return Objects.equals(this.treatments, other.treatments)
119 && Objects.equals(this.appId, other.appId)
120 && Objects.equals(this.type, other.type)
121 && Objects.equals(this.id, other.id)
122 && Objects.equals(this.op, other.op)
123 && Objects.equals(this.meta, other.meta);
124 }
125 return false;
126 }
127
alshabibfaa1e362015-04-02 15:01:54 -0700128 /**
129 * Returns a new builder.
130 *
131 * @return new builder
132 */
133 public static Builder builder() {
134 return new Builder();
135 }
136
Thomas Vachuska00f48162016-02-29 17:07:23 -0800137 @Override
138 public Builder copy() {
139 return new Builder(this);
140 }
141
alshabibfaa1e362015-04-02 15:01:54 -0700142 public static final class Builder implements NextObjective.Builder {
143
144 private ApplicationId appId;
145 private Type type;
146 private Integer id;
Ray Milkey810d6e72015-08-14 10:35:06 -0700147 private List<TrafficTreatment> treatments;
148 private Operation op;
149 private ObjectiveContext context;
Saurav Das8a0732e2015-11-20 15:27:53 -0800150 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700151
152 private final ImmutableList.Builder<TrafficTreatment> listBuilder
153 = ImmutableList.builder();
154
Thomas Vachuska00f48162016-02-29 17:07:23 -0800155 // Creates an empty builder
156 private Builder() {
157 }
158
159 // Creates a builder set to create a copy of the specified objective.
160 private Builder(NextObjective objective) {
161 this.type = objective.type();
162 this.id = objective.id();
163 this.treatments = ImmutableList.copyOf(objective.next());
164 this.meta = objective.meta();
165 this.appId = objective.appId();
166 this.op = objective.op();
167 }
168
alshabibfaa1e362015-04-02 15:01:54 -0700169 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700170 public Builder withId(int nextId) {
alshabibfaa1e362015-04-02 15:01:54 -0700171 this.id = nextId;
172 return this;
173 }
174
175 @Override
176 public Builder withType(Type type) {
177 this.type = type;
178 return this;
179 }
180
181 @Override
182 public Builder addTreatment(TrafficTreatment treatment) {
183 listBuilder.add(treatment);
184 return this;
185 }
186
187 /**
188 * Noop. This method has no effect.
189 *
190 * @param timeout a timeout
191 * @return a next objective builder
192 */
193 @Override
194 public Builder makeTemporary(int timeout) {
195 return this;
196 }
197
198 /**
199 * Noop. This method has no effect.
200 *
201 * @return a next objective builder
202 */
203 @Override
204 public Builder makePermanent() {
205 return this;
206 }
207
208 @Override
Ray Milkey810d6e72015-08-14 10:35:06 -0700209 public Builder fromApp(ApplicationId appId) {
alshabibfaa1e362015-04-02 15:01:54 -0700210 this.appId = appId;
211 return this;
212 }
213
214 /**
215 * Noop. This method has no effect.
216 *
217 * @param priority an integer
218 * @return a next objective builder
219 */
220 @Override
221 public Builder withPriority(int priority) {
222 return this;
223 }
224
225 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800226 public Builder withMeta(TrafficSelector meta) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800227 this.meta = meta;
228 return this;
229 }
230
231 @Override
alshabib2a441c62015-04-13 18:39:38 -0700232 public NextObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700233 treatments = listBuilder.build();
234 op = Operation.ADD;
alshabibfaa1e362015-04-02 15:01:54 -0700235 checkNotNull(appId, "Must supply an application id");
236 checkNotNull(id, "id cannot be null");
237 checkNotNull(type, "The type cannot be null");
238 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
239
Ray Milkey810d6e72015-08-14 10:35:06 -0700240 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700241 }
242
243 @Override
244 public NextObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700245 treatments = listBuilder.build();
246 op = Operation.REMOVE;
alshabib2a441c62015-04-13 18:39:38 -0700247 checkNotNull(appId, "Must supply an application id");
248 checkNotNull(id, "id cannot be null");
249 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700250
Ray Milkey810d6e72015-08-14 10:35:06 -0700251 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700252 }
253
254 @Override
255 public NextObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700256 treatments = listBuilder.build();
257 op = Operation.ADD;
258 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700259 checkNotNull(appId, "Must supply an application id");
260 checkNotNull(id, "id cannot be null");
261 checkNotNull(type, "The type cannot be null");
262 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
263
Ray Milkey810d6e72015-08-14 10:35:06 -0700264 return new DefaultNextObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700265 }
266
267 @Override
268 public NextObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700269 treatments = listBuilder.build();
270 op = Operation.REMOVE;
271 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700272 checkNotNull(appId, "Must supply an application id");
273 checkNotNull(id, "id cannot be null");
274 checkNotNull(type, "The type cannot be null");
alshabib2a441c62015-04-13 18:39:38 -0700275
Ray Milkey810d6e72015-08-14 10:35:06 -0700276 return new DefaultNextObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700277 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800278
279 @Override
280 public NextObjective addToExisting() {
281 treatments = listBuilder.build();
282 op = Operation.ADD_TO_EXISTING;
283 checkNotNull(appId, "Must supply an application id");
284 checkNotNull(id, "id cannot be null");
285 checkNotNull(type, "The type cannot be null");
286 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
287
288 return new DefaultNextObjective(this);
289 }
290
291 @Override
292 public NextObjective removeFromExisting() {
293 treatments = listBuilder.build();
294 op = Operation.REMOVE_FROM_EXISTING;
295 checkNotNull(appId, "Must supply an application id");
296 checkNotNull(id, "id cannot be null");
297 checkNotNull(type, "The type cannot be null");
298
299 return new DefaultNextObjective(this);
300 }
301
302 @Override
303 public NextObjective addToExisting(ObjectiveContext context) {
304 treatments = listBuilder.build();
305 op = Operation.ADD_TO_EXISTING;
306 this.context = context;
307 checkNotNull(appId, "Must supply an application id");
308 checkNotNull(id, "id cannot be null");
309 checkNotNull(type, "The type cannot be null");
310 checkArgument(!treatments.isEmpty(), "Must have at least one treatment");
311
312 return new DefaultNextObjective(this);
313 }
314
315 @Override
316 public NextObjective removeFromExisting(ObjectiveContext context) {
317 treatments = listBuilder.build();
318 op = Operation.REMOVE_FROM_EXISTING;
319 this.context = context;
320 checkNotNull(appId, "Must supply an application id");
321 checkNotNull(id, "id cannot be null");
322 checkNotNull(type, "The type cannot be null");
323
324 return new DefaultNextObjective(this);
325 }
326
alshabibfaa1e362015-04-02 15:01:54 -0700327 }
Saurav Das8a0732e2015-11-20 15:27:53 -0800328
alshabibfaa1e362015-04-02 15:01:54 -0700329}