blob: 84e38aff3ec66c89ef1274bc7a32fc9e390a2167 [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())
172 .add("context", context())
173 .toString();
174 }
175
alshabibfaa1e362015-04-02 15:01:54 -0700176 /**
177 * Returns a new builder.
178 *
179 * @return new builder
180 */
181 public static Builder builder() {
182 return new Builder();
183 }
184
Thomas Vachuska00f48162016-02-29 17:07:23 -0800185
186 @Override
187 public Builder copy() {
188 return new Builder(this);
189 }
190
191
alshabibfaa1e362015-04-02 15:01:54 -0700192 public static final class Builder implements ForwardingObjective.Builder {
193
194 private TrafficSelector selector;
195 private Flag flag;
196 private boolean permanent = DEFAULT_PERMANENT;
197 private int timeout = DEFAULT_TIMEOUT;
198 private int priority = DEFAULT_PRIORITY;
199 private ApplicationId appId;
200 private Integer nextId;
201 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700202 private Operation op;
203 private ObjectiveContext context;
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700204 private TrafficSelector meta;
alshabibfaa1e362015-04-02 15:01:54 -0700205
Thomas Vachuska00f48162016-02-29 17:07:23 -0800206 // Creates an empty builder
207 private Builder() {
208 }
209
210 // Creates a builder set to create a copy of the specified objective.
211 private Builder(ForwardingObjective objective) {
212 this.selector = objective.selector();
213 this.flag = objective.flag();
214 this.permanent = objective.permanent();
215 this.timeout = objective.timeout();
216 this.priority = objective.priority();
217 this.appId = objective.appId();
218 this.nextId = objective.nextId();
219 this.treatment = objective.treatment();
220 this.op = objective.op();
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700221 this.meta = objective.meta();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800222 }
223
alshabibfaa1e362015-04-02 15:01:54 -0700224 @Override
225 public Builder withSelector(TrafficSelector selector) {
226 this.selector = selector;
227 return this;
228 }
229
230 @Override
231 public Builder nextStep(int nextId) {
232 this.nextId = nextId;
233 return this;
234 }
235
236 @Override
237 public Builder withTreatment(TrafficTreatment treatment) {
238 this.treatment = treatment;
239 return this;
240 }
241
242 @Override
243 public Builder withFlag(Flag flag) {
244 this.flag = flag;
245 return this;
246 }
247
248 @Override
249 public Builder makeTemporary(int timeout) {
250 this.timeout = timeout;
251 this.permanent = false;
252 return this;
253 }
254
255 @Override
256 public Builder makePermanent() {
257 this.permanent = true;
258 return this;
259 }
260
261 @Override
262 public Builder fromApp(ApplicationId appId) {
263 this.appId = appId;
264 return this;
265 }
266
267 @Override
268 public Builder withPriority(int priority) {
269 this.priority = priority;
270 return this;
271 }
272
273 @Override
Charles Chan4ca2f7f2016-03-25 13:40:16 -0700274 public Builder withMeta(TrafficSelector meta) {
275 this.meta = meta;
276 return this;
277 }
278
279 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700280 public ForwardingObjective add() {
281 checkNotNull(selector, "Must have a selector");
282 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700283 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700284 "least a treatment and/or a nextId");
285 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700286 op = Operation.ADD;
287 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700288 }
289
290 @Override
291 public ForwardingObjective remove() {
292 checkNotNull(selector, "Must have a selector");
293 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700294 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700295 "least a treatment and/or a nextId");
296 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700297 op = Operation.REMOVE;
298 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700299 }
alshabib2a441c62015-04-13 18:39:38 -0700300
301 @Override
302 public ForwardingObjective add(ObjectiveContext context) {
303 checkNotNull(selector, "Must have a selector");
304 checkNotNull(flag, "A flag must be set");
305 checkArgument(nextId != null || treatment != null, "Must supply at " +
306 "least a treatment and/or a nextId");
307 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700308 op = Operation.ADD;
309 this.context = context;
310
311 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700312 }
313
314 @Override
315 public ForwardingObjective remove(ObjectiveContext context) {
316 checkNotNull(selector, "Must have a selector");
317 checkNotNull(flag, "A flag must be set");
318 checkArgument(nextId != null || treatment != null, "Must supply at " +
319 "least a treatment and/or a nextId");
320 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700321 op = Operation.REMOVE;
322 this.context = context;
323
324 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700325 }
alshabibfaa1e362015-04-02 15:01:54 -0700326 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800327
alshabibfaa1e362015-04-02 15:01:54 -0700328}