blob: da92b80d19e7d88fff933d6b5cc88e02702a7872 [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;
20import org.onosproject.net.flow.criteria.Criterion;
21
22import java.util.Collection;
23import java.util.List;
24import java.util.Objects;
25
26import static com.google.common.base.Preconditions.checkArgument;
27import static com.google.common.base.Preconditions.checkNotNull;
28
29/**
30 * Default implementation of a filtering objective.
31 */
32public final class DefaultFilteringObjective implements FilteringObjective {
33
34
alshabib910aff12015-04-09 16:55:57 -070035 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070036 private final boolean permanent;
37 private final int timeout;
38 private final ApplicationId appId;
39 private final int priority;
40 private final List<Criterion> conditions;
41 private final int id;
42 private final Operation op;
43
alshabib910aff12015-04-09 16:55:57 -070044 private DefaultFilteringObjective(Type type, boolean permanent, int timeout,
alshabibfaa1e362015-04-02 15:01:54 -070045 ApplicationId appId, int priority,
46 List<Criterion> conditions, Operation op) {
alshabib910aff12015-04-09 16:55:57 -070047 this.type = type;
alshabibfaa1e362015-04-02 15:01:54 -070048 this.permanent = permanent;
49 this.timeout = timeout;
50 this.appId = appId;
51 this.priority = priority;
52 this.conditions = conditions;
53 this.op = op;
54
alshabib910aff12015-04-09 16:55:57 -070055 this.id = Objects.hash(type, conditions, permanent,
alshabibfaa1e362015-04-02 15:01:54 -070056 timeout, appId, priority);
57 }
58
alshabibfaa1e362015-04-02 15:01:54 -070059 @Override
alshabib910aff12015-04-09 16:55:57 -070060 public Type type() {
61 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070062 }
63
64 @Override
65 public Collection<Criterion> conditions() {
66 return conditions;
67 }
68
69 @Override
70 public int id() {
71 return id;
72 }
73
74 @Override
75 public int priority() {
76 return priority;
77 }
78
79 @Override
80 public ApplicationId appId() {
81 return appId;
82 }
83
84 @Override
85 public int timeout() {
86 return timeout;
87 }
88
89 @Override
90 public boolean permanent() {
91 return permanent;
92 }
93
94 @Override
95 public Operation op() {
96 return op;
97 }
98
99 /**
100 * Returns a new builder.
101 *
102 * @return new builder
103 */
104 public static Builder builder() {
105 return new Builder();
106 }
107
108
109 public static final class Builder implements FilteringObjective.Builder {
110 private final ImmutableList.Builder<Criterion> listBuilder
111 = ImmutableList.builder();
112
alshabib910aff12015-04-09 16:55:57 -0700113 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700114 private boolean permanent = DEFAULT_PERMANENT;
115 private int timeout = DEFAULT_TIMEOUT;
116 private ApplicationId appId;
117 private int priority = DEFAULT_PRIORITY;
118
119 @Override
120 public Builder addCondition(Criterion criterion) {
121 listBuilder.add(criterion);
122 return this;
123 }
124
125 @Override
alshabib910aff12015-04-09 16:55:57 -0700126 public Builder permit() {
127 this.type = Type.PERMIT;
128 return this;
129 }
130
131 @Override
132 public Builder deny() {
133 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700134 return this;
135 }
136
137 @Override
138 public Builder makeTemporary(int timeout) {
139 this.timeout = timeout;
140 permanent = false;
141 return this;
142 }
143
144 @Override
145 public Builder makePermanent() {
146 permanent = true;
147 return this;
148 }
149
150 @Override
151 public Builder fromApp(ApplicationId appId) {
152 this.appId = appId;
153 return this;
154 }
155
156 @Override
157 public Builder withPriority(int priority) {
158 this.priority = priority;
159 return this;
160 }
161
162 @Override
163 public FilteringObjective add() {
164 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700165 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700166 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
167 checkNotNull(appId, "Must supply an application id");
168
alshabib910aff12015-04-09 16:55:57 -0700169 return new DefaultFilteringObjective(type, permanent, timeout,
alshabibfaa1e362015-04-02 15:01:54 -0700170 appId, priority, conditions,
171 Operation.ADD);
172
173 }
174
175 @Override
176 public FilteringObjective remove() {
177 List<Criterion> conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700178 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700179 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
180 checkNotNull(appId, "Must supply an application id");
181
alshabib910aff12015-04-09 16:55:57 -0700182 return new DefaultFilteringObjective(type, permanent, timeout,
alshabibfaa1e362015-04-02 15:01:54 -0700183 appId, priority, conditions,
184 Operation.REMOVE);
185
186 }
187
188
189 }
190
191}