blob: af4818057dd0a6209b87195d09507c1d61357b7f [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 /*
124 * (non-Javadoc)
125 *
126 * @see java.lang.Object#hashCode()
127 */
128 @Override
129 public int hashCode() {
130 return Objects.hash(selector, flag, permanent,
131 timeout, appId, priority, nextId,
132 treatment, op);
133 }
134
135 /*
136 * (non-Javadoc)
137 *
138 * @see java.lang.Object#equals(java.lang.Object)
139 */
140 @Override
141 public boolean equals(final Object obj) {
142 if (this == obj) {
143 return true;
144 }
145 if (!(obj instanceof DefaultForwardingObjective)) {
146 return false;
147 }
148 final DefaultForwardingObjective other = (DefaultForwardingObjective) obj;
149 boolean nextEq = false, treatmentEq = false;
150 if (this.selector.equals(other.selector) &&
151 this.flag == other.flag &&
152 this.permanent == other.permanent &&
153 this.timeout == other.timeout &&
154 this.appId.equals(other.appId) &&
155 this.priority == other.priority &&
156 this.op == other.op) {
157 if (this.nextId != null && other.nextId != null) {
158 nextEq = this.nextId == other.nextId;
159 }
160 if (this.treatment != null && other.treatment != null) {
161 treatmentEq = this.treatment.equals(other.treatment);
162 }
163 if (nextEq && treatmentEq) {
164 return true;
165 }
166 }
167 return false;
168 }
169
alshabibfaa1e362015-04-02 15:01:54 -0700170 /**
171 * Returns a new builder.
172 *
173 * @return new builder
174 */
175 public static Builder builder() {
176 return new Builder();
177 }
178
179 public static final class Builder implements ForwardingObjective.Builder {
180
181 private TrafficSelector selector;
182 private Flag flag;
183 private boolean permanent = DEFAULT_PERMANENT;
184 private int timeout = DEFAULT_TIMEOUT;
185 private int priority = DEFAULT_PRIORITY;
186 private ApplicationId appId;
187 private Integer nextId;
188 private TrafficTreatment treatment;
Ray Milkey810d6e72015-08-14 10:35:06 -0700189 private Operation op;
190 private ObjectiveContext context;
alshabibfaa1e362015-04-02 15:01:54 -0700191
192 @Override
193 public Builder withSelector(TrafficSelector selector) {
194 this.selector = selector;
195 return this;
196 }
197
198 @Override
199 public Builder nextStep(int nextId) {
200 this.nextId = nextId;
201 return this;
202 }
203
204 @Override
205 public Builder withTreatment(TrafficTreatment treatment) {
206 this.treatment = treatment;
207 return this;
208 }
209
210 @Override
211 public Builder withFlag(Flag flag) {
212 this.flag = flag;
213 return this;
214 }
215
216 @Override
217 public Builder makeTemporary(int timeout) {
218 this.timeout = timeout;
219 this.permanent = false;
220 return this;
221 }
222
223 @Override
224 public Builder makePermanent() {
225 this.permanent = true;
226 return this;
227 }
228
229 @Override
230 public Builder fromApp(ApplicationId appId) {
231 this.appId = appId;
232 return this;
233 }
234
235 @Override
236 public Builder withPriority(int priority) {
237 this.priority = priority;
238 return this;
239 }
240
241 @Override
242 public ForwardingObjective add() {
243 checkNotNull(selector, "Must have a selector");
244 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700245 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700246 "least a treatment and/or a nextId");
247 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700248 op = Operation.ADD;
249 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700250 }
251
252 @Override
253 public ForwardingObjective remove() {
254 checkNotNull(selector, "Must have a selector");
255 checkNotNull(flag, "A flag must be set");
alshabib2a441c62015-04-13 18:39:38 -0700256 checkArgument(nextId != null || treatment != null, "Must supply at " +
alshabibfaa1e362015-04-02 15:01:54 -0700257 "least a treatment and/or a nextId");
258 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700259 op = Operation.REMOVE;
260 return new DefaultForwardingObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700261 }
alshabib2a441c62015-04-13 18:39:38 -0700262
263 @Override
264 public ForwardingObjective add(ObjectiveContext context) {
265 checkNotNull(selector, "Must have a selector");
266 checkNotNull(flag, "A flag must be set");
267 checkArgument(nextId != null || treatment != null, "Must supply at " +
268 "least a treatment and/or a nextId");
269 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700270 op = Operation.ADD;
271 this.context = context;
272
273 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700274 }
275
276 @Override
277 public ForwardingObjective remove(ObjectiveContext context) {
278 checkNotNull(selector, "Must have a selector");
279 checkNotNull(flag, "A flag must be set");
280 checkArgument(nextId != null || treatment != null, "Must supply at " +
281 "least a treatment and/or a nextId");
282 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700283 op = Operation.REMOVE;
284 this.context = context;
285
286 return new DefaultForwardingObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700287 }
alshabibfaa1e362015-04-02 15:01:54 -0700288 }
289}