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