blob: fd08fd4ef9f4e1ca406ec0b858c779e0c50bcffd [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;
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;
alshabibfaa1e362015-04-02 15:01:54 -070047
48 private final int id;
49
Ray Milkey810d6e72015-08-14 10:35:06 -070050 private DefaultForwardingObjective(Builder builder) {
51 this.selector = builder.selector;
52 this.flag = builder.flag;
53 this.permanent = builder.permanent;
54 this.timeout = builder.timeout;
55 this.appId = builder.appId;
56 this.priority = builder.priority;
57 this.nextId = builder.nextId;
58 this.treatment = builder.treatment;
59 this.op = builder.op;
60 this.context = Optional.ofNullable(builder.context);
alshabib2a441c62015-04-13 18:39:38 -070061
62 this.id = Objects.hash(selector, flag, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070063 timeout, appId, priority, nextId,
64 treatment, op);
alshabibfaa1e362015-04-02 15:01:54 -070065 }
66
67
68 @Override
69 public TrafficSelector selector() {
70 return selector;
71 }
72
73 @Override
74 public Integer nextId() {
75 return nextId;
76 }
77
78 @Override
79 public TrafficTreatment treatment() {
80 return treatment;
81 }
82
83
84 @Override
85 public Flag flag() {
86 return flag;
87 }
88
89 @Override
90 public int id() {
91 return id;
92 }
93
94 @Override
95 public int priority() {
96 return priority;
97 }
98
99 @Override
100 public ApplicationId appId() {
101 return appId;
102 }
103
104 @Override
105 public int timeout() {
106 return timeout;
107 }
108
109 @Override
110 public boolean permanent() {
111 return permanent;
112 }
113
114 @Override
115 public Operation op() {
116 return op;
117 }
118
alshabib2a441c62015-04-13 18:39:38 -0700119 @Override
120 public Optional<ObjectiveContext> context() {
121 return context;
122 }
123
Saurav Das8a0732e2015-11-20 15:27:53 -0800124 @Override
125 public int hashCode() {
Thomas Vachuska00f48162016-02-29 17:07:23 -0800126 return Objects.hash(selector, flag, permanent, timeout, appId,
127 priority, nextId, treatment, op);
Saurav Das8a0732e2015-11-20 15:27:53 -0800128 }
129
Saurav Das8a0732e2015-11-20 15:27:53 -0800130 @Override
Thomas Vachuska00f48162016-02-29 17:07:23 -0800131 public boolean equals(Object obj) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800132 if (this == obj) {
133 return true;
134 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800135 if (obj instanceof DefaultForwardingObjective) {
136 final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
137 return Objects.equals(this.selector, other.selector)
138 && Objects.equals(this.flag, other.flag)
139 && Objects.equals(this.permanent, other.permanent)
140 && Objects.equals(this.timeout, other.timeout)
141 && Objects.equals(this.appId, other.appId)
142 && Objects.equals(this.priority, other.priority)
143 && Objects.equals(this.nextId, other.nextId)
144 && Objects.equals(this.treatment, other.treatment)
145 && Objects.equals(this.op, other.op);
Saurav Das8a0732e2015-11-20 15:27:53 -0800146 }
147 return false;
148 }
149
Charles Chan3e041292016-02-27 15:58:43 -0800150 @Override
151 public String toString() {
152 return toStringHelper(this)
153 .add("id", id())
154 .add("op", op())
155 .add("priority", priority())
156 .add("selector", selector())
157 .add("treatment", treatment())
158 .add("nextId", nextId())
159 .add("flag", flag())
160 .add("appId", appId())
161 .add("permanent", permanent())
162 .add("timeout", timeout())
163 .add("context", context())
164 .toString();
165 }
166
alshabibfaa1e362015-04-02 15:01:54 -0700167 /**
168 * Returns a new builder.
169 *
170 * @return new builder
171 */
172 public static Builder builder() {
173 return new Builder();
174 }
175
Thomas Vachuska00f48162016-02-29 17:07:23 -0800176
177 @Override
178 public Builder copy() {
179 return new Builder(this);
180 }
181
182
alshabibfaa1e362015-04-02 15:01:54 -0700183 public static final class Builder implements ForwardingObjective.Builder {
184
185 private TrafficSelector selector;
186 private Flag flag;
187 private boolean permanent = DEFAULT_PERMANENT;
188 private int timeout = DEFAULT_TIMEOUT;
189 private int priority = DEFAULT_PRIORITY;
190 private ApplicationId appId;
191 private Integer nextId;
192 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700193 private Operation op;
194 private ObjectiveContext context;
alshabibfaa1e362015-04-02 15:01:54 -0700195
Thomas Vachuska00f48162016-02-29 17:07:23 -0800196 // Creates an empty builder
197 private Builder() {
198 }
199
200 // Creates a builder set to create a copy of the specified objective.
201 private Builder(ForwardingObjective objective) {
202 this.selector = objective.selector();
203 this.flag = objective.flag();
204 this.permanent = objective.permanent();
205 this.timeout = objective.timeout();
206 this.priority = objective.priority();
207 this.appId = objective.appId();
208 this.nextId = objective.nextId();
209 this.treatment = objective.treatment();
210 this.op = objective.op();
211 }
212
alshabibfaa1e362015-04-02 15:01:54 -0700213 @Override
214 public Builder withSelector(TrafficSelector selector) {
215 this.selector = selector;
216 return this;
217 }
218
219 @Override
220 public Builder nextStep(int nextId) {
221 this.nextId = nextId;
222 return this;
223 }
224
225 @Override
226 public Builder withTreatment(TrafficTreatment treatment) {
227 this.treatment = treatment;
228 return this;
229 }
230
231 @Override
232 public Builder withFlag(Flag flag) {
233 this.flag = flag;
234 return this;
235 }
236
237 @Override
238 public Builder makeTemporary(int timeout) {
239 this.timeout = timeout;
240 this.permanent = false;
241 return this;
242 }
243
244 @Override
245 public Builder makePermanent() {
246 this.permanent = true;
247 return this;
248 }
249
250 @Override
251 public Builder fromApp(ApplicationId appId) {
252 this.appId = appId;
253 return this;
254 }
255
256 @Override
257 public Builder withPriority(int priority) {
258 this.priority = priority;
259 return this;
260 }
261
262 @Override
263 public ForwardingObjective add() {
264 checkNotNull(selector, "Must have a selector");
265 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700266 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700267 "least a treatment and/or a nextId");
268 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700269 op = Operation.ADD;
270 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700271 }
272
273 @Override
274 public ForwardingObjective remove() {
275 checkNotNull(selector, "Must have a selector");
276 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700277 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700278 "least a treatment and/or a nextId");
279 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700280 op = Operation.REMOVE;
281 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700282 }
alshabib2a441c62015-04-13 18:39:38 -0700283
284 @Override
285 public ForwardingObjective add(ObjectiveContext context) {
286 checkNotNull(selector, "Must have a selector");
287 checkNotNull(flag, "A flag must be set");
288 checkArgument(nextId != null || treatment != null, "Must supply at " +
289 "least a treatment and/or a nextId");
290 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700291 op = Operation.ADD;
292 this.context = context;
293
294 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700295 }
296
297 @Override
298 public ForwardingObjective remove(ObjectiveContext context) {
299 checkNotNull(selector, "Must have a selector");
300 checkNotNull(flag, "A flag must be set");
301 checkArgument(nextId != null || treatment != null, "Must supply at " +
302 "least a treatment and/or a nextId");
303 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700304 op = Operation.REMOVE;
305 this.context = context;
306
307 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700308 }
alshabibfaa1e362015-04-02 15:01:54 -0700309 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800310
alshabibfaa1e362015-04-02 15:01:54 -0700311}