blob: 4d9d7225f4745f729161283f725a9a95ea249665 [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;
Saurav Das88f4c842015-10-27 13:13:19 -070021import org.onosproject.net.flow.TrafficTreatment;
alshabiba3a476d2015-04-10 14:35:38 -070022import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070023import org.onosproject.net.flow.criteria.Criterion;
24
25import java.util.Collection;
26import java.util.List;
27import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070028import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070029
30import static com.google.common.base.Preconditions.checkArgument;
31import static com.google.common.base.Preconditions.checkNotNull;
32
33/**
34 * Default implementation of a filtering objective.
35 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070036@Beta
alshabibfaa1e362015-04-02 15:01:54 -070037public final class DefaultFilteringObjective implements FilteringObjective {
38
39
alshabib910aff12015-04-09 16:55:57 -070040 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070041 private final boolean permanent;
42 private final int timeout;
43 private final ApplicationId appId;
44 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070045 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070046 private final List<Criterion> conditions;
47 private final int id;
48 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070049 private final Optional<ObjectiveContext> context;
Saurav Das88f4c842015-10-27 13:13:19 -070050 private final TrafficTreatment meta;
alshabibfaa1e362015-04-02 15:01:54 -070051
Ray Milkey810d6e72015-08-14 10:35:06 -070052 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;
60 this.op = builder.op;
61 this.context = Optional.ofNullable(builder.context);
Saurav Das88f4c842015-10-27 13:13:19 -070062 this.meta = builder.meta;
alshabib2a441c62015-04-13 18:39:38 -070063
64 this.id = Objects.hash(type, key, conditions, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070065 timeout, appId, priority);
alshabibfaa1e362015-04-02 15:01:54 -070066 }
67
alshabibfaa1e362015-04-02 15:01:54 -070068 @Override
alshabiba3a476d2015-04-10 14:35:38 -070069 public Criterion key() {
70 return key;
71 }
72
73 @Override
alshabib910aff12015-04-09 16:55:57 -070074 public Type type() {
75 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070076 }
77
78 @Override
79 public Collection<Criterion> conditions() {
80 return conditions;
81 }
82
83 @Override
84 public int id() {
85 return id;
86 }
87
88 @Override
Saurav Das88f4c842015-10-27 13:13:19 -070089 public TrafficTreatment meta() {
90 return meta;
91 }
92
93
94 @Override
alshabibfaa1e362015-04-02 15:01:54 -070095 public int priority() {
96 return priority;
97 }
98
99 @Override
100 public ApplicationId appId() {
101 return appId;
102 }
103
104 @Override
105 public int timeout() {
106 return timeout;
107 }
108
109 @Override
110 public boolean permanent() {
111 return permanent;
112 }
113
114 @Override
115 public Operation op() {
116 return op;
117 }
118
alshabib2a441c62015-04-13 18:39:38 -0700119 @Override
120 public Optional<ObjectiveContext> context() {
121 return context;
122 }
123
alshabibfaa1e362015-04-02 15:01:54 -0700124 /**
125 * Returns a new builder.
126 *
127 * @return new builder
128 */
129 public static Builder builder() {
130 return new Builder();
131 }
132
133
134 public static final class Builder implements FilteringObjective.Builder {
135 private final ImmutableList.Builder<Criterion> listBuilder
136 = ImmutableList.builder();
137
alshabib910aff12015-04-09 16:55:57 -0700138 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700139 private boolean permanent = DEFAULT_PERMANENT;
140 private int timeout = DEFAULT_TIMEOUT;
141 private ApplicationId appId;
142 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700143 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700144 private List<Criterion> conditions;
145 private Operation op;
146 private ObjectiveContext context;
Saurav Das88f4c842015-10-27 13:13:19 -0700147 private TrafficTreatment meta;
alshabiba3a476d2015-04-10 14:35:38 -0700148
149 @Override
150 public Builder withKey(Criterion key) {
151 this.key = key;
152 return this;
153 }
alshabibfaa1e362015-04-02 15:01:54 -0700154
155 @Override
156 public Builder addCondition(Criterion criterion) {
157 listBuilder.add(criterion);
158 return this;
159 }
160
161 @Override
alshabib910aff12015-04-09 16:55:57 -0700162 public Builder permit() {
163 this.type = Type.PERMIT;
164 return this;
165 }
166
167 @Override
168 public Builder deny() {
169 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700170 return this;
171 }
172
173 @Override
174 public Builder makeTemporary(int timeout) {
175 this.timeout = timeout;
176 permanent = false;
177 return this;
178 }
179
180 @Override
181 public Builder makePermanent() {
182 permanent = true;
183 return this;
184 }
185
186 @Override
187 public Builder fromApp(ApplicationId appId) {
188 this.appId = appId;
189 return this;
190 }
191
192 @Override
193 public Builder withPriority(int priority) {
194 this.priority = priority;
195 return this;
196 }
197
198 @Override
Saurav Das4ce45962015-11-24 23:21:05 -0800199 public Builder withMeta(TrafficTreatment treatment) {
Saurav Das88f4c842015-10-27 13:13:19 -0700200 this.meta = treatment;
201 return this;
202 }
203
204 @Override
alshabibfaa1e362015-04-02 15:01:54 -0700205 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700206 conditions = listBuilder.build();
207 op = Operation.ADD;
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
Ray Milkey810d6e72015-08-14 10:35:06 -0700212 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700213
214 }
215
216 @Override
217 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700218 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");
Ray Milkey810d6e72015-08-14 10:35:06 -0700222 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700223
Ray Milkey810d6e72015-08-14 10:35:06 -0700224 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700225
226 }
227
alshabib2a441c62015-04-13 18:39:38 -0700228 @Override
229 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700230 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700231 checkNotNull(type, "Must have a type.");
232 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
233 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700234 op = Operation.ADD;
235 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700236
Ray Milkey810d6e72015-08-14 10:35:06 -0700237 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700238 }
239
240 @Override
241 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700242 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700243 checkNotNull(type, "Must have a type.");
244 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
245 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700246 op = Operation.REMOVE;
247 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700248
Ray Milkey810d6e72015-08-14 10:35:06 -0700249 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700250 }
251
alshabibfaa1e362015-04-02 15:01:54 -0700252
253 }
254
255}