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.TrafficTreatment;
22 import org.onosproject.net.flow.criteria.Criteria;
23 import org.onosproject.net.flow.criteria.Criterion;
25 import java.util.Collection;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
30 import static com.google.common.base.Preconditions.checkArgument;
31 import static com.google.common.base.Preconditions.checkNotNull;
34 * Default implementation of a filtering objective.
37 public final class DefaultFilteringObjective implements FilteringObjective {
40 private final Type type;
41 private final boolean permanent;
42 private final int timeout;
43 private final ApplicationId appId;
44 private final int priority;
45 private final Criterion key;
46 private final List<Criterion> conditions;
48 private final Operation op;
49 private final Optional<ObjectiveContext> context;
50 private final TrafficTreatment meta;
52 private DefaultFilteringObjective(Builder builder) {
53 this.key = builder.key;
54 this.type = builder.type;
55 this.permanent = builder.permanent;
56 this.timeout = builder.timeout;
57 this.appId = builder.appId;
58 this.priority = builder.priority;
59 this.conditions = builder.conditions;
61 this.context = Optional.ofNullable(builder.context);
62 this.meta = builder.meta;
64 this.id = Objects.hash(type, key, conditions, permanent,
65 timeout, appId, priority);
69 public Criterion key() {
79 public Collection<Criterion> conditions() {
89 public TrafficTreatment meta() {
95 public int priority() {
100 public ApplicationId appId() {
105 public int timeout() {
110 public boolean permanent() {
115 public Operation op() {
120 public Optional<ObjectiveContext> context() {
125 * Returns a new builder.
127 * @return new builder
129 public static Builder builder() {
130 return new Builder();
134 public static final class Builder implements FilteringObjective.Builder {
135 private final ImmutableList.Builder<Criterion> listBuilder
136 = ImmutableList.builder();
139 private boolean permanent = DEFAULT_PERMANENT;
140 private int timeout = DEFAULT_TIMEOUT;
141 private ApplicationId appId;
142 private int priority = DEFAULT_PRIORITY;
143 private Criterion key = Criteria.dummy();
144 private List<Criterion> conditions;
145 private Operation op;
146 private ObjectiveContext context;
147 private TrafficTreatment meta;
150 public Builder withKey(Criterion key) {
156 public Builder addCondition(Criterion criterion) {
157 listBuilder.add(criterion);
162 public Builder permit() {
163 this.type = Type.PERMIT;
168 public Builder deny() {
169 this.type = Type.DENY;
174 public Builder makeTemporary(int timeout) {
175 this.timeout = timeout;
181 public Builder makePermanent() {
187 public Builder fromApp(ApplicationId appId) {
193 public Builder withPriority(int priority) {
194 this.priority = priority;
199 public Builder setMeta(TrafficTreatment treatment) {
200 this.meta = treatment;
205 public FilteringObjective add() {
206 conditions = listBuilder.build();
208 checkNotNull(type, "Must have a type.");
209 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
210 checkNotNull(appId, "Must supply an application id");
212 return new DefaultFilteringObjective(this);
217 public FilteringObjective remove() {
218 conditions = listBuilder.build();
219 checkNotNull(type, "Must have a type.");
220 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
221 checkNotNull(appId, "Must supply an application id");
222 op = Operation.REMOVE;
224 return new DefaultFilteringObjective(this);
229 public FilteringObjective add(ObjectiveContext context) {
230 conditions = listBuilder.build();
231 checkNotNull(type, "Must have a type.");
232 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
233 checkNotNull(appId, "Must supply an application id");
235 this.context = context;
237 return new DefaultFilteringObjective(this);
241 public FilteringObjective remove(ObjectiveContext context) {
242 conditions = listBuilder.build();
243 checkNotNull(type, "Must have a type.");
244 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
245 checkNotNull(appId, "Must supply an application id");
246 op = Operation.REMOVE;
247 this.context = context;
249 return new DefaultFilteringObjective(this);