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 | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 19 | import com.google.common.annotations.Beta; |
Sithara Punnassery | 4b091dc | 2017-03-02 17:22:40 -0800 | [diff] [blame] | 20 | import org.onosproject.yang.model.ResourceId; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 21 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 22 | import java.util.LinkedHashSet; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 23 | import 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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 29 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 31 | * using a large number of filtering criteria; |
| 32 | */ |
Sithara Punnassery | 0620879 | 2017-02-10 16:25:29 -0800 | [diff] [blame] | 33 | @Beta |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 34 | public class Filter { |
| 35 | /** |
| 36 | * Traversal modes. |
| 37 | */ |
| 38 | public enum TraversalMode { |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 39 | SUB_TREE(-1), |
| 40 | NODE_ONLY(0), |
| 41 | GIVEN_DEPTH; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 42 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 43 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 51 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 52 | int val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 53 | TraversalMode() { |
| 54 | |
| 55 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 56 | TraversalMode(int val) { |
| 57 | this.val = val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 58 | } |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 59 | int val() { |
| 60 | return val; |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 64 | /** Filtering criteria. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 65 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 66 | 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 75 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 132 | * 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] | 133 | * This will not check for the validity of the ResourceId. |
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 | * @param add new criterion |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 136 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 137 | void addCriteria(ResourceId add) { |
| 138 | criteria.add(add); |
| 139 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 140 | |
| 141 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 142 | * 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 156 | * 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] | 157 | * This will not check for the validity of the ResourceId. |
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 | * @param remove criterion to be removed |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 160 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 161 | void removeCriteria(ResourceId remove) { |
| 162 | criteria.remove(remove); |
| 163 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 164 | |
| 165 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 166 | * 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 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 | * @param removeAll criteria to be removed |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 172 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 173 | 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 Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 185 | |
| 186 | /** |
| 187 | * Method to create a filter that include all entries rejected by the criteria. |
| 188 | * |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 189 | * @param original Filter object with a criteria set |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 190 | * @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 Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 194 | Filter negateFilter(Filter original) { |
| 195 | throw new FailedException("Not yet implemented"); |
| 196 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 197 | |
| 198 | /** |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 199 | * Returns if the Filter has an empty criteria set. |
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 | * @return {@code true} if criteria set is empty, {@code false} otherwise. |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 202 | */ |
Sithara Punnassery | 9306e6b | 2017-02-06 15:38:19 -0800 | [diff] [blame] | 203 | boolean isEmptyFilter() { |
| 204 | return criteria.isEmpty(); |
| 205 | } |
Sithara Punnassery | ff11455 | 2017-01-10 11:40:55 -0800 | [diff] [blame] | 206 | } |