blob: 593cde67ff92dbf44b9f46a7d49577740e78d085 [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;
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
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Default implementation of a filtering objective.
35 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070036@Beta
alshabibfaa1e362015-04-02 15:01:54 -070037public final class DefaultFilteringObjective implements FilteringObjective {
38
alshabib910aff12015-04-09 16:55:57 -070039 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070040 private final boolean permanent;
41 private final int timeout;
42 private final ApplicationId appId;
43 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070044 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070045 private final List<Criterion> conditions;
46 private final int id;
47 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070048 private final Optional<ObjectiveContext> context;
Saurav Das88f4c842015-10-27 13:13:19 -070049 private final TrafficTreatment meta;
alshabibfaa1e362015-04-02 15:01:54 -070050
Ray Milkey810d6e72015-08-14 10:35:06 -070051 private DefaultFilteringObjective(Builder builder) {
52 this.key = builder.key;
53 this.type = builder.type;
54 this.permanent = builder.permanent;
55 this.timeout = builder.timeout;
56 this.appId = builder.appId;
57 this.priority = builder.priority;
58 this.conditions = builder.conditions;
59 this.op = builder.op;
60 this.context = Optional.ofNullable(builder.context);
Saurav Das88f4c842015-10-27 13:13:19 -070061 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070062
63 this.id = Objects.hash(type, key, conditions, permanent,
Thomas Vachuska00f48162016-02-29 17:07:23 -080064 timeout, appId, priority);
alshabibfaa1e362015-04-02 15:01:54 -070065 }
66
alshabibfaa1e362015-04-02 15:01:54 -070067 @Override
alshabiba3a476d2015-04-10 14:35:38 -070068 public Criterion key() {
69 return key;
70 }
71
72 @Override
alshabib910aff12015-04-09 16:55:57 -070073 public Type type() {
74 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070075 }
76
77 @Override
78 public Collection<Criterion> conditions() {
79 return conditions;
80 }
81
82 @Override
83 public int id() {
84 return id;
85 }
86
87 @Override
Saurav Das88f4c842015-10-27 13:13:19 -070088 public TrafficTreatment meta() {
89 return meta;
90 }
91
92
93 @Override
alshabibfaa1e362015-04-02 15:01:54 -070094 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
Thomas Vachuska00f48162016-02-29 17:07:23 -0800123
124 @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
alshabibfaa1e362015-04-02 15:01:54 -0700150 /**
151 * Returns a new builder.
152 *
153 * @return new builder
154 */
155 public static Builder builder() {
156 return new Builder();
157 }
158
Thomas Vachuska00f48162016-02-29 17:07:23 -0800159 @Override
160 public Builder copy() {
161 return new Builder(this);
162 }
alshabibfaa1e362015-04-02 15:01:54 -0700163
164 public static final class Builder implements FilteringObjective.Builder {
165 private final ImmutableList.Builder<Criterion> listBuilder
166 = ImmutableList.builder();
167
alshabib910aff12015-04-09 16:55:57 -0700168 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700169 private boolean permanent = DEFAULT_PERMANENT;
170 private int timeout = DEFAULT_TIMEOUT;
171 private ApplicationId appId;
172 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700173 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700174 private List<Criterion> conditions;
175 private Operation op;
176 private ObjectiveContext context;
Saurav Das88f4c842015-10-27 13:13:19 -0700177 private TrafficTreatment meta;
alshabiba3a476d2015-04-10 14:35:38 -0700178
Thomas Vachuska00f48162016-02-29 17:07:23 -0800179 // Creates an empty builder
180 private Builder() {
181 }
182
183 // Creates a builder set to create a copy of the specified objective.
184 private Builder(FilteringObjective objective) {
185 this.type = objective.type();
186 this.key = objective.key();
187 this.conditions = ImmutableList.copyOf(objective.conditions());
188 this.permanent = objective.permanent();
189 this.timeout = objective.timeout();
190 this.priority = objective.priority();
191 this.appId = objective.appId();
192 this.meta = objective.meta();
193 this.op = objective.op();
194 }
195
alshabiba3a476d2015-04-10 14:35:38 -0700196 @Override
197 public Builder withKey(Criterion key) {
198 this.key = key;
199 return this;
200 }
alshabibfaa1e362015-04-02 15:01:54 -0700201
202 @Override
203 public Builder addCondition(Criterion criterion) {
204 listBuilder.add(criterion);
205 return this;
206 }
207
208 @Override
alshabib910aff12015-04-09 16:55:57 -0700209 public Builder permit() {
210 this.type = Type.PERMIT;
211 return this;
212 }
213
214 @Override
215 public Builder deny() {
216 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700217 return this;
218 }
219
220 @Override
221 public Builder makeTemporary(int timeout) {
222 this.timeout = timeout;
223 permanent = false;
224 return this;
225 }
226
227 @Override
228 public Builder makePermanent() {
229 permanent = true;
230 return this;
231 }
232
233 @Override
234 public Builder fromApp(ApplicationId appId) {
235 this.appId = appId;
236 return this;
237 }
238
239 @Override
240 public Builder withPriority(int priority) {
241 this.priority = priority;
242 return this;
243 }
244
245 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800246 public Builder withMeta(TrafficTreatment treatment) {
Saurav Das88f4c842015-10-27 13:13:19 -0700247 this.meta = treatment;
248 return this;
249 }
250
251 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700252 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700253 conditions = listBuilder.build();
254 op = Operation.ADD;
alshabib910aff12015-04-09 16:55:57 -0700255 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700256 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
257 checkNotNull(appId, "Must supply an application id");
258
Ray Milkey810d6e72015-08-14 10:35:06 -0700259 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700260 }
261
262 @Override
263 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700264 conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700265 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700266 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
267 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700268 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700269
Ray Milkey810d6e72015-08-14 10:35:06 -0700270 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700271 }
272
alshabib2a441c62015-04-13 18:39:38 -0700273 @Override
274 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700275 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700276 checkNotNull(type, "Must have a type.");
277 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
278 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700279 op = Operation.ADD;
280 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700281
Ray Milkey810d6e72015-08-14 10:35:06 -0700282 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700283 }
284
285 @Override
286 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700287 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700288 checkNotNull(type, "Must have a type.");
289 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
290 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700291 op = Operation.REMOVE;
292 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700293
Ray Milkey810d6e72015-08-14 10:35:06 -0700294 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700295 }
296
alshabibfaa1e362015-04-02 15:01:54 -0700297 }
298
299}