blob: 1f0fbd16da62c6cb649b5fcca0ae80ad144a2652 [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;
Sithara Punnassery4b091dc2017-03-02 17:22:40 -080020import org.onosproject.yang.model.ResourceId;
Sithara Punnasseryff114552017-01-10 11:40:55 -080021
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080022import java.util.LinkedHashSet;
Sithara Punnasseryff114552017-01-10 11:40:55 -080023import java.util.Set;
24
25/**
26 * Abstraction for Filters that can be used while traversing the dynamic config store.
27 * This abstraction allows to select entries of interest based on various criteria
28 * defined by this interface.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080029 * NOTE: Only criteria based on {@code ResourceId} are supported currently.
30 * This is a placeholder for a filter; Set of ResourceId becomes inefficient when
Sithara Punnasseryff114552017-01-10 11:40:55 -080031 * using a large number of filtering criteria;
32 */
Sithara Punnassery06208792017-02-10 16:25:29 -080033@Beta
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080034public class Filter {
35 /**
36 * Traversal modes.
37 */
38 public enum TraversalMode {
Sithara Punnasseryff114552017-01-10 11:40:55 -080039 SUB_TREE(-1),
40 NODE_ONLY(0),
41 GIVEN_DEPTH;
Sithara Punnasseryff114552017-01-10 11:40:55 -080042 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080043 * SUB_TREE => if the node points to a subtree, the entire subtree will
44 * be traversed; if pointing to a leaf, just the leaf will be retrieved.
45 * NODE_ONLY => tree will not be traversed; will retrieve just the
46 * specific node, irrespective of it being a subtree root or a leaf node
47 * GIVEN_DEPTH => as many levels of the subtree as indicated by depth
48 * field of filter that will be traversed; if depth > the number of
49 * levels of children, the entire subtree will be traversed and end
50 * the traversal, without throwing any errors.
Sithara Punnasseryff114552017-01-10 11:40:55 -080051 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080052 int val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080053 TraversalMode() {
54
55 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080056 TraversalMode(int val) {
57 this.val = val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080058 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080059 int val() {
60 return val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080061 }
62 }
63
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080064 /** Filtering criteria.
Sithara Punnasseryff114552017-01-10 11:40:55 -080065 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080066 Set<ResourceId> criteria = new LinkedHashSet<ResourceId>();
67 /** Traversal mode; default is to read just the given node(NODE_ONLY).
68 */
69 TraversalMode mode = TraversalMode.NODE_ONLY;
70 /** depth of traversal; default value is 0.
71 */
72 int depth = TraversalMode.NODE_ONLY.val();
Sithara Punnasseryff114552017-01-10 11:40:55 -080073
74 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080075 * Creates a new Filter object.
76 */
77 public Filter() {
78
79 }
80 /**
81 * Creates a new Filter object.
82 *
83 * @param criteria set of filtering criteria
84 * @param mode traversal mode
85 * @param depth depth of traversal
86 */
87 public Filter(Set<ResourceId> criteria, TraversalMode mode, int depth) {
88 this.criteria = criteria;
89 this.mode = mode;
90 this.depth = depth;
91
92 }
93
94 /**
95 * Returns the traversal mode.
96 *
97 *@return traversal mode
98 */
99 TraversalMode mode() {
100 return mode;
101 }
102
103 /**
104 * Sets the traversal mode.
105 *
106 * @param mode traversal mode
107 */
108 void mode(TraversalMode mode) {
109 this.mode = mode;
110 }
111
112 /**
113 * Returns the depth.
114 *
115 *@return depth
116 */
117 int depth() {
118 return depth;
119 }
120
121 /**
122 * Sets the depth.
123 *
124 * @param depth of traversal
125 */
126 void mode(int depth) {
127 this.depth = depth;
128 }
129 /**
130 * Adds a new ResourceId filtering criterion to a Filter object.
131 * If the same ResourceId is already part of the criteria
Sithara Punnasseryff114552017-01-10 11:40:55 -0800132 * for the object, it will not be added again, but will not throw any exceptions.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800133 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800134 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800135 * @param add new criterion
Sithara Punnasseryff114552017-01-10 11:40:55 -0800136 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800137 void addCriteria(ResourceId add) {
138 criteria.add(add);
139 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800140
141 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800142 * Adds new ResourceId filtering criteria to a Filter object.
143 * If the same ResourceId is already part of the criteria
144 * for the object, it will not be added again, but will not throw any exceptions.
145 * This will not check for the validity of the ResourceId.
146 *
147 * @param addAll new criteria
148 */
149 void addCriteria(Set<ResourceId> addAll) {
150 criteria.addAll(addAll);
151 }
152
153 /**
154 * Removes the given ResourceId filtering criterion from a Filter object.
155 * If the ResourceId was NOT already part of the criteria for
Sithara Punnasseryff114552017-01-10 11:40:55 -0800156 * the object, it will not be removed, but will not throw any exceptions.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800157 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800158 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800159 * @param remove criterion to be removed
Sithara Punnasseryff114552017-01-10 11:40:55 -0800160 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800161 void removeCriteria(ResourceId remove) {
162 criteria.remove(remove);
163 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800164
165 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800166 * Removes the given ResourceId filtering criteria from a Filter object.
167 * If the ResourceId was NOT already part of the criteria for
168 * the object, it will not be removed, but will not throw any exceptions.
169 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800170 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800171 * @param removeAll criteria to be removed
Sithara Punnasseryff114552017-01-10 11:40:55 -0800172 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800173 void removeCriteria(Set<ResourceId> removeAll) {
174 criteria.removeAll(removeAll);
175 }
176
177 /**
178 * Returns the criteria that are in place for a Filter.
179 *
180 * @return Set of ResourceId criteria
181 */
182 Set<ResourceId> criteria() {
183 return this.criteria;
184 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800185
186 /**
187 * Method to create a filter that include all entries rejected by the criteria.
188 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800189 * @param original Filter object with a criteria set
Sithara Punnasseryff114552017-01-10 11:40:55 -0800190 * @return Filter object with negated criteria set
191 * @throws InvalidFilterException if the received Filter object
192 * was null or if it had an empty criteria set
193 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800194 Filter negateFilter(Filter original) {
195 throw new FailedException("Not yet implemented");
196 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800197
198 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800199 * Returns if the Filter has an empty criteria set.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800200 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800201 * @return {@code true} if criteria set is empty, {@code false} otherwise.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800202 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800203 boolean isEmptyFilter() {
204 return criteria.isEmpty();
205 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800206}