blob: 37000afeda31e340ffbc125a0ac39ba89f491c50 [file] [log] [blame]
alshabibfaa1e362015-04-02 15:01:54 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
Andrea Campanellacbd16942021-06-01 16:33:14 +020021import org.onosproject.net.AbstractAnnotated;
22import org.onosproject.net.Annotations;
Saurav Das88f4c842015-10-27 13:13:19 -070023import org.onosproject.net.flow.TrafficTreatment;
alshabiba3a476d2015-04-10 14:35:38 -070024import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070025import org.onosproject.net.flow.criteria.Criterion;
26
27import java.util.Collection;
28import java.util.List;
29import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070030import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070031
Charles Chan3e041292016-02-27 15:58:43 -080032import static com.google.common.base.MoreObjects.toStringHelper;
alshabibfaa1e362015-04-02 15:01:54 -070033import static com.google.common.base.Preconditions.checkArgument;
34import static com.google.common.base.Preconditions.checkNotNull;
35
36/**
37 * Default implementation of a filtering objective.
38 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070039@Beta
Andrea Campanellacbd16942021-06-01 16:33:14 +020040public final class DefaultFilteringObjective extends AbstractAnnotated
41 implements FilteringObjective {
alshabibfaa1e362015-04-02 15:01:54 -070042
alshabib910aff12015-04-09 16:55:57 -070043 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070044 private final boolean permanent;
45 private final int timeout;
46 private final ApplicationId appId;
47 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070048 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070049 private final List<Criterion> conditions;
50 private final int id;
51 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070052 private final Optional<ObjectiveContext> context;
Saurav Das88f4c842015-10-27 13:13:19 -070053 private final TrafficTreatment meta;
alshabibfaa1e362015-04-02 15:01:54 -070054
Ray Milkey810d6e72015-08-14 10:35:06 -070055 private DefaultFilteringObjective(Builder builder) {
Andrea Campanellacbd16942021-06-01 16:33:14 +020056 super(builder.annotations);
Ray Milkey810d6e72015-08-14 10:35:06 -070057 this.key = builder.key;
58 this.type = builder.type;
59 this.permanent = builder.permanent;
60 this.timeout = builder.timeout;
61 this.appId = builder.appId;
62 this.priority = builder.priority;
63 this.conditions = builder.conditions;
64 this.op = builder.op;
65 this.context = Optional.ofNullable(builder.context);
Saurav Das88f4c842015-10-27 13:13:19 -070066 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070067
68 this.id = Objects.hash(type, key, conditions, permanent,
Thomas Vachuska00f48162016-02-29 17:07:23 -080069 timeout, appId, priority);
alshabibfaa1e362015-04-02 15:01:54 -070070 }
71
alshabibfaa1e362015-04-02 15:01:54 -070072 @Override
alshabiba3a476d2015-04-10 14:35:38 -070073 public Criterion key() {
74 return key;
75 }
76
77 @Override
alshabib910aff12015-04-09 16:55:57 -070078 public Type type() {
79 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070080 }
81
82 @Override
83 public Collection<Criterion> conditions() {
84 return conditions;
85 }
86
87 @Override
88 public int id() {
89 return id;
90 }
91
92 @Override
Saurav Das88f4c842015-10-27 13:13:19 -070093 public TrafficTreatment meta() {
94 return meta;
95 }
96
97
98 @Override
alshabibfaa1e362015-04-02 15:01:54 -070099 public int priority() {
100 return priority;
101 }
102
103 @Override
104 public ApplicationId appId() {
105 return appId;
106 }
107
108 @Override
109 public int timeout() {
110 return timeout;
111 }
112
113 @Override
114 public boolean permanent() {
115 return permanent;
116 }
117
118 @Override
119 public Operation op() {
120 return op;
121 }
122
alshabib2a441c62015-04-13 18:39:38 -0700123 @Override
124 public Optional<ObjectiveContext> context() {
125 return context;
126 }
127
Thomas Vachuska00f48162016-02-29 17:07:23 -0800128 @Override
129 public int hashCode() {
130 return Objects.hash(type, permanent, timeout, appId, priority, key,
131 conditions, op, meta);
132 }
133
134 @Override
135 public boolean equals(Object obj) {
136 if (this == obj) {
137 return true;
138 }
139 if (obj instanceof DefaultFilteringObjective) {
140 final DefaultFilteringObjective other = (DefaultFilteringObjective) obj;
141 return Objects.equals(this.type, other.type)
142 && Objects.equals(this.permanent, other.permanent)
143 && Objects.equals(this.timeout, other.timeout)
144 && Objects.equals(this.appId, other.appId)
145 && Objects.equals(this.priority, other.priority)
146 && Objects.equals(this.key, other.key)
147 && Objects.equals(this.conditions, other.conditions)
148 && Objects.equals(this.op, other.op)
149 && Objects.equals(this.meta, other.meta);
150 }
151 return false;
152 }
153
Charles Chan3e041292016-02-27 15:58:43 -0800154 @Override
155 public String toString() {
156 return toStringHelper(this)
157 .add("id", id())
158 .add("type", type())
159 .add("op", op())
160 .add("priority", priority())
161 .add("key", key())
162 .add("conditions", conditions())
163 .add("meta", meta())
164 .add("appId", appId())
165 .add("permanent", permanent())
166 .add("timeout", timeout())
Andrea Campanellacbd16942021-06-01 16:33:14 +0200167 .add("annotations", annotations())
Charles Chan3e041292016-02-27 15:58:43 -0800168 .toString();
169 }
170
alshabibfaa1e362015-04-02 15:01:54 -0700171 /**
172 * Returns a new builder.
173 *
174 * @return new builder
175 */
176 public static Builder builder() {
177 return new Builder();
178 }
179
Thomas Vachuska00f48162016-02-29 17:07:23 -0800180 @Override
181 public Builder copy() {
182 return new Builder(this);
183 }
alshabibfaa1e362015-04-02 15:01:54 -0700184
185 public static final class Builder implements FilteringObjective.Builder {
186 private final ImmutableList.Builder<Criterion> listBuilder
187 = ImmutableList.builder();
188
alshabib910aff12015-04-09 16:55:57 -0700189 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700190 private boolean permanent = DEFAULT_PERMANENT;
191 private int timeout = DEFAULT_TIMEOUT;
192 private ApplicationId appId;
193 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700194 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700195 private List<Criterion> conditions;
196 private Operation op;
197 private ObjectiveContext context;
Saurav Das88f4c842015-10-27 13:13:19 -0700198 private TrafficTreatment meta;
Andrea Campanellacbd16942021-06-01 16:33:14 +0200199 private Annotations annotations;
alshabiba3a476d2015-04-10 14:35:38 -0700200
Thomas Vachuska00f48162016-02-29 17:07:23 -0800201 // Creates an empty builder
202 private Builder() {
203 }
204
205 // Creates a builder set to create a copy of the specified objective.
206 private Builder(FilteringObjective objective) {
207 this.type = objective.type();
208 this.key = objective.key();
Konstantinos Kanonakisd7690cf2016-05-26 11:38:33 -0500209 objective.conditions().forEach(this::addCondition);
Thomas Vachuska00f48162016-02-29 17:07:23 -0800210 this.permanent = objective.permanent();
211 this.timeout = objective.timeout();
212 this.priority = objective.priority();
213 this.appId = objective.appId();
214 this.meta = objective.meta();
215 this.op = objective.op();
Andrea Campanellacbd16942021-06-01 16:33:14 +0200216 this.annotations = objective.annotations();
Thomas Vachuska00f48162016-02-29 17:07:23 -0800217 }
218
alshabiba3a476d2015-04-10 14:35:38 -0700219 @Override
220 public Builder withKey(Criterion key) {
221 this.key = key;
222 return this;
223 }
alshabibfaa1e362015-04-02 15:01:54 -0700224
225 @Override
226 public Builder addCondition(Criterion criterion) {
227 listBuilder.add(criterion);
228 return this;
229 }
230
231 @Override
alshabib910aff12015-04-09 16:55:57 -0700232 public Builder permit() {
233 this.type = Type.PERMIT;
234 return this;
235 }
236
237 @Override
238 public Builder deny() {
239 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700240 return this;
241 }
242
243 @Override
244 public Builder makeTemporary(int timeout) {
245 this.timeout = timeout;
246 permanent = false;
247 return this;
248 }
249
250 @Override
251 public Builder makePermanent() {
252 permanent = true;
253 return this;
254 }
255
256 @Override
257 public Builder fromApp(ApplicationId appId) {
258 this.appId = appId;
259 return this;
260 }
261
262 @Override
263 public Builder withPriority(int priority) {
264 this.priority = priority;
265 return this;
266 }
267
268 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800269 public Builder withMeta(TrafficTreatment treatment) {
Saurav Das88f4c842015-10-27 13:13:19 -0700270 this.meta = treatment;
271 return this;
272 }
273
274 @Override
Andrea Campanellacbd16942021-06-01 16:33:14 +0200275 public Builder withAnnotations(Annotations annotations) {
276 this.annotations = annotations;
277 return this;
278 }
279
280 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700281 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700282 conditions = listBuilder.build();
283 op = Operation.ADD;
alshabib910aff12015-04-09 16:55:57 -0700284 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700285 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
286 checkNotNull(appId, "Must supply an application id");
Jayasree Ghosh3684dc72016-06-09 18:07:19 +0530287 checkArgument(priority <= MAX_PRIORITY && priority >= MIN_PRIORITY, "Priority " +
288 "out of range");
alshabibfaa1e362015-04-02 15:01:54 -0700289
Ray Milkey810d6e72015-08-14 10:35:06 -0700290 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700291 }
292
293 @Override
294 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700295 conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700296 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700297 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
298 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700299 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700300
Ray Milkey810d6e72015-08-14 10:35:06 -0700301 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700302 }
303
alshabib2a441c62015-04-13 18:39:38 -0700304 @Override
305 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700306 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700307 checkNotNull(type, "Must have a type.");
308 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
309 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700310 op = Operation.ADD;
311 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700312
Ray Milkey810d6e72015-08-14 10:35:06 -0700313 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700314 }
315
316 @Override
317 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700318 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700319 checkNotNull(type, "Must have a type.");
320 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
321 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700322 op = Operation.REMOVE;
323 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700324
Ray Milkey810d6e72015-08-14 10:35:06 -0700325 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700326 }
327
alshabibfaa1e362015-04-02 15:01:54 -0700328 }
329
330}