blob: 4cde9895870d15294e28fab23bde31d79f44c4d5 [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())
Charles Chan3e041292016-02-27 15:58:43 -0800163 .toString();
164 }
165
alshabibfaa1e362015-04-02 15:01:54 -0700166 /**
167 * Returns a new builder.
168 *
169 * @return new builder
170 */
171 public static Builder builder() {
172 return new Builder();
173 }
174
Thomas Vachuska00f48162016-02-29 17:07:23 -0800175 @Override
176 public Builder copy() {
177 return new Builder(this);
178 }
alshabibfaa1e362015-04-02 15:01:54 -0700179
180 public static final class Builder implements FilteringObjective.Builder {
181 private final ImmutableList.Builder<Criterion> listBuilder
182 = ImmutableList.builder();
183
alshabib910aff12015-04-09 16:55:57 -0700184 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700185 private boolean permanent = DEFAULT_PERMANENT;
186 private int timeout = DEFAULT_TIMEOUT;
187 private ApplicationId appId;
188 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700189 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700190 private List<Criterion> conditions;
191 private Operation op;
192 private ObjectiveContext context;
Saurav Das88f4c842015-10-27 13:13:19 -0700193 private TrafficTreatment meta;
alshabiba3a476d2015-04-10 14:35:38 -0700194
Thomas Vachuska00f48162016-02-29 17:07:23 -0800195 // Creates an empty builder
196 private Builder() {
197 }
198
199 // Creates a builder set to create a copy of the specified objective.
200 private Builder(FilteringObjective objective) {
201 this.type = objective.type();
202 this.key = objective.key();
Konstantinos Kanonakisd7690cf2016-05-26 11:38:33 -0500203 objective.conditions().forEach(this::addCondition);
Thomas Vachuska00f48162016-02-29 17:07:23 -0800204 this.permanent = objective.permanent();
205 this.timeout = objective.timeout();
206 this.priority = objective.priority();
207 this.appId = objective.appId();
208 this.meta = objective.meta();
209 this.op = objective.op();
210 }
211
alshabiba3a476d2015-04-10 14:35:38 -0700212 @Override
213 public Builder withKey(Criterion key) {
214 this.key = key;
215 return this;
216 }
alshabibfaa1e362015-04-02 15:01:54 -0700217
218 @Override
219 public Builder addCondition(Criterion criterion) {
220 listBuilder.add(criterion);
221 return this;
222 }
223
224 @Override
alshabib910aff12015-04-09 16:55:57 -0700225 public Builder permit() {
226 this.type = Type.PERMIT;
227 return this;
228 }
229
230 @Override
231 public Builder deny() {
232 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700233 return this;
234 }
235
236 @Override
237 public Builder makeTemporary(int timeout) {
238 this.timeout = timeout;
239 permanent = false;
240 return this;
241 }
242
243 @Override
244 public Builder makePermanent() {
245 permanent = true;
246 return this;
247 }
248
249 @Override
250 public Builder fromApp(ApplicationId appId) {
251 this.appId = appId;
252 return this;
253 }
254
255 @Override
256 public Builder withPriority(int priority) {
257 this.priority = priority;
258 return this;
259 }
260
261 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800262 public Builder withMeta(TrafficTreatment treatment) {
Saurav Das88f4c842015-10-27 13:13:19 -0700263 this.meta = treatment;
264 return this;
265 }
266
267 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700268 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700269 conditions = listBuilder.build();
270 op = Operation.ADD;
alshabib910aff12015-04-09 16:55:57 -0700271 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700272 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
273 checkNotNull(appId, "Must supply an application id");
Jayasree Ghosh3684dc72016-06-09 18:07:19 +0530274 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " +
275 "out of range");
alshabibfaa1e362015-04-02 15:01:54 -0700276
Ray Milkey810d6e72015-08-14 10:35:06 -0700277 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700278 }
279
280 @Override
281 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700282 conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700283 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700284 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
285 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700286 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700287
Ray Milkey810d6e72015-08-14 10:35:06 -0700288 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700289 }
290
alshabib2a441c62015-04-13 18:39:38 -0700291 @Override
292 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700293 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700294 checkNotNull(type, "Must have a type.");
295 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
296 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700297 op = Operation.ADD;
298 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700299
Ray Milkey810d6e72015-08-14 10:35:06 -0700300 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700301 }
302
303 @Override
304 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700305 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700306 checkNotNull(type, "Must have a type.");
307 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
308 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700309 op = Operation.REMOVE;
310 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700311
Ray Milkey810d6e72015-08-14 10:35:06 -0700312 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700313 }
314
alshabibfaa1e362015-04-02 15:01:54 -0700315 }
316
317}