Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | */ |
Ray Milkey | a412236 | 2015-08-18 15:19:08 -0700 | [diff] [blame] | 16 | package org.onosproject.net.config; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 17 | |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 22 | import com.google.common.annotations.Beta; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import com.google.common.collect.Iterators; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 25 | import com.google.common.collect.Lists; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpAddress; |
| 27 | import org.onlab.packet.MacAddress; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.Collection; |
| 30 | import java.util.List; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 31 | import java.util.Set; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 32 | import java.util.function.Function; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 35 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Base abstraction of a configuration facade for a specific subject. Derived |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 39 | * classes should keep all state in the specified JSON tree as that is the |
| 40 | * only state that will be distributed or persisted; this class is merely |
| 41 | * a facade for interacting with a particular facet of configuration on a |
| 42 | * given subject. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 43 | * |
| 44 | * @param <S> type of subject |
| 45 | */ |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 46 | @Beta |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 47 | public abstract class Config<S> { |
| 48 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 49 | protected S subject; |
| 50 | protected String key; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 51 | |
| 52 | protected JsonNode node; |
| 53 | protected ObjectNode object; |
| 54 | protected ArrayNode array; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 55 | protected ObjectMapper mapper; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 56 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 57 | protected ConfigApplyDelegate delegate; |
| 58 | |
| 59 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 60 | * Indicator of whether a configuration JSON field is required. |
| 61 | */ |
| 62 | public enum FieldPresence { |
| 63 | /** |
| 64 | * Signifies that config field is an optional one. |
| 65 | */ |
| 66 | OPTIONAL, |
| 67 | |
| 68 | /** |
| 69 | * Signifies that config field is mandatory. |
| 70 | */ |
| 71 | MANDATORY |
| 72 | } |
| 73 | |
| 74 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 75 | * Initializes the configuration behaviour with necessary context. |
| 76 | * |
| 77 | * @param subject configuration subject |
| 78 | * @param key configuration key |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 79 | * @param node JSON node where configuration data is stored |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 80 | * @param mapper JSON object mapper |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 81 | * @param delegate delegate context, or null for detached configs. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 82 | */ |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 83 | public final void init(S subject, String key, JsonNode node, ObjectMapper mapper, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 84 | ConfigApplyDelegate delegate) { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 85 | this.subject = checkNotNull(subject, "Subject cannot be null"); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 86 | this.key = key; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 87 | this.node = checkNotNull(node, "Node cannot be null"); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 88 | this.object = node instanceof ObjectNode ? (ObjectNode) node : null; |
| 89 | this.array = node instanceof ArrayNode ? (ArrayNode) node : null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 90 | this.mapper = checkNotNull(mapper, "Mapper cannot be null"); |
| 91 | this.delegate = delegate; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 92 | } |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 93 | |
| 94 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 95 | * Indicates whether or not the backing JSON node contains valid data. |
| 96 | * <p> |
| 97 | * Default implementation returns true. |
| 98 | * Subclasses are expected to override this with their own validation. |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 99 | * Implementations are free to throw a RuntimeException if data is invalid. |
| 100 | * * </p> |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 101 | * |
| 102 | * @return true if the data is valid; false otherwise |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 103 | * @throws RuntimeException if configuration is invalid or completely foobar |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 104 | */ |
| 105 | public boolean isValid() { |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 106 | // Derivatives should use the provided set of predicates to test |
| 107 | // validity of their fields, e.g.: |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 108 | // isString(path) |
| 109 | // isBoolean(path) |
| 110 | // isNumber(path, [min, max]) |
| 111 | // isDecimal(path, [min, max]) |
| 112 | // isMacAddress(path) |
| 113 | // isIpAddress(path) |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | /** |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 118 | * Returns the specific subject to which this configuration pertains. |
| 119 | * |
| 120 | * @return configuration subject |
| 121 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 122 | public S subject() { |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 123 | return subject; |
| 124 | } |
| 125 | |
| 126 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 127 | * Returns the configuration key. This is primarily aimed for use in |
| 128 | * composite JSON trees in external representations and has no bearing on |
| 129 | * the internal behaviours. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 130 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 131 | * @return configuration key |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 132 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 133 | public String key() { |
| 134 | return key; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Returns the JSON node that contains the configuration data. |
| 139 | * |
| 140 | * @return JSON node backing the configuration |
| 141 | */ |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 142 | public JsonNode node() { |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 143 | return node; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Applies any configuration changes made via this configuration. |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 148 | * |
| 149 | * Not effective for detached configs. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 150 | */ |
| 151 | public void apply() { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame^] | 152 | checkState(delegate != null, "Cannot apply detached config"); |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 153 | delegate.onApply(this); |
| 154 | } |
| 155 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 156 | // Miscellaneous helpers for interacting with JSON |
| 157 | |
| 158 | /** |
| 159 | * Gets the specified property as a string. |
| 160 | * |
| 161 | * @param name property name |
| 162 | * @param defaultValue default value if property not set |
| 163 | * @return property value or default value |
| 164 | */ |
| 165 | protected String get(String name, String defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 166 | return object.path(name).asText(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sets the specified property as a string or clears it if null value given. |
| 171 | * |
| 172 | * @param name property name |
| 173 | * @param value new value or null to clear the property |
| 174 | * @return self |
| 175 | */ |
| 176 | protected Config<S> setOrClear(String name, String value) { |
| 177 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 178 | object.put(name, value); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 179 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 180 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 181 | } |
| 182 | return this; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Gets the specified property as a boolean. |
| 187 | * |
| 188 | * @param name property name |
| 189 | * @param defaultValue default value if property not set |
| 190 | * @return property value or default value |
| 191 | */ |
| 192 | protected boolean get(String name, boolean defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 193 | return object.path(name).asBoolean(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | /** |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 197 | * Clears the specified property. |
| 198 | * |
| 199 | * @param name property name |
| 200 | * @return self |
| 201 | */ |
| 202 | protected Config<S> clear(String name) { |
| 203 | object.remove(name); |
| 204 | return this; |
| 205 | } |
| 206 | |
| 207 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 208 | * Sets the specified property as a boolean or clears it if null value given. |
| 209 | * |
| 210 | * @param name property name |
| 211 | * @param value new value or null to clear the property |
| 212 | * @return self |
| 213 | */ |
| 214 | protected Config<S> setOrClear(String name, Boolean value) { |
| 215 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 216 | object.put(name, value.booleanValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 217 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 218 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 219 | } |
| 220 | return this; |
| 221 | } |
| 222 | |
| 223 | /** |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 224 | * Gets the specified property as an integer. |
| 225 | * |
| 226 | * @param name property name |
| 227 | * @param defaultValue default value if property not set |
| 228 | * @return property value or default value |
| 229 | */ |
| 230 | protected int get(String name, int defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 231 | return object.path(name).asInt(defaultValue); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Sets the specified property as an integer or clears it if null value given. |
| 236 | * |
| 237 | * @param name property name |
| 238 | * @param value new value or null to clear the property |
| 239 | * @return self |
| 240 | */ |
| 241 | protected Config<S> setOrClear(String name, Integer value) { |
| 242 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 243 | object.put(name, value.intValue()); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 244 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 245 | object.remove(name); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 246 | } |
| 247 | return this; |
| 248 | } |
| 249 | |
| 250 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 251 | * Gets the specified property as a long. |
| 252 | * |
| 253 | * @param name property name |
| 254 | * @param defaultValue default value if property not set |
| 255 | * @return property value or default value |
| 256 | */ |
| 257 | protected long get(String name, long defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 258 | return object.path(name).asLong(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Sets the specified property as a long or clears it if null value given. |
| 263 | * |
| 264 | * @param name property name |
| 265 | * @param value new value or null to clear the property |
| 266 | * @return self |
| 267 | */ |
| 268 | protected Config<S> setOrClear(String name, Long value) { |
| 269 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 270 | object.put(name, value.longValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 271 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 272 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 273 | } |
| 274 | return this; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Gets the specified property as a double. |
| 279 | * |
| 280 | * @param name property name |
| 281 | * @param defaultValue default value if property not set |
| 282 | * @return property value or default value |
| 283 | */ |
| 284 | protected double get(String name, double defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 285 | return object.path(name).asDouble(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Sets the specified property as a double or clears it if null value given. |
| 290 | * |
| 291 | * @param name property name |
| 292 | * @param value new value or null to clear the property |
| 293 | * @return self |
| 294 | */ |
| 295 | protected Config<S> setOrClear(String name, Double value) { |
| 296 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 297 | object.put(name, value.doubleValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 298 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 299 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 300 | } |
| 301 | return this; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Gets the specified property as an enum. |
| 306 | * |
| 307 | * @param name property name |
| 308 | * @param defaultValue default value if property not set |
| 309 | * @param enumClass the enum class |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 310 | * @param <E> type of enum |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 311 | * @return property value or default value |
| 312 | */ |
| 313 | protected <E extends Enum<E>> E get(String name, E defaultValue, Class<E> enumClass) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 314 | return Enum.valueOf(enumClass, object.path(name).asText(defaultValue.toString())); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Sets the specified property as a double or clears it if null value given. |
| 319 | * |
| 320 | * @param name property name |
| 321 | * @param value new value or null to clear the property |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 322 | * @param <E> type of enum |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 323 | * @return self |
| 324 | */ |
| 325 | protected <E extends Enum> Config<S> setOrClear(String name, E value) { |
| 326 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 327 | object.put(name, value.toString()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 328 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 329 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 330 | } |
| 331 | return this; |
| 332 | } |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 333 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 334 | /** |
| 335 | * Gets the specified array property as a list of items. |
| 336 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 337 | * @param name property name |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 338 | * @param function mapper from string to item |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 339 | * @param <T> type of item |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 340 | * @return list of items |
| 341 | */ |
| 342 | protected <T> List<T> getList(String name, Function<String, T> function) { |
| 343 | List<T> list = Lists.newArrayList(); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 344 | ArrayNode arrayNode = (ArrayNode) object.path(name); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 345 | arrayNode.forEach(i -> list.add(function.apply(i.asText()))); |
| 346 | return list; |
| 347 | } |
| 348 | |
| 349 | /** |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 350 | * Gets the specified array property as a list of items. |
| 351 | * |
| 352 | * @param name property name |
| 353 | * @param function mapper from string to item |
| 354 | * @param defaultValue default value if property not set |
| 355 | * @param <T> type of item |
| 356 | * @return list of items |
| 357 | */ |
| 358 | protected <T> List<T> getList(String name, Function<String, T> function, List<T> defaultValue) { |
| 359 | List<T> list = Lists.newArrayList(); |
| 360 | JsonNode jsonNode = object.path(name); |
| 361 | if (jsonNode.isMissingNode()) { |
| 362 | return defaultValue; |
| 363 | } |
| 364 | ArrayNode arrayNode = (ArrayNode) jsonNode; |
| 365 | arrayNode.forEach(i -> list.add(function.apply(i.asText()))); |
| 366 | return list; |
| 367 | } |
| 368 | |
| 369 | /** |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 370 | * Sets the specified property as an array of items in a given collection or |
| 371 | * clears it if null is given. |
| 372 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 373 | * @param name propertyName |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 374 | * @param collection collection of items |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 375 | * @param <T> type of items |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 376 | * @return self |
| 377 | */ |
| 378 | protected <T> Config<S> setOrClear(String name, Collection<T> collection) { |
| 379 | if (collection == null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 380 | object.remove(name); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 381 | } else { |
| 382 | ArrayNode arrayNode = mapper.createArrayNode(); |
| 383 | collection.forEach(i -> arrayNode.add(i.toString())); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 384 | object.set(name, arrayNode); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 385 | } |
| 386 | return this; |
| 387 | } |
| 388 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 389 | /** |
| 390 | * Indicates whether only the specified fields are present in the backing JSON. |
| 391 | * |
| 392 | * @param allowedFields allowed field names |
| 393 | * @return true if all allowedFields are present; false otherwise |
| 394 | */ |
| 395 | protected boolean hasOnlyFields(String... allowedFields) { |
| 396 | Set<String> fields = ImmutableSet.copyOf(allowedFields); |
| 397 | return !Iterators.any(object.fieldNames(), f -> !fields.contains(f)); |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Indicates whether the specified field holds a valid MAC address. |
| 402 | * |
| 403 | * @param field JSON field name |
| 404 | * @param presence specifies if field is optional or mandatory |
| 405 | * @return true if valid; false otherwise |
| 406 | * @throws IllegalArgumentException if field is present, but not valid MAC |
| 407 | */ |
| 408 | protected boolean isMacAddress(String field, FieldPresence presence) { |
| 409 | JsonNode node = object.path(field); |
| 410 | return isValid(node, presence, node.isTextual() && |
| 411 | MacAddress.valueOf(node.asText()) != null); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Indicates whether the specified field holds a valid IP address. |
| 416 | * |
| 417 | * @param field JSON field name |
| 418 | * @param presence specifies if field is optional or mandatory |
| 419 | * @return true if valid; false otherwise |
| 420 | * @throws IllegalArgumentException if field is present, but not valid IP |
| 421 | */ |
| 422 | protected boolean isIpAddress(String field, FieldPresence presence) { |
| 423 | JsonNode node = object.path(field); |
| 424 | return isValid(node, presence, node.isTextual() && |
| 425 | IpAddress.valueOf(node.asText()) != null); |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * Indicates whether the specified field holds a valid string value. |
| 430 | * |
| 431 | * @param field JSON field name |
| 432 | * @param presence specifies if field is optional or mandatory |
| 433 | * @param pattern optional regex pattern |
| 434 | * @return true if valid; false otherwise |
| 435 | * @throws IllegalArgumentException if field is present, but not valid MAC |
| 436 | */ |
| 437 | protected boolean isString(String field, FieldPresence presence, String... pattern) { |
| 438 | JsonNode node = object.path(field); |
| 439 | return isValid(node, presence, node.isTextual() && |
| 440 | (pattern.length > 0 && node.asText().matches(pattern[0]) || pattern.length < 1)); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Indicates whether the specified field holds a valid number. |
| 445 | * |
| 446 | * @param field JSON field name |
| 447 | * @param presence specifies if field is optional or mandatory |
| 448 | * @param minMax optional min/max values |
| 449 | * @return true if valid; false otherwise |
| 450 | * @throws IllegalArgumentException if field is present, but not valid |
| 451 | */ |
| 452 | protected boolean isNumber(String field, FieldPresence presence, long... minMax) { |
| 453 | JsonNode node = object.path(field); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 454 | return isValid(node, presence, node.isNumber() && |
| 455 | (minMax.length > 0 && minMax[0] <= node.asLong() || minMax.length < 1) && |
| 456 | (minMax.length > 1 && minMax[1] > node.asLong() || minMax.length < 2)); |
| 457 | } |
| 458 | /** |
| 459 | * Indicates whether the specified field holds a valid number. |
| 460 | * |
| 461 | * @param field JSON field name |
| 462 | * @param presence specifies if field is optional or mandatory |
| 463 | * @param minMax optional min/max values |
| 464 | * @return true if valid; false otherwise |
| 465 | * @throws IllegalArgumentException if field is present, but not valid |
| 466 | */ |
| 467 | protected boolean isNumber(String field, FieldPresence presence, double... minMax) { |
| 468 | JsonNode node = object.path(field); |
| 469 | return isValid(node, presence, node.isNumber() && |
| 470 | (minMax.length > 0 && minMax[0] <= node.asDouble() || minMax.length < 1) && |
| 471 | (minMax.length > 1 && minMax[1] > node.asDouble() || minMax.length < 2)); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * Indicates whether the specified field holds a valid integer. |
| 476 | * |
| 477 | * @param field JSON field name |
| 478 | * @param presence specifies if field is optional or mandatory |
| 479 | * @param minMax optional min/max values |
| 480 | * @return true if valid; false otherwise |
| 481 | * @throws IllegalArgumentException if field is present, but not valid |
| 482 | */ |
| 483 | protected boolean isIntegralNumber(String field, FieldPresence presence, long... minMax) { |
| 484 | JsonNode node = object.path(field); |
| 485 | return isValid(node, presence, node.isIntegralNumber() && |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 486 | (minMax.length > 0 && minMax[0] <= node.asLong() || minMax.length < 1) && |
| 487 | (minMax.length > 1 && minMax[1] > node.asLong() || minMax.length < 2)); |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Indicates whether the specified field holds a valid decimal number. |
| 492 | * |
| 493 | * @param field JSON field name |
| 494 | * @param presence specifies if field is optional or mandatory |
| 495 | * @param minMax optional min/max values |
| 496 | * @return true if valid; false otherwise |
| 497 | * @throws IllegalArgumentException if field is present, but not valid |
| 498 | */ |
| 499 | protected boolean isDecimal(String field, FieldPresence presence, double... minMax) { |
| 500 | JsonNode node = object.path(field); |
| 501 | return isValid(node, presence, (node.isDouble() || node.isFloat()) && |
| 502 | (minMax.length > 0 && minMax[0] <= node.asDouble() || minMax.length < 1) && |
| 503 | (minMax.length > 1 && minMax[1] > node.asDouble() || minMax.length < 2)); |
| 504 | } |
| 505 | |
| 506 | /** |
Thejaswi NK | 6f4ae1c | 2015-12-08 01:15:53 +0530 | [diff] [blame] | 507 | * Indicates whether the specified field holds a valid boolean value. |
| 508 | * |
| 509 | * @param field JSON field name |
| 510 | * @param presence specifies if field is optional or mandatory |
| 511 | * @return true if valid; false otherwise |
| 512 | * @throws IllegalArgumentException if field is present, but not valid |
| 513 | */ |
| 514 | protected boolean isBoolean(String field, FieldPresence presence) { |
| 515 | JsonNode node = object.path(field); |
| 516 | return isValid(node, presence, node.isBoolean()); |
| 517 | } |
| 518 | |
| 519 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 520 | * Indicates whether the node is present and of correct value or not |
| 521 | * mandatory and absent. |
| 522 | * |
| 523 | * @param node JSON node |
| 524 | * @param presence specifies if field is optional or mandatory |
| 525 | * @param correctValue true if the value is correct |
| 526 | * @return true if the field is as expected |
| 527 | */ |
| 528 | private boolean isValid(JsonNode node, FieldPresence presence, boolean correctValue) { |
| 529 | boolean isMandatory = presence == FieldPresence.MANDATORY; |
| 530 | return isMandatory && correctValue || !isMandatory && !node.isNull() || correctValue; |
| 531 | } |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 532 | } |