blob: e826bfa2010ed836d12f32cde2f16e07064589ec [file] [log] [blame]
Sithara Punnasseryff114552017-01-10 11:40:55 -08001/*
2 * Copyright 2016-present 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.config;
17
18
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080019import org.onosproject.config.model.ResourceId;
Sithara Punnasseryff114552017-01-10 11:40:55 -080020
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080021import java.util.LinkedHashSet;
Sithara Punnasseryff114552017-01-10 11:40:55 -080022import java.util.Set;
23
24/**
25 * Abstraction for Filters that can be used while traversing the dynamic config store.
26 * This abstraction allows to select entries of interest based on various criteria
27 * defined by this interface.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080028 * NOTE: Only criteria based on {@code ResourceId} are supported currently.
29 * This is a placeholder for a filter; Set of ResourceId becomes inefficient when
Sithara Punnasseryff114552017-01-10 11:40:55 -080030 * using a large number of filtering criteria;
31 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080032public class Filter {
33 /**
34 * Traversal modes.
35 */
36 public enum TraversalMode {
Sithara Punnasseryff114552017-01-10 11:40:55 -080037 SUB_TREE(-1),
38 NODE_ONLY(0),
39 GIVEN_DEPTH;
Sithara Punnasseryff114552017-01-10 11:40:55 -080040 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080041 * SUB_TREE => if the node points to a subtree, the entire subtree will
42 * be traversed; if pointing to a leaf, just the leaf will be retrieved.
43 * NODE_ONLY => tree will not be traversed; will retrieve just the
44 * specific node, irrespective of it being a subtree root or a leaf node
45 * GIVEN_DEPTH => as many levels of the subtree as indicated by depth
46 * field of filter that will be traversed; if depth > the number of
47 * levels of children, the entire subtree will be traversed and end
48 * the traversal, without throwing any errors.
Sithara Punnasseryff114552017-01-10 11:40:55 -080049 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080050 int val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080051 TraversalMode() {
52
53 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080054 TraversalMode(int val) {
55 this.val = val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080056 }
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080057 int val() {
58 return val;
Sithara Punnasseryff114552017-01-10 11:40:55 -080059 }
60 }
61
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080062 /** Filtering criteria.
Sithara Punnasseryff114552017-01-10 11:40:55 -080063 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080064 Set<ResourceId> criteria = new LinkedHashSet<ResourceId>();
65 /** Traversal mode; default is to read just the given node(NODE_ONLY).
66 */
67 TraversalMode mode = TraversalMode.NODE_ONLY;
68 /** depth of traversal; default value is 0.
69 */
70 int depth = TraversalMode.NODE_ONLY.val();
Sithara Punnasseryff114552017-01-10 11:40:55 -080071
72 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -080073 * Creates a new Filter object.
74 */
75 public Filter() {
76
77 }
78 /**
79 * Creates a new Filter object.
80 *
81 * @param criteria set of filtering criteria
82 * @param mode traversal mode
83 * @param depth depth of traversal
84 */
85 public Filter(Set<ResourceId> criteria, TraversalMode mode, int depth) {
86 this.criteria = criteria;
87 this.mode = mode;
88 this.depth = depth;
89
90 }
91
92 /**
93 * Returns the traversal mode.
94 *
95 *@return traversal mode
96 */
97 TraversalMode mode() {
98 return mode;
99 }
100
101 /**
102 * Sets the traversal mode.
103 *
104 * @param mode traversal mode
105 */
106 void mode(TraversalMode mode) {
107 this.mode = mode;
108 }
109
110 /**
111 * Returns the depth.
112 *
113 *@return depth
114 */
115 int depth() {
116 return depth;
117 }
118
119 /**
120 * Sets the depth.
121 *
122 * @param depth of traversal
123 */
124 void mode(int depth) {
125 this.depth = depth;
126 }
127 /**
128 * Adds a new ResourceId filtering criterion to a Filter object.
129 * If the same ResourceId is already part of the criteria
Sithara Punnasseryff114552017-01-10 11:40:55 -0800130 * for the object, it will not be added again, but will not throw any exceptions.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800131 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800132 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800133 * @param add new criterion
Sithara Punnasseryff114552017-01-10 11:40:55 -0800134 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800135 void addCriteria(ResourceId add) {
136 criteria.add(add);
137 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800138
139 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800140 * Adds new ResourceId filtering criteria to a Filter object.
141 * If the same ResourceId is already part of the criteria
142 * for the object, it will not be added again, but will not throw any exceptions.
143 * This will not check for the validity of the ResourceId.
144 *
145 * @param addAll new criteria
146 */
147 void addCriteria(Set<ResourceId> addAll) {
148 criteria.addAll(addAll);
149 }
150
151 /**
152 * Removes the given ResourceId filtering criterion from a Filter object.
153 * If the ResourceId was NOT already part of the criteria for
Sithara Punnasseryff114552017-01-10 11:40:55 -0800154 * the object, it will not be removed, but will not throw any exceptions.
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800155 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800156 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800157 * @param remove criterion to be removed
Sithara Punnasseryff114552017-01-10 11:40:55 -0800158 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800159 void removeCriteria(ResourceId remove) {
160 criteria.remove(remove);
161 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800162
163 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800164 * Removes the given ResourceId filtering criteria from a Filter object.
165 * If the ResourceId was NOT already part of the criteria for
166 * the object, it will not be removed, but will not throw any exceptions.
167 * This will not check for the validity of the ResourceId.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800168 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800169 * @param removeAll criteria to be removed
Sithara Punnasseryff114552017-01-10 11:40:55 -0800170 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800171 void removeCriteria(Set<ResourceId> removeAll) {
172 criteria.removeAll(removeAll);
173 }
174
175 /**
176 * Returns the criteria that are in place for a Filter.
177 *
178 * @return Set of ResourceId criteria
179 */
180 Set<ResourceId> criteria() {
181 return this.criteria;
182 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800183
184 /**
185 * Method to create a filter that include all entries rejected by the criteria.
186 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800187 * @param original Filter object with a criteria set
Sithara Punnasseryff114552017-01-10 11:40:55 -0800188 * @return Filter object with negated criteria set
189 * @throws InvalidFilterException if the received Filter object
190 * was null or if it had an empty criteria set
191 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800192 Filter negateFilter(Filter original) {
193 throw new FailedException("Not yet implemented");
194 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800195
196 /**
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800197 * Returns if the Filter has an empty criteria set.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800198 *
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800199 * @return {@code true} if criteria set is empty, {@code false} otherwise.
Sithara Punnasseryff114552017-01-10 11:40:55 -0800200 */
Sithara Punnassery9306e6b2017-02-06 15:38:19 -0800201 boolean isEmptyFilter() {
202 return criteria.isEmpty();
203 }
Sithara Punnasseryff114552017-01-10 11:40:55 -0800204}