2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.net.flowobjective;
18 import com.google.common.annotations.Beta;
19 import com.google.common.collect.ImmutableList;
20 import org.onosproject.core.ApplicationId;
21 import org.onosproject.net.flow.criteria.Criteria;
22 import org.onosproject.net.flow.criteria.Criterion;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.Objects;
27 import java.util.Optional;
29 import static com.google.common.base.Preconditions.checkArgument;
30 import static com.google.common.base.Preconditions.checkNotNull;
33 * Default implementation of a filtering objective.
36 public final class DefaultFilteringObjective implements FilteringObjective {
39 private final Type type;
40 private final boolean permanent;
41 private final int timeout;
42 private final ApplicationId appId;
43 private final int priority;
44 private final Criterion key;
45 private final List<Criterion> conditions;
47 private final Operation op;
48 private final Optional<ObjectiveContext> context;
50 private DefaultFilteringObjective(Builder builder) {
51 this.key = builder.key;
52 this.type = builder.type;
53 this.permanent = builder.permanent;
54 this.timeout = builder.timeout;
55 this.appId = builder.appId;
56 this.priority = builder.priority;
57 this.conditions = builder.conditions;
59 this.context = Optional.ofNullable(builder.context);
61 this.id = Objects.hash(type, key, conditions, permanent,
62 timeout, appId, priority);
66 public Criterion key() {
76 public Collection<Criterion> conditions() {
86 public int priority() {
91 public ApplicationId appId() {
96 public int timeout() {
101 public boolean permanent() {
106 public Operation op() {
111 public Optional<ObjectiveContext> context() {
116 * Returns a new builder.
118 * @return new builder
120 public static Builder builder() {
121 return new Builder();
125 public static final class Builder implements FilteringObjective.Builder {
126 private final ImmutableList.Builder<Criterion> listBuilder
127 = ImmutableList.builder();
130 private boolean permanent = DEFAULT_PERMANENT;
131 private int timeout = DEFAULT_TIMEOUT;
132 private ApplicationId appId;
133 private int priority = DEFAULT_PRIORITY;
134 private Criterion key = Criteria.dummy();
135 private List<Criterion> conditions;
136 private Operation op;
137 private ObjectiveContext context;
140 public Builder withKey(Criterion key) {
146 public Builder addCondition(Criterion criterion) {
147 listBuilder.add(criterion);
152 public Builder permit() {
153 this.type = Type.PERMIT;
158 public Builder deny() {
159 this.type = Type.DENY;
164 public Builder makeTemporary(int timeout) {
165 this.timeout = timeout;
171 public Builder makePermanent() {
177 public Builder fromApp(ApplicationId appId) {
183 public Builder withPriority(int priority) {
184 this.priority = priority;
189 public FilteringObjective add() {
190 conditions = listBuilder.build();
192 checkNotNull(type, "Must have a type.");
193 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
194 checkNotNull(appId, "Must supply an application id");
196 return new DefaultFilteringObjective(this);
201 public FilteringObjective remove() {
202 conditions = listBuilder.build();
203 checkNotNull(type, "Must have a type.");
204 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
205 checkNotNull(appId, "Must supply an application id");
206 op = Operation.REMOVE;
208 return new DefaultFilteringObjective(this);
213 public FilteringObjective add(ObjectiveContext context) {
214 conditions = listBuilder.build();
215 checkNotNull(type, "Must have a type.");
216 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
217 checkNotNull(appId, "Must supply an application id");
219 this.context = context;
221 return new DefaultFilteringObjective(this);
225 public FilteringObjective remove(ObjectiveContext context) {
226 conditions = listBuilder.build();
227 checkNotNull(type, "Must have a type.");
228 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
229 checkNotNull(appId, "Must supply an application id");
230 op = Operation.REMOVE;
231 this.context = context;
233 return new DefaultFilteringObjective(this);