blob: a44b05b808a93fe56081892e806f581e3ed55893 [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;
21import org.onosproject.net.flow.TrafficSelector;
22import org.onosproject.net.flow.TrafficTreatment;
23
24import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070025import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070026
Charles Chan3e041292016-02-27 15:58:43 -080027import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070028import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Default implementation of a forwarding objective.
33 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070034@Beta
alshabibfaa1e362015-04-02 15:01:54 -070035public final class DefaultForwardingObjective implements ForwardingObjective {
36
37 private final TrafficSelector selector;
38 private final Flag flag;
39 private final boolean permanent;
40 private final int timeout;
41 private final ApplicationId appId;
42 private final int priority;
Jonathan Hart17d00452015-04-21 17:10:00 -070043 private final Integer nextId;
alshabibfaa1e362015-04-02 15:01:54 -070044 private final TrafficTreatment treatment;
45 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070046 private final Optional<ObjectiveContext> context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -070047 private final TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -070048
49 private final int id;
50
Ray Milkey810d6e72015-08-14 10:35:06 -070051 private DefaultForwardingObjective(Builder builder) {
52 this.selector = builder.selector;
53 this.flag = builder.flag;
54 this.permanent = builder.permanent;
55 this.timeout = builder.timeout;
56 this.appId = builder.appId;
57 this.priority = builder.priority;
58 this.nextId = builder.nextId;
59 this.treatment = builder.treatment;
60 this.op = builder.op;
61 this.context = Optional.ofNullable(builder.context);
Charles Chan4ca2f7f2016-03-25 13:40:16 -070062 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070063
64 this.id = Objects.hash(selector, flag, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070065 timeout, appId, priority, nextId,
66 treatment, op);
alshabibfaa1e362015-04-02 15:01:54 -070067 }
68
69
70 @Override
71 public TrafficSelector selector() {
72 return selector;
73 }
74
75 @Override
76 public Integer nextId() {
77 return nextId;
78 }
79
80 @Override
81 public TrafficTreatment treatment() {
82 return treatment;
83 }
84
85
86 @Override
87 public Flag flag() {
88 return flag;
89 }
90
91 @Override
92 public int id() {
93 return id;
94 }
95
96 @Override
97 public int priority() {
98 return priority;
99 }
100
101 @Override
102 public ApplicationId appId() {
103 return appId;
104 }
105
106 @Override
107 public int timeout() {
108 return timeout;
109 }
110
111 @Override
112 public boolean permanent() {
113 return permanent;
114 }
115
116 @Override
117 public Operation op() {
118 return op;
119 }
120
alshabib2a441c62015-04-13 18:39:38 -0700121 @Override
122 public Optional<ObjectiveContext> context() {
123 return context;
124 }
125
Saurav Das8a0732e2015-11-20 15:27:53 -0800126 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700127 public TrafficSelector meta() {
128 return meta;
129 }
130
131 @Override
Saurav Das8a0732e2015-11-20 15:27:53 -0800132 public int hashCode() {
Thomas Vachuska00f48162016-02-29 17:07:23 -0800133 return Objects.hash(selector, flag, permanent, timeout, appId,
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700134 priority, nextId, treatment, op, meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800135 }
136
Saurav Das8a0732e2015-11-20 15:27:53 -0800137 @Override
Thomas Vachuska00f48162016-02-29 17:07:23 -0800138 public boolean equals(Object obj) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800139 if (this == obj) {
140 return true;
141 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800142 if (obj instanceof DefaultForwardingObjective) {
143 final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
144 return Objects.equals(this.selector, other.selector)
145 && Objects.equals(this.flag, other.flag)
146 && Objects.equals(this.permanent, other.permanent)
147 && Objects.equals(this.timeout, other.timeout)
148 && Objects.equals(this.appId, other.appId)
149 && Objects.equals(this.priority, other.priority)
150 && Objects.equals(this.nextId, other.nextId)
151 && Objects.equals(this.treatment, other.treatment)
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700152 && Objects.equals(this.op, other.op)
153 && Objects.equals(this.meta, other.meta);
Saurav Das8a0732e2015-11-20 15:27:53 -0800154 }
155 return false;
156 }
157
Charles Chan3e041292016-02-27 15:58:43 -0800158 @Override
159 public String toString() {
160 return toStringHelper(this)
161 .add("id", id())
162 .add("op", op())
163 .add("priority", priority())
164 .add("selector", selector())
165 .add("treatment", treatment())
166 .add("nextId", nextId())
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700167 .add("meta", meta())
Charles Chan3e041292016-02-27 15:58:43 -0800168 .add("flag", flag())
169 .add("appId", appId())
170 .add("permanent", permanent())
171 .add("timeout", timeout())
Charles Chan3e041292016-02-27 15:58:43 -0800172 .toString();
173 }
174
alshabibfaa1e362015-04-02 15:01:54 -0700175 /**
176 * Returns a new builder.
177 *
178 * @return new builder
179 */
180 public static Builder builder() {
181 return new Builder();
182 }
183
Pier Luigif90c6502017-03-16 10:06:21 +0100184 /**
185 * Returns a new builder primed to produce entities
186 * patterned after the supplied forwarding objective.
187 *
188 * @param fwd base fwd
189 * @return forwarding objective builder
190 */
191 public static Builder builder(ForwardingObjective fwd) {
192 return new Builder(fwd);
193 }
194
Thomas Vachuska00f48162016-02-29 17:07:23 -0800195
196 @Override
197 public Builder copy() {
198 return new Builder(this);
199 }
200
201
alshabibfaa1e362015-04-02 15:01:54 -0700202 public static final class Builder implements ForwardingObjective.Builder {
203
204 private TrafficSelector selector;
205 private Flag flag;
206 private boolean permanent = DEFAULT_PERMANENT;
207 private int timeout = DEFAULT_TIMEOUT;
208 private int priority = DEFAULT_PRIORITY;
209 private ApplicationId appId;
210 private Integer nextId;
211 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700212 private Operation op;
213 private ObjectiveContext context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700214 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700215
Thomas Vachuska00f48162016-02-29 17:07:23 -0800216 // Creates an empty builder
217 private Builder() {
218 }
219
220 // Creates a builder set to create a copy of the specified objective.
221 private Builder(ForwardingObjective objective) {
222 this.selector = objective.selector();
223 this.flag = objective.flag();
224 this.permanent = objective.permanent();
225 this.timeout = objective.timeout();
226 this.priority = objective.priority();
227 this.appId = objective.appId();
228 this.nextId = objective.nextId();
229 this.treatment = objective.treatment();
230 this.op = objective.op();
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700231 this.meta = objective.meta();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800232 }
233
alshabibfaa1e362015-04-02 15:01:54 -0700234 @Override
235 public Builder withSelector(TrafficSelector selector) {
236 this.selector = selector;
237 return this;
238 }
239
240 @Override
241 public Builder nextStep(int nextId) {
242 this.nextId = nextId;
243 return this;
244 }
245
246 @Override
247 public Builder withTreatment(TrafficTreatment treatment) {
248 this.treatment = treatment;
249 return this;
250 }
251
252 @Override
253 public Builder withFlag(Flag flag) {
254 this.flag = flag;
255 return this;
256 }
257
258 @Override
259 public Builder makeTemporary(int timeout) {
260 this.timeout = timeout;
261 this.permanent = false;
262 return this;
263 }
264
265 @Override
266 public Builder makePermanent() {
267 this.permanent = true;
268 return this;
269 }
270
271 @Override
272 public Builder fromApp(ApplicationId appId) {
273 this.appId = appId;
274 return this;
275 }
276
277 @Override
278 public Builder withPriority(int priority) {
279 this.priority = priority;
280 return this;
281 }
282
283 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700284 public Builder withMeta(TrafficSelector meta) {
285 this.meta = meta;
286 return this;
287 }
288
289 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700290 public ForwardingObjective add() {
291 checkNotNull(selector, "Must have a selector");
292 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700293 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700294 "least a treatment and/or a nextId");
295 checkNotNull(appId, "Must supply an application id");
Jayasree Ghosh3684dc72016-06-09 18:07:19 +0530296 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " +
297 "out of range");
Ray Milkey810d6e72015-08-14 10:35:06 -0700298 op = Operation.ADD;
299 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700300 }
301
302 @Override
303 public ForwardingObjective remove() {
304 checkNotNull(selector, "Must have a selector");
305 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700306 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700307 "least a treatment and/or a nextId");
308 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700309 op = Operation.REMOVE;
310 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700311 }
alshabib2a441c62015-04-13 18:39:38 -0700312
313 @Override
314 public ForwardingObjective add(ObjectiveContext context) {
315 checkNotNull(selector, "Must have a selector");
316 checkNotNull(flag, "A flag must be set");
317 checkArgument(nextId != null || treatment != null, "Must supply at " +
318 "least a treatment and/or a nextId");
319 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700320 op = Operation.ADD;
321 this.context = context;
322
323 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700324 }
325
326 @Override
327 public ForwardingObjective remove(ObjectiveContext context) {
328 checkNotNull(selector, "Must have a selector");
329 checkNotNull(flag, "A flag must be set");
330 checkArgument(nextId != null || treatment != null, "Must supply at " +
331 "least a treatment and/or a nextId");
332 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700333 op = Operation.REMOVE;
334 this.context = context;
335
336 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700337 }
alshabibfaa1e362015-04-02 15:01:54 -0700338 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800339
alshabibfaa1e362015-04-02 15:01:54 -0700340}