blob: 0eaf3485b800b6949f4ee08165dfcb79bac140c3 [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;
alshabibfaa1e362015-04-02 15:01:54 -070019import com.google.common.collect.ImmutableList;
20import org.onosproject.core.ApplicationId;
Saurav Das88f4c842015-10-27 13:13:19 -070021import org.onosproject.net.flow.TrafficTreatment;
alshabiba3a476d2015-04-10 14:35:38 -070022import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070023import org.onosproject.net.flow.criteria.Criterion;
24
25import java.util.Collection;
26import java.util.List;
27import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070028import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070029
Charles Chan3e041292016-02-27 15:58:43 -080030import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070031import static com.google.common.base.Preconditions.checkArgument;
32import static com.google.common.base.Preconditions.checkNotNull;
33
34/**
35 * Default implementation of a filtering objective.
36 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070037@Beta
alshabibfaa1e362015-04-02 15:01:54 -070038public final class DefaultFilteringObjective implements FilteringObjective {
39
alshabib910aff12015-04-09 16:55:57 -070040 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070041 private final boolean permanent;
42 private final int timeout;
43 private final ApplicationId appId;
44 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070045 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070046 private final List<Criterion> conditions;
47 private final int id;
48 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070049 private final Optional<ObjectiveContext> context;
Saurav Das88f4c842015-10-27 13:13:19 -070050 private final TrafficTreatment meta;
alshabibfaa1e362015-04-02 15:01:54 -070051
Ray Milkey810d6e72015-08-14 10:35:06 -070052 private DefaultFilteringObjective(Builder builder) {
53 this.key = builder.key;
54 this.type = builder.type;
55 this.permanent = builder.permanent;
56 this.timeout = builder.timeout;
57 this.appId = builder.appId;
58 this.priority = builder.priority;
59 this.conditions = builder.conditions;
60 this.op = builder.op;
61 this.context = Optional.ofNullable(builder.context);
Saurav Das88f4c842015-10-27 13:13:19 -070062 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070063
64 this.id = Objects.hash(type, key, conditions, permanent,
Thomas Vachuska00f48162016-02-29 17:07:23 -080065 timeout, appId, priority);
alshabibfaa1e362015-04-02 15:01:54 -070066 }
67
alshabibfaa1e362015-04-02 15:01:54 -070068 @Override
alshabiba3a476d2015-04-10 14:35:38 -070069 public Criterion key() {
70 return key;
71 }
72
73 @Override
alshabib910aff12015-04-09 16:55:57 -070074 public Type type() {
75 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070076 }
77
78 @Override
79 public Collection<Criterion> conditions() {
80 return conditions;
81 }
82
83 @Override
84 public int id() {
85 return id;
86 }
87
88 @Override
Saurav Das88f4c842015-10-27 13:13:19 -070089 public TrafficTreatment meta() {
90 return meta;
91 }
92
93
94 @Override
alshabibfaa1e362015-04-02 15:01:54 -070095 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
Thomas Vachuska00f48162016-02-29 17:07:23 -0800124 @Override
125 public int hashCode() {
126 return Objects.hash(type, permanent, timeout, appId, priority, key,
127 conditions, op, meta);
128 }
129
130 @Override
131 public boolean equals(Object obj) {
132 if (this == obj) {
133 return true;
134 }
135 if (obj instanceof DefaultFilteringObjective) {
136 final DefaultFilteringObjective other = (DefaultFilteringObjective) obj;
137 return Objects.equals(this.type, other.type)
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.key, other.key)
143 && Objects.equals(this.conditions, other.conditions)
144 && Objects.equals(this.op, other.op)
145 && Objects.equals(this.meta, other.meta);
146 }
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("type", type())
155 .add("op", op())
156 .add("priority", priority())
157 .add("key", key())
158 .add("conditions", conditions())
159 .add("meta", meta())
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 @Override
177 public Builder copy() {
178 return new Builder(this);
179 }
alshabibfaa1e362015-04-02 15:01:54 -0700180
181 public static final class Builder implements FilteringObjective.Builder {
182 private final ImmutableList.Builder<Criterion> listBuilder
183 = ImmutableList.builder();
184
alshabib910aff12015-04-09 16:55:57 -0700185 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700186 private boolean permanent = DEFAULT_PERMANENT;
187 private int timeout = DEFAULT_TIMEOUT;
188 private ApplicationId appId;
189 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700190 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700191 private List<Criterion> conditions;
192 private Operation op;
193 private ObjectiveContext context;
Saurav Das88f4c842015-10-27 13:13:19 -0700194 private TrafficTreatment meta;
alshabiba3a476d2015-04-10 14:35:38 -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(FilteringObjective objective) {
202 this.type = objective.type();
203 this.key = objective.key();
Konstantinos Kanonakisd7690cf2016-05-26 11:38:33 -0500204 objective.conditions().forEach(this::addCondition);
Thomas Vachuska00f48162016-02-29 17:07:23 -0800205 this.permanent = objective.permanent();
206 this.timeout = objective.timeout();
207 this.priority = objective.priority();
208 this.appId = objective.appId();
209 this.meta = objective.meta();
210 this.op = objective.op();
211 }
212
alshabiba3a476d2015-04-10 14:35:38 -0700213 @Override
214 public Builder withKey(Criterion key) {
215 this.key = key;
216 return this;
217 }
alshabibfaa1e362015-04-02 15:01:54 -0700218
219 @Override
220 public Builder addCondition(Criterion criterion) {
221 listBuilder.add(criterion);
222 return this;
223 }
224
225 @Override
alshabib910aff12015-04-09 16:55:57 -0700226 public Builder permit() {
227 this.type = Type.PERMIT;
228 return this;
229 }
230
231 @Override
232 public Builder deny() {
233 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700234 return this;
235 }
236
237 @Override
238 public Builder makeTemporary(int timeout) {
239 this.timeout = timeout;
240 permanent = false;
241 return this;
242 }
243
244 @Override
245 public Builder makePermanent() {
246 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
Saurav Das4ce45962015-11-24 23:21:05 -0800263 public Builder withMeta(TrafficTreatment treatment) {
Saurav Das88f4c842015-10-27 13:13:19 -0700264 this.meta = treatment;
265 return this;
266 }
267
268 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700269 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700270 conditions = listBuilder.build();
271 op = Operation.ADD;
alshabib910aff12015-04-09 16:55:57 -0700272 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700273 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
274 checkNotNull(appId, "Must supply an application id");
275
Ray Milkey810d6e72015-08-14 10:35:06 -0700276 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700277 }
278
279 @Override
280 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700281 conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700282 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700283 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
284 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700285 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700286
Ray Milkey810d6e72015-08-14 10:35:06 -0700287 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700288 }
289
alshabib2a441c62015-04-13 18:39:38 -0700290 @Override
291 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700292 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700293 checkNotNull(type, "Must have a type.");
294 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
295 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700296 op = Operation.ADD;
297 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700298
Ray Milkey810d6e72015-08-14 10:35:06 -0700299 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700300 }
301
302 @Override
303 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700304 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700305 checkNotNull(type, "Must have a type.");
306 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
307 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700308 op = Operation.REMOVE;
309 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700310
Ray Milkey810d6e72015-08-14 10:35:06 -0700311 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700312 }
313
alshabibfaa1e362015-04-02 15:01:54 -0700314 }
315
316}