blob: f076fdab554774aca54fe73c197d7558da988e32 [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;
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
Thomas Vachuska00f48162016-02-29 17:07:23 -0800184
185 @Override
186 public Builder copy() {
187 return new Builder(this);
188 }
189
190
alshabibfaa1e362015-04-02 15:01:54 -0700191 public static final class Builder implements ForwardingObjective.Builder {
192
193 private TrafficSelector selector;
194 private Flag flag;
195 private boolean permanent = DEFAULT_PERMANENT;
196 private int timeout = DEFAULT_TIMEOUT;
197 private int priority = DEFAULT_PRIORITY;
198 private ApplicationId appId;
199 private Integer nextId;
200 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700201 private Operation op;
202 private ObjectiveContext context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700203 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700204
Thomas Vachuska00f48162016-02-29 17:07:23 -0800205 // Creates an empty builder
206 private Builder() {
207 }
208
209 // Creates a builder set to create a copy of the specified objective.
210 private Builder(ForwardingObjective objective) {
211 this.selector = objective.selector();
212 this.flag = objective.flag();
213 this.permanent = objective.permanent();
214 this.timeout = objective.timeout();
215 this.priority = objective.priority();
216 this.appId = objective.appId();
217 this.nextId = objective.nextId();
218 this.treatment = objective.treatment();
219 this.op = objective.op();
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700220 this.meta = objective.meta();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800221 }
222
alshabibfaa1e362015-04-02 15:01:54 -0700223 @Override
224 public Builder withSelector(TrafficSelector selector) {
225 this.selector = selector;
226 return this;
227 }
228
229 @Override
230 public Builder nextStep(int nextId) {
231 this.nextId = nextId;
232 return this;
233 }
234
235 @Override
236 public Builder withTreatment(TrafficTreatment treatment) {
237 this.treatment = treatment;
238 return this;
239 }
240
241 @Override
242 public Builder withFlag(Flag flag) {
243 this.flag = flag;
244 return this;
245 }
246
247 @Override
248 public Builder makeTemporary(int timeout) {
249 this.timeout = timeout;
250 this.permanent = false;
251 return this;
252 }
253
254 @Override
255 public Builder makePermanent() {
256 this.permanent = true;
257 return this;
258 }
259
260 @Override
261 public Builder fromApp(ApplicationId appId) {
262 this.appId = appId;
263 return this;
264 }
265
266 @Override
267 public Builder withPriority(int priority) {
268 this.priority = priority;
269 return this;
270 }
271
272 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700273 public Builder withMeta(TrafficSelector meta) {
274 this.meta = meta;
275 return this;
276 }
277
278 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700279 public ForwardingObjective add() {
280 checkNotNull(selector, "Must have a selector");
281 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700282 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700283 "least a treatment and/or a nextId");
284 checkNotNull(appId, "Must supply an application id");
Jayasree Ghosh3684dc72016-06-09 18:07:19 +0530285 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " +
286 "out of range");
Ray Milkey810d6e72015-08-14 10:35:06 -0700287 op = Operation.ADD;
288 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700289 }
290
291 @Override
292 public ForwardingObjective remove() {
293 checkNotNull(selector, "Must have a selector");
294 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700295 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700296 "least a treatment and/or a nextId");
297 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700298 op = Operation.REMOVE;
299 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700300 }
alshabib2a441c62015-04-13 18:39:38 -0700301
302 @Override
303 public ForwardingObjective add(ObjectiveContext context) {
304 checkNotNull(selector, "Must have a selector");
305 checkNotNull(flag, "A flag must be set");
306 checkArgument(nextId != null || treatment != null, "Must supply at " +
307 "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.ADD;
310 this.context = context;
311
312 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700313 }
314
315 @Override
316 public ForwardingObjective remove(ObjectiveContext context) {
317 checkNotNull(selector, "Must have a selector");
318 checkNotNull(flag, "A flag must be set");
319 checkArgument(nextId != null || treatment != null, "Must supply at " +
320 "least a treatment and/or a nextId");
321 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700322 op = Operation.REMOVE;
323 this.context = context;
324
325 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700326 }
alshabibfaa1e362015-04-02 15:01:54 -0700327 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800328
alshabibfaa1e362015-04-02 15:01:54 -0700329}