Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.config; |
| 17 | |
| 18 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 19 | import org.onosproject.config.model.ResourceId; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 20 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 21 | import java.util.LinkedHashSet; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 22 | import 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 28 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 30 | * using a large number of filtering criteria; |
| 31 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 32 | public class Filter { |
| 33 | /** |
| 34 | * Traversal modes. |
| 35 | */ |
| 36 | public enum TraversalMode { |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 37 | SUB_TREE(-1), |
| 38 | NODE_ONLY(0), |
| 39 | GIVEN_DEPTH; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 40 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 41 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 49 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 50 | int val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 51 | TraversalMode() { |
| 52 | |
| 53 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 54 | TraversalMode(int val) { |
| 55 | this.val = val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 56 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 57 | int val() { |
| 58 | return val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 62 | /** Filtering criteria. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 63 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 64 | 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 71 | |
| 72 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 73 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 130 | * for the object, it will not be added again, but will not throw any exceptions. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 131 | * This will not check for the validity of the ResourceId. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 132 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 133 | * @param add new criterion |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 134 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 135 | void addCriteria(ResourceId add) { |
| 136 | criteria.add(add); |
| 137 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 138 | |
| 139 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 140 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 154 | * the object, it will not be removed, but will not throw any exceptions. |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 155 | * This will not check for the validity of the ResourceId. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 156 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 157 | * @param remove criterion to be removed |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 158 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 159 | void removeCriteria(ResourceId remove) { |
| 160 | criteria.remove(remove); |
| 161 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 162 | |
| 163 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 164 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 168 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 169 | * @param removeAll criteria to be removed |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 170 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 171 | 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Method to create a filter that include all entries rejected by the criteria. |
| 186 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 187 | * @param original Filter object with a criteria set |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 188 | * @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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 192 | Filter negateFilter(Filter original) { |
| 193 | throw new FailedException("Not yet implemented"); |
| 194 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 195 | |
| 196 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 197 | * Returns if the Filter has an empty criteria set. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 198 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 199 | * @return {@code true} if criteria set is empty, {@code false} otherwise. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 200 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 201 | boolean isEmptyFilter() { |
| 202 | return criteria.isEmpty(); |
| 203 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 204 | } |