Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 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; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 25 | import org.onlab.packet.IpAddress; |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 26 | import org.onlab.packet.IpPrefix; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 27 | import org.onlab.packet.MacAddress; |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 28 | import org.onlab.packet.TpPort; |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 29 | import org.onosproject.net.ConnectPoint; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 30 | |
| 31 | import java.util.Collection; |
| 32 | import java.util.List; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 33 | import java.util.Set; |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 34 | import java.util.function.Function; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 37 | import static com.google.common.base.Preconditions.checkState; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Base abstraction of a configuration facade for a specific subject. Derived |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 41 | * classes should keep all state in the specified JSON tree as that is the |
| 42 | * only state that will be distributed or persisted; this class is merely |
| 43 | * a facade for interacting with a particular facet of configuration on a |
| 44 | * given subject. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 45 | * |
| 46 | * @param <S> type of subject |
| 47 | */ |
Thomas Vachuska | e2b7e7e | 2015-05-20 11:11:31 -0700 | [diff] [blame] | 48 | @Beta |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 49 | public abstract class Config<S> { |
| 50 | |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 51 | private static final String TRUE_LITERAL = "true"; |
| 52 | private static final String FALSE_LITERAL = "false"; |
| 53 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 54 | protected S subject; |
| 55 | protected String key; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 56 | |
| 57 | protected JsonNode node; |
| 58 | protected ObjectNode object; |
| 59 | protected ArrayNode array; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 60 | protected ObjectMapper mapper; |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 61 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 62 | protected ConfigApplyDelegate delegate; |
| 63 | |
| 64 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 65 | * Indicator of whether a configuration JSON field is required. |
| 66 | */ |
| 67 | public enum FieldPresence { |
| 68 | /** |
| 69 | * Signifies that config field is an optional one. |
| 70 | */ |
| 71 | OPTIONAL, |
| 72 | |
| 73 | /** |
| 74 | * Signifies that config field is mandatory. |
| 75 | */ |
| 76 | MANDATORY |
| 77 | } |
| 78 | |
| 79 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 80 | * Initializes the configuration behaviour with necessary context. |
| 81 | * |
| 82 | * @param subject configuration subject |
| 83 | * @param key configuration key |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 84 | * @param node JSON node where configuration data is stored |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 85 | * @param mapper JSON object mapper |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 86 | * @param delegate delegate context, or null for detached configs. |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 87 | */ |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 88 | public final void init(S subject, String key, JsonNode node, ObjectMapper mapper, |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 89 | ConfigApplyDelegate delegate) { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 90 | this.subject = checkNotNull(subject, "Subject cannot be null"); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 91 | this.key = key; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 92 | this.node = checkNotNull(node, "Node cannot be null"); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 93 | this.object = node instanceof ObjectNode ? (ObjectNode) node : null; |
| 94 | this.array = node instanceof ArrayNode ? (ArrayNode) node : null; |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 95 | this.mapper = checkNotNull(mapper, "Mapper cannot be null"); |
| 96 | this.delegate = delegate; |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 97 | } |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 98 | |
| 99 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 100 | * Indicates whether or not the backing JSON node contains valid data. |
| 101 | * <p> |
| 102 | * Default implementation returns true. |
| 103 | * Subclasses are expected to override this with their own validation. |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 104 | * Implementations are free to throw a RuntimeException if data is invalid. |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 105 | * </p> |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 106 | * |
| 107 | * @return true if the data is valid; false otherwise |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 108 | * @throws RuntimeException if configuration is invalid or completely foobar |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 109 | */ |
| 110 | public boolean isValid() { |
Thomas Vachuska | 3600846 | 2016-01-07 15:38:20 -0800 | [diff] [blame] | 111 | // Derivatives should use the provided set of predicates to test |
| 112 | // validity of their fields, e.g.: |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 113 | // isString(path) |
| 114 | // isBoolean(path) |
| 115 | // isNumber(path, [min, max]) |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 116 | // isIntegralNumber(path, [min, max]) |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 117 | // isDecimal(path, [min, max]) |
| 118 | // isMacAddress(path) |
| 119 | // isIpAddress(path) |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 120 | // isIpPrefix(path) |
| 121 | // isConnectPoint(path) |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 122 | // isTpPort(path) |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 123 | return true; |
| 124 | } |
| 125 | |
| 126 | /** |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 127 | * Returns the specific subject to which this configuration pertains. |
| 128 | * |
| 129 | * @return configuration subject |
| 130 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 131 | public S subject() { |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 132 | return subject; |
| 133 | } |
| 134 | |
| 135 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 136 | * Returns the configuration key. This is primarily aimed for use in |
| 137 | * composite JSON trees in external representations and has no bearing on |
| 138 | * the internal behaviours. |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 139 | * |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 140 | * @return configuration key |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 141 | */ |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 142 | public String key() { |
| 143 | return key; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Returns the JSON node that contains the configuration data. |
| 148 | * |
| 149 | * @return JSON node backing the configuration |
| 150 | */ |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 151 | public JsonNode node() { |
Jonathan Hart | a862548 | 2015-09-08 16:14:56 -0700 | [diff] [blame] | 152 | return node; |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Applies any configuration changes made via this configuration. |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 157 | * <p> |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 158 | * Not effective for detached configs. |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 159 | * </p> |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 160 | */ |
| 161 | public void apply() { |
Charles Chan | 023a898 | 2016-02-04 11:00:41 -0800 | [diff] [blame] | 162 | checkState(delegate != null, "Cannot apply detached config"); |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 163 | delegate.onApply(this); |
| 164 | } |
| 165 | |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 166 | // Miscellaneous helpers for interacting with JSON |
| 167 | |
| 168 | /** |
| 169 | * Gets the specified property as a string. |
| 170 | * |
| 171 | * @param name property name |
| 172 | * @param defaultValue default value if property not set |
| 173 | * @return property value or default value |
| 174 | */ |
| 175 | protected String get(String name, String defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 176 | return object.path(name).asText(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Sets the specified property as a string or clears it if null value given. |
| 181 | * |
| 182 | * @param name property name |
| 183 | * @param value new value or null to clear the property |
| 184 | * @return self |
| 185 | */ |
| 186 | protected Config<S> setOrClear(String name, String value) { |
| 187 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 188 | object.put(name, value); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 189 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 190 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 191 | } |
| 192 | return this; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Gets the specified property as a boolean. |
| 197 | * |
| 198 | * @param name property name |
| 199 | * @param defaultValue default value if property not set |
| 200 | * @return property value or default value |
| 201 | */ |
| 202 | protected boolean get(String name, boolean defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 203 | return object.path(name).asBoolean(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 207 | * Clears the specified property. |
| 208 | * |
| 209 | * @param name property name |
| 210 | * @return self |
| 211 | */ |
| 212 | protected Config<S> clear(String name) { |
| 213 | object.remove(name); |
| 214 | return this; |
| 215 | } |
| 216 | |
| 217 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 218 | * Sets the specified property as a boolean or clears it if null value given. |
| 219 | * |
| 220 | * @param name property name |
| 221 | * @param value new value or null to clear the property |
| 222 | * @return self |
| 223 | */ |
| 224 | protected Config<S> setOrClear(String name, Boolean value) { |
| 225 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 226 | object.put(name, value.booleanValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 227 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 228 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 229 | } |
| 230 | return this; |
| 231 | } |
| 232 | |
| 233 | /** |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 234 | * Gets the specified property as an integer. |
| 235 | * |
| 236 | * @param name property name |
| 237 | * @param defaultValue default value if property not set |
| 238 | * @return property value or default value |
| 239 | */ |
| 240 | protected int get(String name, int defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 241 | return object.path(name).asInt(defaultValue); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Sets the specified property as an integer or clears it if null value given. |
| 246 | * |
| 247 | * @param name property name |
| 248 | * @param value new value or null to clear the property |
| 249 | * @return self |
| 250 | */ |
| 251 | protected Config<S> setOrClear(String name, Integer value) { |
| 252 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 253 | object.put(name, value.intValue()); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 254 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 255 | object.remove(name); |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 256 | } |
| 257 | return this; |
| 258 | } |
| 259 | |
| 260 | /** |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 261 | * Gets the specified property as a long. |
| 262 | * |
| 263 | * @param name property name |
| 264 | * @param defaultValue default value if property not set |
| 265 | * @return property value or default value |
| 266 | */ |
| 267 | protected long get(String name, long defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 268 | return object.path(name).asLong(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Sets the specified property as a long or clears it if null value given. |
| 273 | * |
| 274 | * @param name property name |
| 275 | * @param value new value or null to clear the property |
| 276 | * @return self |
| 277 | */ |
| 278 | protected Config<S> setOrClear(String name, Long value) { |
| 279 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 280 | object.put(name, value.longValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 281 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 282 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 283 | } |
| 284 | return this; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Gets the specified property as a double. |
| 289 | * |
| 290 | * @param name property name |
| 291 | * @param defaultValue default value if property not set |
| 292 | * @return property value or default value |
| 293 | */ |
| 294 | protected double get(String name, double defaultValue) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 295 | return object.path(name).asDouble(defaultValue); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Sets the specified property as a double or clears it if null value given. |
| 300 | * |
| 301 | * @param name property name |
| 302 | * @param value new value or null to clear the property |
| 303 | * @return self |
| 304 | */ |
| 305 | protected Config<S> setOrClear(String name, Double value) { |
| 306 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 307 | object.put(name, value.doubleValue()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 308 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 309 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 310 | } |
| 311 | return this; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Gets the specified property as an enum. |
| 316 | * |
| 317 | * @param name property name |
| 318 | * @param defaultValue default value if property not set |
| 319 | * @param enumClass the enum class |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 320 | * @param <E> type of enum |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 321 | * @return property value or default value |
| 322 | */ |
| 323 | protected <E extends Enum<E>> E get(String name, E defaultValue, Class<E> enumClass) { |
Thomas Vachuska | 2d6c599 | 2016-07-01 12:42:26 +0200 | [diff] [blame] | 324 | if (defaultValue != null) { |
| 325 | Enum.valueOf(enumClass, object.path(name).asText(defaultValue.toString())); |
| 326 | } |
| 327 | |
| 328 | JsonNode node = object.get(name); |
| 329 | return node == null ? null : Enum.valueOf(enumClass, node.asText()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Sets the specified property as a double or clears it if null value given. |
| 334 | * |
| 335 | * @param name property name |
| 336 | * @param value new value or null to clear the property |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 337 | * @param <E> type of enum |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 338 | * @return self |
| 339 | */ |
| 340 | protected <E extends Enum> Config<S> setOrClear(String name, E value) { |
| 341 | if (value != null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 342 | object.put(name, value.toString()); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 343 | } else { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 344 | object.remove(name); |
Thomas Vachuska | 96d55b1 | 2015-05-11 08:52:03 -0700 | [diff] [blame] | 345 | } |
| 346 | return this; |
| 347 | } |
Jonathan Hart | 111b42b | 2015-07-14 13:28:05 -0700 | [diff] [blame] | 348 | |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 349 | /** |
| 350 | * Gets the specified array property as a list of items. |
| 351 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 352 | * @param name property name |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 353 | * @param function mapper from string to item |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 354 | * @param <T> type of item |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 355 | * @return list of items |
| 356 | */ |
| 357 | protected <T> List<T> getList(String name, Function<String, T> function) { |
| 358 | List<T> list = Lists.newArrayList(); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 359 | ArrayNode arrayNode = (ArrayNode) object.path(name); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 360 | arrayNode.forEach(i -> list.add(function.apply(i.asText()))); |
| 361 | return list; |
| 362 | } |
| 363 | |
| 364 | /** |
Naoki Shiota | 399a0b3 | 2015-11-15 20:36:13 -0600 | [diff] [blame] | 365 | * Gets the specified array property as a list of items. |
| 366 | * |
| 367 | * @param name property name |
| 368 | * @param function mapper from string to item |
| 369 | * @param defaultValue default value if property not set |
| 370 | * @param <T> type of item |
| 371 | * @return list of items |
| 372 | */ |
| 373 | protected <T> List<T> getList(String name, Function<String, T> function, List<T> defaultValue) { |
| 374 | List<T> list = Lists.newArrayList(); |
| 375 | JsonNode jsonNode = object.path(name); |
| 376 | if (jsonNode.isMissingNode()) { |
| 377 | return defaultValue; |
| 378 | } |
| 379 | ArrayNode arrayNode = (ArrayNode) jsonNode; |
| 380 | arrayNode.forEach(i -> list.add(function.apply(i.asText()))); |
| 381 | return list; |
| 382 | } |
| 383 | |
| 384 | /** |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 385 | * Sets the specified property as an array of items in a given collection or |
| 386 | * clears it if null is given. |
| 387 | * |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 388 | * @param name propertyName |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 389 | * @param collection collection of items |
Thomas Vachuska | d894b5d | 2015-07-30 11:59:07 -0700 | [diff] [blame] | 390 | * @param <T> type of items |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 391 | * @return self |
| 392 | */ |
| 393 | protected <T> Config<S> setOrClear(String name, Collection<T> collection) { |
| 394 | if (collection == null) { |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 395 | object.remove(name); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 396 | } else { |
| 397 | ArrayNode arrayNode = mapper.createArrayNode(); |
| 398 | collection.forEach(i -> arrayNode.add(i.toString())); |
Thomas Vachuska | 0a400ea | 2015-09-04 11:25:03 -0700 | [diff] [blame] | 399 | object.set(name, arrayNode); |
Brian O'Connor | ce2d8b5 | 2015-07-29 16:24:13 -0700 | [diff] [blame] | 400 | } |
| 401 | return this; |
| 402 | } |
| 403 | |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 404 | /** |
| 405 | * Indicates whether only the specified fields are present in the backing JSON. |
| 406 | * |
| 407 | * @param allowedFields allowed field names |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 408 | * @return true if only allowedFields are present; false otherwise |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 409 | */ |
| 410 | protected boolean hasOnlyFields(String... allowedFields) { |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 411 | return hasOnlyFields(object, allowedFields); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Indicates whether only the specified fields are present in a particular |
| 416 | * JSON object. |
| 417 | * |
| 418 | * @param node node whose fields to check |
| 419 | * @param allowedFields allowed field names |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 420 | * @return true if only allowedFields are present; false otherwise |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 421 | */ |
| 422 | protected boolean hasOnlyFields(ObjectNode node, String... allowedFields) { |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 423 | Set<String> fields = ImmutableSet.copyOf(allowedFields); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 424 | node.fieldNames().forEachRemaining(f -> { |
| 425 | if (!fields.contains(f)) { |
| 426 | throw new InvalidFieldException(f, "Field is not allowed"); |
| 427 | } |
| 428 | }); |
| 429 | return true; |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | /** |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 433 | * Indicates whether all specified fields are present in the backing JSON. |
| 434 | * |
| 435 | * @param mandatoryFields mandatory field names |
| 436 | * @return true if all mandatory fields are present; false otherwise |
| 437 | */ |
| 438 | protected boolean hasFields(String... mandatoryFields) { |
| 439 | return hasFields(object, mandatoryFields); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Indicates whether all specified fields are present in a particular |
| 444 | * JSON object. |
| 445 | * |
| 446 | * @param node node whose fields to check |
| 447 | * @param mandatoryFields mandatory field names |
| 448 | * @return true if all mandatory fields are present; false otherwise |
| 449 | */ |
| 450 | protected boolean hasFields(ObjectNode node, String... mandatoryFields) { |
| 451 | Set<String> fields = ImmutableSet.copyOf(mandatoryFields); |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 452 | fields.forEach(f -> { |
| 453 | if (node.path(f).isMissingNode()) { |
| 454 | throw new InvalidFieldException(f, "Mandatory field is not present"); |
| 455 | } |
| 456 | }); |
| 457 | return true; |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 461 | * Indicates whether the specified field holds a valid MAC address. |
| 462 | * |
| 463 | * @param field JSON field name |
| 464 | * @param presence specifies if field is optional or mandatory |
| 465 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 466 | * @throws InvalidFieldException if the field is present but not valid |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 467 | */ |
| 468 | protected boolean isMacAddress(String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 469 | return isMacAddress(object, field, presence); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | /** |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 473 | * Indicates whether the specified field of a particular node holds a valid |
| 474 | * MAC address. |
| 475 | * |
| 476 | * @param objectNode JSON node |
| 477 | * @param field JSON field name |
| 478 | * @param presence specifies if field is optional or mandatory |
| 479 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 480 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 481 | */ |
| 482 | protected boolean isMacAddress(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 483 | return isValid(objectNode, field, presence, n -> { |
| 484 | MacAddress.valueOf(n.asText()); |
| 485 | return true; |
| 486 | }); |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /** |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 490 | * Indicates whether the specified field holds a valid IP address. |
| 491 | * |
| 492 | * @param field JSON field name |
| 493 | * @param presence specifies if field is optional or mandatory |
| 494 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 495 | * @throws InvalidFieldException if the field is present but not valid |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 496 | */ |
| 497 | protected boolean isIpAddress(String field, FieldPresence presence) { |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 498 | return isIpAddress(object, field, presence); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Indicates whether the specified field of a particular node holds a valid |
| 503 | * IP address. |
| 504 | * |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 505 | * @param objectNode node from whom to access the field |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 506 | * @param field JSON field name |
| 507 | * @param presence specifies if field is optional or mandatory |
| 508 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 509 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 3a8896b | 2016-02-16 13:06:26 -0800 | [diff] [blame] | 510 | */ |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 511 | protected boolean isIpAddress(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 512 | return isValid(objectNode, field, presence, n -> { |
| 513 | IpAddress.valueOf(n.asText()); |
| 514 | return true; |
| 515 | }); |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | /** |
| 519 | * Indicates whether the specified field holds a valid IP prefix. |
| 520 | * |
| 521 | * @param field JSON field name |
| 522 | * @param presence specifies if field is optional or mandatory |
| 523 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 524 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 525 | */ |
| 526 | protected boolean isIpPrefix(String field, FieldPresence presence) { |
| 527 | return isIpPrefix(object, field, presence); |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Indicates whether the specified field of a particular node holds a valid |
| 532 | * IP prefix. |
| 533 | * |
| 534 | * @param objectNode node from whom to access the field |
| 535 | * @param field JSON field name |
| 536 | * @param presence specifies if field is optional or mandatory |
| 537 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 538 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 539 | */ |
| 540 | protected boolean isIpPrefix(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 541 | return isValid(objectNode, field, presence, n -> { |
| 542 | IpPrefix.valueOf(n.asText()); |
| 543 | return true; |
| 544 | }); |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | /** |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 548 | * Indicates whether the specified field holds a valid transport layer port. |
| 549 | * |
| 550 | * @param field JSON field name |
| 551 | * @param presence specifies if field is optional or mandatory |
| 552 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 553 | * @throws InvalidFieldException if the field is present but not valid |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 554 | */ |
| 555 | protected boolean isTpPort(String field, FieldPresence presence) { |
| 556 | return isTpPort(object, field, presence); |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * Indicates whether the specified field of a particular node holds a valid |
| 561 | * transport layer port. |
| 562 | * |
| 563 | * @param objectNode node from whom to access the field |
| 564 | * @param field JSON field name |
| 565 | * @param presence specifies if field is optional or mandatory |
| 566 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 567 | * @throws InvalidFieldException if the field is present but not valid |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 568 | */ |
| 569 | protected boolean isTpPort(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 570 | return isValid(objectNode, field, presence, n -> { |
| 571 | TpPort.tpPort(n.asInt()); |
| 572 | return true; |
| 573 | }); |
Hyunsun Moon | 61b73e9 | 2016-05-10 18:05:57 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /** |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 577 | * Indicates whether the specified field holds a valid connect point string. |
| 578 | * |
| 579 | * @param field JSON field name |
| 580 | * @param presence specifies if field is optional or mandatory |
| 581 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 582 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 583 | */ |
| 584 | protected boolean isConnectPoint(String field, FieldPresence presence) { |
| 585 | return isConnectPoint(object, field, presence); |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Indicates whether the specified field of a particular node holds a valid |
| 590 | * connect point string. |
| 591 | * |
| 592 | * @param objectNode JSON node |
| 593 | * @param field JSON field name |
| 594 | * @param presence specifies if field is optional or mandatory |
| 595 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 596 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 597 | */ |
| 598 | protected boolean isConnectPoint(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 599 | return isValid(objectNode, field, presence, n -> { |
| 600 | ConnectPoint.deviceConnectPoint(n.asText()); |
| 601 | return true; |
| 602 | }); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | /** |
| 606 | * Indicates whether the specified field holds a valid string value. |
| 607 | * |
| 608 | * @param field JSON field name |
| 609 | * @param presence specifies if field is optional or mandatory |
| 610 | * @param pattern optional regex pattern |
| 611 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 612 | * @throws InvalidFieldException if the field is present but not valid |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 613 | */ |
| 614 | protected boolean isString(String field, FieldPresence presence, String... pattern) { |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 615 | return isString(object, field, presence, pattern); |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * Indicates whether the specified field on a particular node holds a valid |
| 620 | * string value. |
| 621 | * |
| 622 | * @param objectNode JSON node |
| 623 | * @param field JSON field name |
| 624 | * @param presence specifies if field is optional or mandatory |
| 625 | * @param pattern optional regex pattern |
| 626 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 627 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 628 | */ |
| 629 | protected boolean isString(ObjectNode objectNode, String field, |
| 630 | FieldPresence presence, String... pattern) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 631 | return isValid(objectNode, field, presence, (node) -> { |
| 632 | if (!(node.isTextual() && |
| 633 | (pattern.length > 0 && node.asText().matches(pattern[0]) || pattern.length < 1))) { |
| 634 | fail("Invalid string value"); |
| 635 | } |
| 636 | return true; |
| 637 | }); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | /** |
| 641 | * Indicates whether the specified field holds a valid number. |
| 642 | * |
| 643 | * @param field JSON field name |
| 644 | * @param presence specifies if field is optional or mandatory |
| 645 | * @param minMax optional min/max values |
| 646 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 647 | * @throws InvalidFieldException if the field is present but not valid |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 648 | */ |
| 649 | protected boolean isNumber(String field, FieldPresence presence, long... minMax) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 650 | return isNumber(object, field, presence, minMax); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 651 | } |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 652 | |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 653 | /** |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 654 | * Indicates whether the specified field of a particular node holds a |
| 655 | * valid number. |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 656 | * |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 657 | * @param objectNode JSON object |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 658 | * @param field JSON field name |
| 659 | * @param presence specifies if field is optional or mandatory |
| 660 | * @param minMax optional min/max values |
| 661 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 662 | * @throws InvalidFieldException if the field is present but not valid |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 663 | */ |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 664 | protected boolean isNumber(ObjectNode objectNode, String field, |
| 665 | FieldPresence presence, long... minMax) { |
| 666 | return isValid(objectNode, field, presence, n -> { |
| 667 | long number = (n.isNumber()) ? n.asLong() : Long.parseLong(n.asText()); |
| 668 | if (minMax.length > 1) { |
| 669 | verifyRange(number, minMax[0], minMax[1]); |
| 670 | } else if (minMax.length > 0) { |
| 671 | verifyRange(number, minMax[0]); |
| 672 | } |
| 673 | return true; |
| 674 | }); |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | /** |
| 678 | * Indicates whether the specified field holds a valid integer. |
| 679 | * |
| 680 | * @param field JSON field name |
| 681 | * @param presence specifies if field is optional or mandatory |
| 682 | * @param minMax optional min/max values |
| 683 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 684 | * @throws InvalidFieldException if the field is present but not valid |
HIGUCHI Yuta | 1d7c9cb | 2016-01-20 18:22:36 -0800 | [diff] [blame] | 685 | */ |
| 686 | protected boolean isIntegralNumber(String field, FieldPresence presence, long... minMax) { |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 687 | return isIntegralNumber(object, field, presence, minMax); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Indicates whether the specified field of a particular node holds a valid |
| 692 | * integer. |
| 693 | * |
| 694 | * @param objectNode JSON node |
| 695 | * @param field JSON field name |
| 696 | * @param presence specifies if field is optional or mandatory |
| 697 | * @param minMax optional min/max values |
| 698 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 699 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 700 | */ |
| 701 | protected boolean isIntegralNumber(ObjectNode objectNode, String field, |
| 702 | FieldPresence presence, long... minMax) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 703 | return isValid(objectNode, field, presence, n -> { |
| 704 | long number = (n.isIntegralNumber()) ? n.asLong() : Long.parseLong(n.asText()); |
| 705 | if (minMax.length > 1) { |
| 706 | verifyRange(number, minMax[0], minMax[1]); |
| 707 | } else if (minMax.length > 0) { |
| 708 | verifyRange(number, minMax[0]); |
| 709 | } |
| 710 | return true; |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 711 | }); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | /** |
| 715 | * Indicates whether the specified field holds a valid decimal number. |
| 716 | * |
| 717 | * @param field JSON field name |
| 718 | * @param presence specifies if field is optional or mandatory |
| 719 | * @param minMax optional min/max values |
| 720 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 721 | * @throws InvalidFieldException if the field is present but not valid |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 722 | */ |
| 723 | protected boolean isDecimal(String field, FieldPresence presence, double... minMax) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 724 | return isDecimal(object, field, presence, minMax); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Indicates whether the specified field of a particular node holds a valid |
| 729 | * decimal number. |
| 730 | * |
| 731 | * @param objectNode JSON node |
| 732 | * @param field JSON field name |
| 733 | * @param presence specifies if field is optional or mandatory |
| 734 | * @param minMax optional min/max values |
| 735 | * @return true if valid; false otherwise |
| 736 | * @throws InvalidFieldException if the field is present but not valid |
| 737 | */ |
| 738 | protected boolean isDecimal(ObjectNode objectNode, String field, |
| 739 | FieldPresence presence, double... minMax) { |
| 740 | return isValid(objectNode, field, presence, n -> { |
| 741 | double number = (n.isDouble()) ? n.asDouble() : Double.parseDouble(n.asText()); |
| 742 | if (minMax.length > 1) { |
| 743 | verifyRange(number, minMax[0], minMax[1]); |
| 744 | } else if (minMax.length > 0) { |
| 745 | verifyRange(number, minMax[0]); |
| 746 | } |
| 747 | return true; |
| 748 | }); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | /** |
Thejaswi NK | 6f4ae1c | 2015-12-08 01:15:53 +0530 | [diff] [blame] | 752 | * Indicates whether the specified field holds a valid boolean value. |
| 753 | * |
| 754 | * @param field JSON field name |
| 755 | * @param presence specifies if field is optional or mandatory |
| 756 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 757 | * @throws InvalidFieldException if the field is present but not valid |
Thejaswi NK | 6f4ae1c | 2015-12-08 01:15:53 +0530 | [diff] [blame] | 758 | */ |
| 759 | protected boolean isBoolean(String field, FieldPresence presence) { |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 760 | return isBoolean(object, field, presence); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * Indicates whether the specified field of a particular node holds a valid |
| 765 | * boolean value. |
| 766 | * |
| 767 | * @param objectNode JSON object node |
| 768 | * @param field JSON field name |
| 769 | * @param presence specifies if field is optional or mandatory |
| 770 | * @return true if valid; false otherwise |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 771 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 772 | */ |
| 773 | protected boolean isBoolean(ObjectNode objectNode, String field, FieldPresence presence) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 774 | return isValid(objectNode, field, presence, n -> { |
| 775 | if (!(n.isBoolean() || (n.isTextual() && isBooleanString(n.asText())))) { |
| 776 | fail("Field is not a boolean value"); |
| 777 | } |
| 778 | return true; |
| 779 | }); |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /** |
| 783 | * Indicates whether a string holds a boolean literal value. |
| 784 | * |
| 785 | * @param str string to test |
| 786 | * @return true if the string contains "true" or "false" (case insensitive), |
| 787 | * otherwise false |
| 788 | */ |
| 789 | private boolean isBooleanString(String str) { |
| 790 | return str.equalsIgnoreCase(TRUE_LITERAL) || str.equalsIgnoreCase(FALSE_LITERAL); |
Thejaswi NK | 6f4ae1c | 2015-12-08 01:15:53 +0530 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | /** |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 794 | * Indicates whether a field in the node is present and of correct value or |
| 795 | * not mandatory and absent. |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 796 | * |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 797 | * @param objectNode JSON object node containing field to validate |
| 798 | * @param field name of field to validate |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 799 | * @param presence specified if field is optional or mandatory |
| 800 | * @param validationFunction function which can be used to verify if the |
| 801 | * node has the correct value |
| 802 | * @return true if the field is as expected |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 803 | * @throws InvalidFieldException if the field is present but not valid |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 804 | */ |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 805 | private boolean isValid(ObjectNode objectNode, String field, FieldPresence presence, |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 806 | Function<JsonNode, Boolean> validationFunction) { |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 807 | JsonNode node = objectNode.path(field); |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 808 | boolean isMandatory = presence == FieldPresence.MANDATORY; |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 809 | if (isMandatory && node.isMissingNode()) { |
| 810 | throw new InvalidFieldException(field, "Mandatory field not present"); |
Jonathan Hart | 9eb45bb | 2016-02-12 10:59:11 -0800 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | if (!isMandatory && (node.isNull() || node.isMissingNode())) { |
| 814 | return true; |
| 815 | } |
| 816 | |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 817 | try { |
| 818 | if (validationFunction.apply(node)) { |
| 819 | return true; |
| 820 | } else { |
| 821 | throw new InvalidFieldException(field, "Validation error"); |
| 822 | } |
| 823 | } catch (IllegalArgumentException e) { |
| 824 | throw new InvalidFieldException(field, e); |
| 825 | } |
Thomas Vachuska | ce0bbb3 | 2015-11-18 16:56:10 -0800 | [diff] [blame] | 826 | } |
Jonathan Hart | 54b83e8 | 2016-03-26 20:37:20 -0700 | [diff] [blame] | 827 | |
| 828 | private static void fail(String message) { |
| 829 | throw new IllegalArgumentException(message); |
| 830 | } |
| 831 | |
| 832 | private static <N extends Comparable> void verifyRange(N num, N min) { |
| 833 | if (num.compareTo(min) < 0) { |
| 834 | fail("Field must be greater than " + min); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | private static <N extends Comparable> void verifyRange(N num, N min, N max) { |
| 839 | verifyRange(num, min); |
| 840 | |
| 841 | if (num.compareTo(max) > 0) { |
| 842 | fail("Field must be less than " + max); |
| 843 | } |
| 844 | } |
| 845 | |
Thomas Vachuska | f0e1fae | 2015-04-24 00:51:51 -0700 | [diff] [blame] | 846 | } |