blob: 7b5924fb05db7cfbf816c27017f16138de16269b [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;
alshabiba3a476d2015-04-10 14:35:38 -070021import org.onosproject.net.flow.criteria.Criteria;
alshabibfaa1e362015-04-02 15:01:54 -070022import org.onosproject.net.flow.criteria.Criterion;
23
24import java.util.Collection;
25import java.util.List;
26import java.util.Objects;
alshabib2a441c62015-04-13 18:39:38 -070027import java.util.Optional;
alshabibfaa1e362015-04-02 15:01:54 -070028
29import static com.google.common.base.Preconditions.checkArgument;
30import static com.google.common.base.Preconditions.checkNotNull;
31
32/**
33 * Default implementation of a filtering objective.
34 */
Thomas Vachuskaa9d491e2015-05-20 11:17:21 -070035@Beta
alshabibfaa1e362015-04-02 15:01:54 -070036public final class DefaultFilteringObjective implements FilteringObjective {
37
38
alshabib910aff12015-04-09 16:55:57 -070039 private final Type type;
alshabibfaa1e362015-04-02 15:01:54 -070040 private final boolean permanent;
41 private final int timeout;
42 private final ApplicationId appId;
43 private final int priority;
alshabiba3a476d2015-04-10 14:35:38 -070044 private final Criterion key;
alshabibfaa1e362015-04-02 15:01:54 -070045 private final List<Criterion> conditions;
46 private final int id;
47 private final Operation op;
alshabib2a441c62015-04-13 18:39:38 -070048 private final Optional<ObjectiveContext> context;
alshabibfaa1e362015-04-02 15:01:54 -070049
Ray Milkey810d6e72015-08-14 10:35:06 -070050 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;
58 this.op = builder.op;
59 this.context = Optional.ofNullable(builder.context);
alshabib2a441c62015-04-13 18:39:38 -070060
61 this.id = Objects.hash(type, key, conditions, permanent,
Ray Milkey810d6e72015-08-14 10:35:06 -070062 timeout, appId, priority);
alshabibfaa1e362015-04-02 15:01:54 -070063 }
64
alshabibfaa1e362015-04-02 15:01:54 -070065 @Override
alshabiba3a476d2015-04-10 14:35:38 -070066 public Criterion key() {
67 return key;
68 }
69
70 @Override
alshabib910aff12015-04-09 16:55:57 -070071 public Type type() {
72 return this.type;
alshabibfaa1e362015-04-02 15:01:54 -070073 }
74
75 @Override
76 public Collection<Criterion> conditions() {
77 return conditions;
78 }
79
80 @Override
81 public int id() {
82 return id;
83 }
84
85 @Override
86 public int priority() {
87 return priority;
88 }
89
90 @Override
91 public ApplicationId appId() {
92 return appId;
93 }
94
95 @Override
96 public int timeout() {
97 return timeout;
98 }
99
100 @Override
101 public boolean permanent() {
102 return permanent;
103 }
104
105 @Override
106 public Operation op() {
107 return op;
108 }
109
alshabib2a441c62015-04-13 18:39:38 -0700110 @Override
111 public Optional<ObjectiveContext> context() {
112 return context;
113 }
114
alshabibfaa1e362015-04-02 15:01:54 -0700115 /**
116 * Returns a new builder.
117 *
118 * @return new builder
119 */
120 public static Builder builder() {
121 return new Builder();
122 }
123
124
125 public static final class Builder implements FilteringObjective.Builder {
126 private final ImmutableList.Builder<Criterion> listBuilder
127 = ImmutableList.builder();
128
alshabib910aff12015-04-09 16:55:57 -0700129 private Type type;
alshabibfaa1e362015-04-02 15:01:54 -0700130 private boolean permanent = DEFAULT_PERMANENT;
131 private int timeout = DEFAULT_TIMEOUT;
132 private ApplicationId appId;
133 private int priority = DEFAULT_PRIORITY;
alshabiba3a476d2015-04-10 14:35:38 -0700134 private Criterion key = Criteria.dummy();
Ray Milkey810d6e72015-08-14 10:35:06 -0700135 private List<Criterion> conditions;
136 private Operation op;
137 private ObjectiveContext context;
alshabiba3a476d2015-04-10 14:35:38 -0700138
139 @Override
140 public Builder withKey(Criterion key) {
141 this.key = key;
142 return this;
143 }
alshabibfaa1e362015-04-02 15:01:54 -0700144
145 @Override
146 public Builder addCondition(Criterion criterion) {
147 listBuilder.add(criterion);
148 return this;
149 }
150
151 @Override
alshabib910aff12015-04-09 16:55:57 -0700152 public Builder permit() {
153 this.type = Type.PERMIT;
154 return this;
155 }
156
157 @Override
158 public Builder deny() {
159 this.type = Type.DENY;
alshabibfaa1e362015-04-02 15:01:54 -0700160 return this;
161 }
162
163 @Override
164 public Builder makeTemporary(int timeout) {
165 this.timeout = timeout;
166 permanent = false;
167 return this;
168 }
169
170 @Override
171 public Builder makePermanent() {
172 permanent = true;
173 return this;
174 }
175
176 @Override
177 public Builder fromApp(ApplicationId appId) {
178 this.appId = appId;
179 return this;
180 }
181
182 @Override
183 public Builder withPriority(int priority) {
184 this.priority = priority;
185 return this;
186 }
187
188 @Override
189 public FilteringObjective add() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700190 conditions = listBuilder.build();
191 op = Operation.ADD;
alshabib910aff12015-04-09 16:55:57 -0700192 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700193 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
194 checkNotNull(appId, "Must supply an application id");
195
Ray Milkey810d6e72015-08-14 10:35:06 -0700196 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700197
198 }
199
200 @Override
201 public FilteringObjective remove() {
Ray Milkey810d6e72015-08-14 10:35:06 -0700202 conditions = listBuilder.build();
alshabib910aff12015-04-09 16:55:57 -0700203 checkNotNull(type, "Must have a type.");
alshabibfaa1e362015-04-02 15:01:54 -0700204 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
205 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700206 op = Operation.REMOVE;
alshabibfaa1e362015-04-02 15:01:54 -0700207
Ray Milkey810d6e72015-08-14 10:35:06 -0700208 return new DefaultFilteringObjective(this);
alshabibfaa1e362015-04-02 15:01:54 -0700209
210 }
211
alshabib2a441c62015-04-13 18:39:38 -0700212 @Override
213 public FilteringObjective add(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700214 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700215 checkNotNull(type, "Must have a type.");
216 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
217 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700218 op = Operation.ADD;
219 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700220
Ray Milkey810d6e72015-08-14 10:35:06 -0700221 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700222 }
223
224 @Override
225 public FilteringObjective remove(ObjectiveContext context) {
Ray Milkey810d6e72015-08-14 10:35:06 -0700226 conditions = listBuilder.build();
alshabib2a441c62015-04-13 18:39:38 -0700227 checkNotNull(type, "Must have a type.");
228 checkArgument(!conditions.isEmpty(), "Must have at least one condition.");
229 checkNotNull(appId, "Must supply an application id");
Ray Milkey810d6e72015-08-14 10:35:06 -0700230 op = Operation.REMOVE;
231 this.context = context;
alshabib2a441c62015-04-13 18:39:38 -0700232
Ray Milkey810d6e72015-08-14 10:35:06 -0700233 return new DefaultFilteringObjective(this);
alshabib2a441c62015-04-13 18:39:38 -0700234 }
235
alshabibfaa1e362015-04-02 15:01:54 -0700236
237 }
238
239}