blob: 0d332aeff5fa0f546b8781c4270445964d1c2b32 [file] [log] [blame]
Sithara Punnasseryff114552017-01-10 11:40:55 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Sithara Punnasseryff114552017-01-10 11:40:55 -08003 *
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.config;
17
18
Sithara Punnassery06208792017-02-10 16:25:29 -080019import com.google.common.annotations.Beta;
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070020import com.google.common.collect.ImmutableSet;
21
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080022import org.onosproject.yang.model.ResourceId;
Sithara Punnasseryff114552017-01-10 11:40:55 -080023
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070024import static com.google.common.base.Preconditions.checkNotNull;
25
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080026import java.util.LinkedHashSet;
Sithara Punnasseryff114552017-01-10 11:40:55 -080027import java.util.Set;
28
29/**
30 * Abstraction for Filters that can be used while traversing the dynamic config store.
31 * This abstraction allows to select entries of interest based on various criteria
32 * defined by this interface.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080033 * NOTE: Only criteria based on {@code ResourceId} are supported currently.
34 * This is a placeholder for a filter; Set of ResourceId becomes inefficient when
Sithara Punnasseryff114552017-01-10 11:40:55 -080035 * using a large number of filtering criteria;
36 */
Sithara Punnassery06208792017-02-10 16:25:29 -080037@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080038public class Filter {
39 /**
40 * Traversal modes.
41 */
42 public enum TraversalMode {
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070043
Sithara Punnasseryff114552017-01-10 11:40:55 -080044 /**
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070045 * SUB_TREE : if the node points to a subtree, the entire subtree will
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080046 * be traversed; if pointing to a leaf, just the leaf will be retrieved.
Sithara Punnasseryff114552017-01-10 11:40:55 -080047 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070048 SUB_TREE(-1),
49 /**
50 * NODE_ONLY : tree will not be traversed; will retrieve just the
51 * specific node, irrespective of it being a subtree root or a leaf node.
52 */
53 NODE_ONLY(0),
54 /**
55 * GIVEN_DEPTH : as many levels of the subtree as indicated by depth
56 * field of filter that will be traversed; if depth is greater than
57 * the number of levels of children, the entire subtree will be
58 * traversed and end the traversal, without throwing any errors.
59 */
60 GIVEN_DEPTH;
61
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062 int val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080063 TraversalMode() {
64
65 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080066 TraversalMode(int val) {
67 this.val = val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080068 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080069 int val() {
70 return val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080071 }
72 }
73
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070074 /**
75 * Filtering criteria.
Sithara Punnasseryff114552017-01-10 11:40:55 -080076 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070077 private final Set<ResourceId> criteria;
78 /**
79 * Traversal mode; default is to read just the given node(NODE_ONLY).
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080080 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070081 private final TraversalMode mode;
82 /**
83 * depth of traversal; default value is 0.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080084 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070085 private final int depth;
86
Sithara Punnasseryff114552017-01-10 11:40:55 -080087 /**
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070088 * Creates a default Filter builder.
89 *
90 * @return Filter builder
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080091 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070092 public static FilterBuilder builder() {
93 return new FilterBuilder();
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080094 }
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -070095
96 /**
97 * Creates a Filter builder based on {@code start}.
98 *
99 * @param start Filter to initialize builder with
100 * @return Filter builder
101 *
102 */
103 public static FilterBuilder builder(Filter start) {
104 return new FilterBuilder(start);
105 }
106
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800107 /**
108 * Creates a new Filter object.
109 *
110 * @param criteria set of filtering criteria
111 * @param mode traversal mode
112 * @param depth depth of traversal
113 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700114 protected Filter(Set<ResourceId> criteria, TraversalMode mode, int depth) {
115 this.criteria = ImmutableSet.copyOf(criteria);
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800116 this.mode = mode;
117 this.depth = depth;
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800118 }
119
120 /**
121 * Returns the traversal mode.
122 *
123 *@return traversal mode
124 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700125 public TraversalMode mode() {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800126 return mode;
127 }
128
129 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800130 * Returns the depth.
131 *
132 *@return depth
133 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700134 public int depth() {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800135 return depth;
136 }
137
138 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800139 * Returns the criteria that are in place for a Filter.
140 *
141 * @return Set of ResourceId criteria
142 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700143 public Set<ResourceId> criteria() {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800144 return this.criteria;
145 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800146
147 /**
148 * Method to create a filter that include all entries rejected by the criteria.
149 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800150 * @param original Filter object with a criteria set
Sithara Punnasseryff114552017-01-10 11:40:55 -0800151 * @return Filter object with negated criteria set
152 * @throws InvalidFilterException if the received Filter object
153 * was null or if it had an empty criteria set
154 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700155 public static Filter negateFilter(Filter original) {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800156 throw new FailedException("Not yet implemented");
157 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800158
159 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800160 * Returns if the Filter has an empty criteria set.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800161 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800162 * @return {@code true} if criteria set is empty, {@code false} otherwise.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800163 */
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700164 public boolean isEmptyFilter() {
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800165 return criteria.isEmpty();
166 }
Yuta HIGUCHIac85ee12017-08-03 20:07:35 -0700167
168 public static final class FilterBuilder extends Builder<FilterBuilder> {
169
170 private FilterBuilder(Filter start) {
171 super(start.criteria, start.mode, start.depth);
172 }
173
174 private FilterBuilder() {
175 super();
176 }
177
178 public Filter build() {
179 return new Filter(criteria, mode, depth);
180 }
181 }
182
183 public abstract static class Builder<B extends Builder<B>> {
184
185 protected Set<ResourceId> criteria;
186 protected TraversalMode mode;
187 protected int depth;
188
189 protected Builder() {
190 this(ImmutableSet.of(), TraversalMode.NODE_ONLY, 0);
191 }
192
193 protected Builder(Set<ResourceId> criteria,
194 TraversalMode mode,
195 int depth) {
196 this.criteria = new LinkedHashSet<>(criteria);
197 this.mode = checkNotNull(mode);
198 this.depth = depth;
199 }
200
201 /**
202 * Adds a new ResourceId filtering criterion to a Filter object.
203 * If the same ResourceId is already part of the criteria
204 * for the object, it will not be added again, but will not throw any exceptions.
205 * This will not check for the validity of the ResourceId.
206 *
207 * @param add new criterion
208 * @return self
209 */
210 public B addCriteria(ResourceId add) {
211 criteria.add(add);
212 return (B) this;
213 }
214
215 /**
216 * Adds new ResourceId filtering criteria to a Filter object.
217 * If the same ResourceId is already part of the criteria
218 * for the object, it will not be added again, but will not throw any exceptions.
219 * This will not check for the validity of the ResourceId.
220 *
221 * @param addAll new criteria
222 * @return self
223 */
224 public B addAllCriteria(Set<ResourceId> addAll) {
225 criteria.addAll(addAll);
226 return (B) this;
227 }
228
229 /**
230 * Replaces ResourceId filtering criteria with the one specified.
231 * This will not check for the validity of the ResourceId.
232 *
233 * @param criteria new criteria
234 * @return self
235 */
236 public B setCriteria(Set<ResourceId> criteria) {
237 this.criteria = (criteria);
238 return (B) this;
239 }
240
241 /**
242 * Sets the traversal mode.
243 *
244 * @param mode traversal mode
245 * @return self
246 */
247 public B mode(TraversalMode mode) {
248 this.mode = mode;
249 return (B) this;
250 }
251
252 /**
253 * Sets the depth.
254 *
255 * @param depth of traversal
256 * @return self
257 */
258 public B depth(int depth) {
259 this.depth = depth;
260 return (B) this;
261 }
262 /**
263 * Removes the given ResourceId filtering criterion from a Filter object.
264 * If the ResourceId was NOT already part of the criteria for
265 * the object, it will not be removed, but will not throw any exceptions.
266 * This will not check for the validity of the ResourceId.
267 *
268 * @param remove criterion to be removed
269 * @return self
270 */
271 public B removeCriteria(ResourceId remove) {
272 criteria.remove(remove);
273 return (B) this;
274 }
275
276 /**
277 * Removes the given ResourceId filtering criteria from a Filter object.
278 * If the ResourceId was NOT already part of the criteria for
279 * the object, it will not be removed, but will not throw any exceptions.
280 * This will not check for the validity of the ResourceId.
281 *
282 * @param removeAll criteria to be removed
283 * @return self
284 */
285 public B removeAllCriteria(Set<ResourceId> removeAll) {
286 criteria.removeAll(removeAll);
287 return (B) this;
288 }
289 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800290}