blob: e5589b4b1b0b97c6d5896d71e6d278dd62b6bb90 [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;
alshabiba3a476d2015-04-10 14:35:38 -070021import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070022import org.onosproject.net.flow.criteria.Criterion;
23
24import java.util.Collection;
25import java.util.List;
26import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070027import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070028
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Default implementation of a filtering objective.
34 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070035@Beta
alshabibfaa1e362015-04-02 15:01:54 -070036public final class DefaultFilteringObjective implements FilteringObjective {
37
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;
alshabibfaa1e362015-04-02 15:01:54 -070049
alshabib910aff12015-04-09 16:55:57 -070050 private DefaultFilteringObjective(Type type, boolean permanent, int timeout,
alshabiba3a476d2015-04-10 14:35:38 -070051 ApplicationId appId, int priority, Criterion key,
alshabibfaa1e362015-04-02 15:01:54 -070052 List<Criterion> conditions, Operation op) {
alshabiba3a476d2015-04-10 14:35:38 -070053 this.key = key;
alshabib910aff12015-04-09 16:55:57 -070054 this.type = type;
alshabibfaa1e362015-04-02 15:01:54 -070055 this.permanent = permanent;
56 this.timeout = timeout;
57 this.appId = appId;
58 this.priority = priority;
59 this.conditions = conditions;
60 this.op = op;
alshabib2a441c62015-04-13 18:39:38 -070061 this.context = Optional.empty();
62
63 this.id = Objects.hash(type, key, conditions, permanent,
64 timeout, appId, priority);
65 }
66
67 public DefaultFilteringObjective(Type type, boolean permanent, int timeout,
68 ApplicationId appId, int priority, Criterion key,
69 List<Criterion> conditions,
70 ObjectiveContext context, Operation op) {
71 this.key = key;
72 this.type = type;
73 this.permanent = permanent;
74 this.timeout = timeout;
75 this.appId = appId;
76 this.priority = priority;
77 this.conditions = conditions;
78 this.op = op;
79 this.context = Optional.ofNullable(context);
alshabibfaa1e362015-04-02 15:01:54 -070080
alshabiba3a476d2015-04-10 14:35:38 -070081 this.id = Objects.hash(type, key, conditions, permanent,
alshabibfaa1e362015-04-02 15:01:54 -070082 timeout, appId, priority);
83 }
84
alshabibfaa1e362015-04-02 15:01:54 -070085 @Override
alshabiba3a476d2015-04-10 14:35:38 -070086 public Criterion key() {
87 return key;
88 }
89
90 @Override
alshabib910aff12015-04-09 16:55:57 -070091 public Type type() {
92 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070093 }
94
95 @Override
96 public Collection<Criterion> conditions() {
97 return conditions;
98 }
99
100 @Override
101 public int id() {
102 return id;
103 }
104
105 @Override
106 public int priority() {
107 return priority;
108 }
109
110 @Override
111 public ApplicationId appId() {
112 return appId;
113 }
114
115 @Override
116 public int timeout() {
117 return timeout;
118 }
119
120 @Override
121 public boolean permanent() {
122 return permanent;
123 }
124
125 @Override
126 public Operation op() {
127 return op;
128 }
129
alshabib2a441c62015-04-13 18:39:38 -0700130 @Override
131 public Optional<ObjectiveContext> context() {
132 return context;
133 }
134
alshabibfaa1e362015-04-02 15:01:54 -0700135 /**
136 * Returns a new builder.
137 *
138 * @return new builder
139 */
140 public static Builder builder() {
141 return new Builder();
142 }
143
144
145 public static final class Builder implements FilteringObjective.Builder {
146 private final ImmutableList.Builder<Criterion> listBuilder
147 = ImmutableList.builder();
148
alshabib910aff12015-04-09 16:55:57 -0700149 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700150 private boolean permanent = DEFAULT_PERMANENT;
151 private int timeout = DEFAULT_TIMEOUT;
152 private ApplicationId appId;
153 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700154 private Criterion key = Criteria.dummy();
155
156 @Override
157 public Builder withKey(Criterion key) {
158 this.key = key;
159 return this;
160 }
alshabibfaa1e362015-04-02 15:01:54 -0700161
162 @Override
163 public Builder addCondition(Criterion criterion) {
164 listBuilder.add(criterion);
165 return this;
166 }
167
168 @Override
alshabib910aff12015-04-09 16:55:57 -0700169 public Builder permit() {
170 this.type = Type.PERMIT;
171 return this;
172 }
173
174 @Override
175 public Builder deny() {
176 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700177 return this;
178 }
179
180 @Override
181 public Builder makeTemporary(int timeout) {
182 this.timeout = timeout;
183 permanent = false;
184 return this;
185 }
186
187 @Override
188 public Builder makePermanent() {
189 permanent = true;
190 return this;
191 }
192
193 @Override
194 public Builder fromApp(ApplicationId appId) {
195 this.appId = appId;
196 return this;
197 }
198
199 @Override
200 public Builder withPriority(int priority) {
201 this.priority = priority;
202 return this;
203 }
204
205 @Override
206 public FilteringObjective add() {
207 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700208 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700209 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
210 checkNotNull(appId, "Must supply an application id");
211
alshabib910aff12015-04-09 16:55:57 -0700212 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700213 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700214 Operation.ADD);
215
216 }
217
218 @Override
219 public FilteringObjective remove() {
220 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700221 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700222 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
223 checkNotNull(appId, "Must supply an application id");
224
alshabiba3a476d2015-04-10 14:35:38 -0700225
alshabib910aff12015-04-09 16:55:57 -0700226 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700227 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700228 Operation.REMOVE);
229
230 }
231
alshabib2a441c62015-04-13 18:39:38 -0700232 @Override
233 public FilteringObjective add(ObjectiveContext context) {
234 List<Criterion> conditions = listBuilder.build();
235 checkNotNull(type, "Must have a type.");
236 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
237 checkNotNull(appId, "Must supply an application id");
238
239 return new DefaultFilteringObjective(type, permanent, timeout,
240 appId, priority, key, conditions,
241 context, Operation.ADD);
242 }
243
244 @Override
245 public FilteringObjective remove(ObjectiveContext context) {
246 List<Criterion> conditions = listBuilder.build();
247 checkNotNull(type, "Must have a type.");
248 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
249 checkNotNull(appId, "Must supply an application id");
250
251
252 return new DefaultFilteringObjective(type, permanent, timeout,
253 appId, priority, key, conditions,
254 context, Operation.REMOVE);
255 }
256
alshabibfaa1e362015-04-02 15:01:54 -0700257
258 }
259
260}