blob: 94519a96ccf80d43517b10918911e926bd08c3c1 [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
18import com.google.common.collect.ImmutableList;
19import org.onosproject.core.ApplicationId;
alshabiba3a476d2015-04-10 14:35:38 -070020import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070021import org.onosproject.net.flow.criteria.Criterion;
22
23import java.util.Collection;
24import java.util.List;
25import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070026import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070027
28import static com.google.common.base.Preconditions.checkArgument;
29import static com.google.common.base.Preconditions.checkNotNull;
30
31/**
32 * Default implementation of a filtering objective.
33 */
34public final class DefaultFilteringObjective implements FilteringObjective {
35
36
alshabib910aff12015-04-09 16:55:57 -070037 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070038 private final boolean permanent;
39 private final int timeout;
40 private final ApplicationId appId;
41 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070042 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070043 private final List<Criterion> conditions;
44 private final int id;
45 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070046 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070047
alshabib910aff12015-04-09 16:55:57 -070048 private DefaultFilteringObjective(Type type, boolean permanent, int timeout,
alshabiba3a476d2015-04-10 14:35:38 -070049 ApplicationId appId, int priority, Criterion key,
alshabibfaa1e362015-04-02 15:01:54 -070050 List<Criterion> conditions, Operation op) {
alshabiba3a476d2015-04-10 14:35:38 -070051 this.key = key;
alshabib910aff12015-04-09 16:55:57 -070052 this.type = type;
alshabibfaa1e362015-04-02 15:01:54 -070053 this.permanent = permanent;
54 this.timeout = timeout;
55 this.appId = appId;
56 this.priority = priority;
57 this.conditions = conditions;
58 this.op = op;
alshabib2a441c62015-04-13 18:39:38 -070059 this.context = Optional.empty();
60
61 this.id = Objects.hash(type, key, conditions, permanent,
62 timeout, appId, priority);
63 }
64
65 public DefaultFilteringObjective(Type type, boolean permanent, int timeout,
66 ApplicationId appId, int priority, Criterion key,
67 List<Criterion> conditions,
68 ObjectiveContext context, Operation op) {
69 this.key = key;
70 this.type = type;
71 this.permanent = permanent;
72 this.timeout = timeout;
73 this.appId = appId;
74 this.priority = priority;
75 this.conditions = conditions;
76 this.op = op;
77 this.context = Optional.ofNullable(context);
alshabibfaa1e362015-04-02 15:01:54 -070078
alshabiba3a476d2015-04-10 14:35:38 -070079 this.id = Objects.hash(type, key, conditions, permanent,
alshabibfaa1e362015-04-02 15:01:54 -070080 timeout, appId, priority);
81 }
82
alshabibfaa1e362015-04-02 15:01:54 -070083 @Override
alshabiba3a476d2015-04-10 14:35:38 -070084 public Criterion key() {
85 return key;
86 }
87
88 @Override
alshabib910aff12015-04-09 16:55:57 -070089 public Type type() {
90 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070091 }
92
93 @Override
94 public Collection<Criterion> conditions() {
95 return conditions;
96 }
97
98 @Override
99 public int id() {
100 return id;
101 }
102
103 @Override
104 public int priority() {
105 return priority;
106 }
107
108 @Override
109 public ApplicationId appId() {
110 return appId;
111 }
112
113 @Override
114 public int timeout() {
115 return timeout;
116 }
117
118 @Override
119 public boolean permanent() {
120 return permanent;
121 }
122
123 @Override
124 public Operation op() {
125 return op;
126 }
127
alshabib2a441c62015-04-13 18:39:38 -0700128 @Override
129 public Optional<ObjectiveContext> context() {
130 return context;
131 }
132
alshabibfaa1e362015-04-02 15:01:54 -0700133 /**
134 * Returns a new builder.
135 *
136 * @return new builder
137 */
138 public static Builder builder() {
139 return new Builder();
140 }
141
142
143 public static final class Builder implements FilteringObjective.Builder {
144 private final ImmutableList.Builder<Criterion> listBuilder
145 = ImmutableList.builder();
146
alshabib910aff12015-04-09 16:55:57 -0700147 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700148 private boolean permanent = DEFAULT_PERMANENT;
149 private int timeout = DEFAULT_TIMEOUT;
150 private ApplicationId appId;
151 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700152 private Criterion key = Criteria.dummy();
153
154 @Override
155 public Builder withKey(Criterion key) {
156 this.key = key;
157 return this;
158 }
alshabibfaa1e362015-04-02 15:01:54 -0700159
160 @Override
161 public Builder addCondition(Criterion criterion) {
162 listBuilder.add(criterion);
163 return this;
164 }
165
166 @Override
alshabib910aff12015-04-09 16:55:57 -0700167 public Builder permit() {
168 this.type = Type.PERMIT;
169 return this;
170 }
171
172 @Override
173 public Builder deny() {
174 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700175 return this;
176 }
177
178 @Override
179 public Builder makeTemporary(int timeout) {
180 this.timeout = timeout;
181 permanent = false;
182 return this;
183 }
184
185 @Override
186 public Builder makePermanent() {
187 permanent = true;
188 return this;
189 }
190
191 @Override
192 public Builder fromApp(ApplicationId appId) {
193 this.appId = appId;
194 return this;
195 }
196
197 @Override
198 public Builder withPriority(int priority) {
199 this.priority = priority;
200 return this;
201 }
202
203 @Override
204 public FilteringObjective add() {
205 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700206 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700207 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
208 checkNotNull(appId, "Must supply an application id");
209
alshabib910aff12015-04-09 16:55:57 -0700210 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700211 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700212 Operation.ADD);
213
214 }
215
216 @Override
217 public FilteringObjective remove() {
218 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700219 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700220 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
221 checkNotNull(appId, "Must supply an application id");
222
alshabiba3a476d2015-04-10 14:35:38 -0700223
alshabib910aff12015-04-09 16:55:57 -0700224 return new DefaultFilteringObjective(type, permanent, timeout,
alshabiba3a476d2015-04-10 14:35:38 -0700225 appId, priority, key, conditions,
alshabibfaa1e362015-04-02 15:01:54 -0700226 Operation.REMOVE);
227
228 }
229
alshabib2a441c62015-04-13 18:39:38 -0700230 @Override
231 public FilteringObjective add(ObjectiveContext context) {
232 List<Criterion> conditions = listBuilder.build();
233 checkNotNull(type, "Must have a type.");
234 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
235 checkNotNull(appId, "Must supply an application id");
236
237 return new DefaultFilteringObjective(type, permanent, timeout,
238 appId, priority, key, conditions,
239 context, Operation.ADD);
240 }
241
242 @Override
243 public FilteringObjective remove(ObjectiveContext context) {
244 List<Criterion> conditions = listBuilder.build();
245 checkNotNull(type, "Must have a type.");
246 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
247 checkNotNull(appId, "Must supply an application id");
248
249
250 return new DefaultFilteringObjective(type, permanent, timeout,
251 appId, priority, key, conditions,
252 context, Operation.REMOVE);
253 }
254
alshabibfaa1e362015-04-02 15:01:54 -0700255
256 }
257
258}