blob: 7c442b20b55791b0c38595f5b6a42885db7e7b0b [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
27import static com.google.common.base.Preconditions.checkArgument;
28import static com.google.common.base.Preconditions.checkNotNull;
29
30/**
31 * Default implementation of a forwarding objective.
32 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070033@Beta
alshabibfaa1e362015-04-02 15:01:54 -070034public final class DefaultForwardingObjective implements ForwardingObjective {
35
36 private final TrafficSelector selector;
37 private final Flag flag;
38 private final boolean permanent;
39 private final int timeout;
40 private final ApplicationId appId;
41 private final int priority;
Jonathan Hart17d00452015-04-21 17:10:00 -070042 private final Integer nextId;
alshabibfaa1e362015-04-02 15:01:54 -070043 private final TrafficTreatment treatment;
44 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070045 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070046
47 private final int id;
48
Ray Milkey810d6e72015-08-14 10:35:06 -070049 private DefaultForwardingObjective(Builder builder) {
50 this.selector = builder.selector;
51 this.flag = builder.flag;
52 this.permanent = builder.permanent;
53 this.timeout = builder.timeout;
54 this.appId = builder.appId;
55 this.priority = builder.priority;
56 this.nextId = builder.nextId;
57 this.treatment = builder.treatment;
58 this.op = builder.op;
59 this.context = Optional.ofNullable(builder.context);
alshabib2a441c62015-04-13 18:39:38 -070060
61 this.id = Objects.hash(selector, flag, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070062 timeout, appId, priority, nextId,
63 treatment, op);
alshabibfaa1e362015-04-02 15:01:54 -070064 }
65
66
67 @Override
68 public TrafficSelector selector() {
69 return selector;
70 }
71
72 @Override
73 public Integer nextId() {
74 return nextId;
75 }
76
77 @Override
78 public TrafficTreatment treatment() {
79 return treatment;
80 }
81
82
83 @Override
84 public Flag flag() {
85 return flag;
86 }
87
88 @Override
89 public int id() {
90 return id;
91 }
92
93 @Override
94 public int priority() {
95 return priority;
96 }
97
98 @Override
99 public ApplicationId appId() {
100 return appId;
101 }
102
103 @Override
104 public int timeout() {
105 return timeout;
106 }
107
108 @Override
109 public boolean permanent() {
110 return permanent;
111 }
112
113 @Override
114 public Operation op() {
115 return op;
116 }
117
alshabib2a441c62015-04-13 18:39:38 -0700118 @Override
119 public Optional<ObjectiveContext> context() {
120 return context;
121 }
122
Saurav Das8a0732e2015-11-20 15:27:53 -0800123 @Override
124 public int hashCode() {
Thomas Vachuska00f48162016-02-29 17:07:23 -0800125 return Objects.hash(selector, flag, permanent, timeout, appId,
126 priority, nextId, treatment, op);
Saurav Das8a0732e2015-11-20 15:27:53 -0800127 }
128
Saurav Das8a0732e2015-11-20 15:27:53 -0800129 @Override
Thomas Vachuska00f48162016-02-29 17:07:23 -0800130 public boolean equals(Object obj) {
Saurav Das8a0732e2015-11-20 15:27:53 -0800131 if (this == obj) {
132 return true;
133 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800134 if (obj instanceof DefaultForwardingObjective) {
135 final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
136 return Objects.equals(this.selector, other.selector)
137 && Objects.equals(this.flag, other.flag)
138 && Objects.equals(this.permanent, other.permanent)
139 && Objects.equals(this.timeout, other.timeout)
140 && Objects.equals(this.appId, other.appId)
141 && Objects.equals(this.priority, other.priority)
142 && Objects.equals(this.nextId, other.nextId)
143 && Objects.equals(this.treatment, other.treatment)
144 && Objects.equals(this.op, other.op);
Saurav Das8a0732e2015-11-20 15:27:53 -0800145 }
146 return false;
147 }
148
alshabibfaa1e362015-04-02 15:01:54 -0700149 /**
150 * Returns a new builder.
151 *
152 * @return new builder
153 */
154 public static Builder builder() {
155 return new Builder();
156 }
157
Thomas Vachuska00f48162016-02-29 17:07:23 -0800158
159 @Override
160 public Builder copy() {
161 return new Builder(this);
162 }
163
164
alshabibfaa1e362015-04-02 15:01:54 -0700165 public static final class Builder implements ForwardingObjective.Builder {
166
167 private TrafficSelector selector;
168 private Flag flag;
169 private boolean permanent = DEFAULT_PERMANENT;
170 private int timeout = DEFAULT_TIMEOUT;
171 private int priority = DEFAULT_PRIORITY;
172 private ApplicationId appId;
173 private Integer nextId;
174 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700175 private Operation op;
176 private ObjectiveContext context;
alshabibfaa1e362015-04-02 15:01:54 -0700177
Thomas Vachuska00f48162016-02-29 17:07:23 -0800178 // Creates an empty builder
179 private Builder() {
180 }
181
182 // Creates a builder set to create a copy of the specified objective.
183 private Builder(ForwardingObjective objective) {
184 this.selector = objective.selector();
185 this.flag = objective.flag();
186 this.permanent = objective.permanent();
187 this.timeout = objective.timeout();
188 this.priority = objective.priority();
189 this.appId = objective.appId();
190 this.nextId = objective.nextId();
191 this.treatment = objective.treatment();
192 this.op = objective.op();
193 }
194
alshabibfaa1e362015-04-02 15:01:54 -0700195 @Override
196 public Builder withSelector(TrafficSelector selector) {
197 this.selector = selector;
198 return this;
199 }
200
201 @Override
202 public Builder nextStep(int nextId) {
203 this.nextId = nextId;
204 return this;
205 }
206
207 @Override
208 public Builder withTreatment(TrafficTreatment treatment) {
209 this.treatment = treatment;
210 return this;
211 }
212
213 @Override
214 public Builder withFlag(Flag flag) {
215 this.flag = flag;
216 return this;
217 }
218
219 @Override
220 public Builder makeTemporary(int timeout) {
221 this.timeout = timeout;
222 this.permanent = false;
223 return this;
224 }
225
226 @Override
227 public Builder makePermanent() {
228 this.permanent = true;
229 return this;
230 }
231
232 @Override
233 public Builder fromApp(ApplicationId appId) {
234 this.appId = appId;
235 return this;
236 }
237
238 @Override
239 public Builder withPriority(int priority) {
240 this.priority = priority;
241 return this;
242 }
243
244 @Override
245 public ForwardingObjective add() {
246 checkNotNull(selector, "Must have a selector");
247 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700248 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700249 "least a treatment and/or a nextId");
250 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700251 op = Operation.ADD;
252 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700253 }
254
255 @Override
256 public ForwardingObjective remove() {
257 checkNotNull(selector, "Must have a selector");
258 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700259 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700260 "least a treatment and/or a nextId");
261 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700262 op = Operation.REMOVE;
263 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700264 }
alshabib2a441c62015-04-13 18:39:38 -0700265
266 @Override
267 public ForwardingObjective add(ObjectiveContext context) {
268 checkNotNull(selector, "Must have a selector");
269 checkNotNull(flag, "A flag must be set");
270 checkArgument(nextId != null || treatment != null, "Must supply at " +
271 "least a treatment and/or a nextId");
272 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700273 op = Operation.ADD;
274 this.context = context;
275
276 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700277 }
278
279 @Override
280 public ForwardingObjective remove(ObjectiveContext context) {
281 checkNotNull(selector, "Must have a selector");
282 checkNotNull(flag, "A flag must be set");
283 checkArgument(nextId != null || treatment != null, "Must supply at " +
284 "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.REMOVE;
287 this.context = context;
288
289 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700290 }
alshabibfaa1e362015-04-02 15:01:54 -0700291 }
Thomas Vachuska00f48162016-02-29 17:07:23 -0800292
alshabibfaa1e362015-04-02 15:01:54 -0700293}