blob: bea1e9689224bc89c3a7ee3d67e39be1d7e18359 [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;
Saurav Das8a0732e2015-11-20 15:27:53 -080019
alshabibfaa1e362015-04-02 15:01:54 -070020import org.onosproject.core.ApplicationId;
Andrea Campanellacbd16942021-06-01 16:33:14 +020021import org.onosproject.net.AbstractAnnotated;
22import org.onosproject.net.Annotations;
alshabibfaa1e362015-04-02 15:01:54 -070023import org.onosproject.net.flow.TrafficSelector;
24import org.onosproject.net.flow.TrafficTreatment;
25
26import 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 forwarding objective.
35 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070036@Beta
Andrea Campanellacbd16942021-06-01 16:33:14 +020037public final class DefaultForwardingObjective extends AbstractAnnotated
38 implements ForwardingObjective {
alshabibfaa1e362015-04-02 15:01:54 -070039
40 private final TrafficSelector selector;
41 private final Flag flag;
42 private final boolean permanent;
43 private final int timeout;
44 private final ApplicationId appId;
45 private final int priority;
Jonathan Hart17d00452015-04-21 17:10:00 -070046 private final Integer nextId;
alshabibfaa1e362015-04-02 15:01:54 -070047 private final TrafficTreatment treatment;
48 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070049 private final Optional<ObjectiveContext> context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -070050 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070051
52 private final int id;
53
Ray Milkey810d6e72015-08-14 10:35:06 -070054 private DefaultForwardingObjective(Builder builder) {
Andrea Campanellacbd16942021-06-01 16:33:14 +020055 super(builder.annotations);
Ray Milkey810d6e72015-08-14 10:35:06 -070056 this.selector = builder.selector;
57 this.flag = builder.flag;
58 this.permanent = builder.permanent;
59 this.timeout = builder.timeout;
60 this.appId = builder.appId;
61 this.priority = builder.priority;
62 this.nextId = builder.nextId;
63 this.treatment = builder.treatment;
64 this.op = builder.op;
65 this.context = Optional.ofNullable(builder.context);
Charles Chan4ca2f7f2016-03-25 13:40:16 -070066 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070067
68 this.id = Objects.hash(selector, flag, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070069 timeout, appId, priority, nextId,
70 treatment, op);
alshabibfaa1e362015-04-02 15:01:54 -070071 }
72
73
74 @Override
75 public TrafficSelector selector() {
76 return selector;
77 }
78
79 @Override
80 public Integer nextId() {
81 return nextId;
82 }
83
84 @Override
85 public TrafficTreatment treatment() {
86 return treatment;
87 }
88
89
90 @Override
91 public Flag flag() {
92 return flag;
93 }
94
95 @Override
96 public int id() {
97 return id;
98 }
99
100 @Override
101 public int priority() {
102 return priority;
103 }
104
105 @Override
106 public ApplicationId appId() {
107 return appId;
108 }
109
110 @Override
111 public int timeout() {
112 return timeout;
113 }
114
115 @Override
116 public boolean permanent() {
117 return permanent;
118 }
119
120 @Override
121 public Operation op() {
122 return op;
123 }
124
alshabib2a441c62015-04-13 18:39:38 -0700125 @Override
126 public Optional<ObjectiveContext> context() {
127 return context;
128 }
129
Saurav Das8a0732e2015-11-20 15:27:53 -0800130 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700131 public TrafficSelector meta() {
132 return meta;
133 }
134
135 @Override
Saurav Das8a0732e2015-11-20 15:27:53 -0800136 public int hashCode() {
Thomas Vachuska00f48162016-02-29 17:07:23 -0800137 return Objects.hash(selector, flag, permanent, timeout, appId,
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700138 priority, nextId, treatment, op, meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800139 }
140
Saurav Das8a0732e2015-11-20 15:27:53 -0800141 @Override
Thomas Vachuska00f48162016-02-29 17:07:23 -0800142 public boolean equals(Object obj) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800143 if (this == obj) {
144 return true;
145 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800146 if (obj instanceof DefaultForwardingObjective) {
147 final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
148 return Objects.equals(this.selector, other.selector)
149 && Objects.equals(this.flag, other.flag)
150 && Objects.equals(this.permanent, other.permanent)
151 && Objects.equals(this.timeout, other.timeout)
152 && Objects.equals(this.appId, other.appId)
153 && Objects.equals(this.priority, other.priority)
154 && Objects.equals(this.nextId, other.nextId)
155 && Objects.equals(this.treatment, other.treatment)
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700156 && Objects.equals(this.op, other.op)
157 && Objects.equals(this.meta, other.meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800158 }
159 return false;
160 }
161
Charles Chan3e041292016-02-27 15:58:43 -0800162 @Override
163 public String toString() {
164 return toStringHelper(this)
165 .add("id", id())
166 .add("op", op())
167 .add("priority", priority())
168 .add("selector", selector())
169 .add("treatment", treatment())
170 .add("nextId", nextId())
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700171 .add("meta", meta())
Charles Chan3e041292016-02-27 15:58:43 -0800172 .add("flag", flag())
173 .add("appId", appId())
174 .add("permanent", permanent())
175 .add("timeout", timeout())
Andrea Campanellacbd16942021-06-01 16:33:14 +0200176 .add("annotations", annotations())
Charles Chan3e041292016-02-27 15:58:43 -0800177 .toString();
178 }
179
alshabibfaa1e362015-04-02 15:01:54 -0700180 /**
181 * Returns a new builder.
182 *
183 * @return new builder
184 */
185 public static Builder builder() {
186 return new Builder();
187 }
188
Pier Luigif90c6502017-03-16 10:06:21 +0100189 /**
190 * Returns a new builder primed to produce entities
191 * patterned after the supplied forwarding objective.
192 *
193 * @param fwd base fwd
194 * @return forwarding objective builder
195 */
196 public static Builder builder(ForwardingObjective fwd) {
197 return new Builder(fwd);
198 }
199
Thomas Vachuska00f48162016-02-29 17:07:23 -0800200
201 @Override
202 public Builder copy() {
203 return new Builder(this);
204 }
205
206
alshabibfaa1e362015-04-02 15:01:54 -0700207 public static final class Builder implements ForwardingObjective.Builder {
208
209 private TrafficSelector selector;
210 private Flag flag;
211 private boolean permanent = DEFAULT_PERMANENT;
212 private int timeout = DEFAULT_TIMEOUT;
213 private int priority = DEFAULT_PRIORITY;
214 private ApplicationId appId;
215 private Integer nextId;
216 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700217 private Operation op;
218 private ObjectiveContext context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700219 private TrafficSelector meta;
Andrea Campanellacbd16942021-06-01 16:33:14 +0200220 private Annotations annotations;
221
alshabibfaa1e362015-04-02 15:01:54 -0700222
Thomas Vachuska00f48162016-02-29 17:07:23 -0800223 // Creates an empty builder
224 private Builder() {
225 }
226
227 // Creates a builder set to create a copy of the specified objective.
228 private Builder(ForwardingObjective objective) {
229 this.selector = objective.selector();
230 this.flag = objective.flag();
231 this.permanent = objective.permanent();
232 this.timeout = objective.timeout();
233 this.priority = objective.priority();
234 this.appId = objective.appId();
235 this.nextId = objective.nextId();
236 this.treatment = objective.treatment();
237 this.op = objective.op();
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700238 this.meta = objective.meta();
Andrea Campanellacbd16942021-06-01 16:33:14 +0200239 this.annotations = objective.annotations();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800240 }
241
alshabibfaa1e362015-04-02 15:01:54 -0700242 @Override
243 public Builder withSelector(TrafficSelector selector) {
244 this.selector = selector;
245 return this;
246 }
247
248 @Override
249 public Builder nextStep(int nextId) {
250 this.nextId = nextId;
251 return this;
252 }
253
254 @Override
255 public Builder withTreatment(TrafficTreatment treatment) {
256 this.treatment = treatment;
257 return this;
258 }
259
260 @Override
261 public Builder withFlag(Flag flag) {
262 this.flag = flag;
263 return this;
264 }
265
266 @Override
267 public Builder makeTemporary(int timeout) {
268 this.timeout = timeout;
269 this.permanent = false;
270 return this;
271 }
272
273 @Override
274 public Builder makePermanent() {
275 this.permanent = true;
276 return this;
277 }
278
279 @Override
280 public Builder fromApp(ApplicationId appId) {
281 this.appId = appId;
282 return this;
283 }
284
285 @Override
286 public Builder withPriority(int priority) {
287 this.priority = priority;
288 return this;
289 }
290
291 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700292 public Builder withMeta(TrafficSelector meta) {
293 this.meta = meta;
294 return this;
295 }
296
297 @Override
Andrea Campanellacbd16942021-06-01 16:33:14 +0200298 public Builder withAnnotations(Annotations annotations) {
299 this.annotations = annotations;
300 return this;
301 }
302
303 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700304 public ForwardingObjective add() {
305 checkNotNull(selector, "Must have a selector");
306 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700307 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700308 "least a treatment and/or a nextId");
309 checkNotNull(appId, "Must supply an application id");
Jayasree Ghosh3684dc72016-06-09 18:07:19 +0530310 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " +
311 "out of range");
Ray Milkey810d6e72015-08-14 10:35:06 -0700312 op = Operation.ADD;
313 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700314 }
315
316 @Override
317 public ForwardingObjective remove() {
318 checkNotNull(selector, "Must have a selector");
319 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700320 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700321 "least a treatment and/or a nextId");
322 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700323 op = Operation.REMOVE;
324 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700325 }
alshabib2a441c62015-04-13 18:39:38 -0700326
327 @Override
328 public ForwardingObjective add(ObjectiveContext context) {
329 checkNotNull(selector, "Must have a selector");
330 checkNotNull(flag, "A flag must be set");
331 checkArgument(nextId != null || treatment != null, "Must supply at " +
332 "least a treatment and/or a nextId");
333 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700334 op = Operation.ADD;
335 this.context = context;
336
337 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700338 }
339
340 @Override
341 public ForwardingObjective remove(ObjectiveContext context) {
342 checkNotNull(selector, "Must have a selector");
343 checkNotNull(flag, "A flag must be set");
344 checkArgument(nextId != null || treatment != null, "Must supply at " +
345 "least a treatment and/or a nextId");
346 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700347 op = Operation.REMOVE;
348 this.context = context;
349
350 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700351 }
alshabibfaa1e362015-04-02 15:01:54 -0700352 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800353
alshabibfaa1e362015-04-02 15:01:54 -0700354}