jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.workflow.api; |
| 17 | |
| 18 | |
| 19 | import com.fasterxml.jackson.core.JsonPointer; |
| 20 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 24 | import com.fasterxml.jackson.databind.node.BooleanNode; |
| 25 | import com.fasterxml.jackson.databind.node.IntNode; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 26 | import com.fasterxml.jackson.databind.node.JsonNodeFactory; |
| 27 | import com.fasterxml.jackson.databind.node.JsonNodeType; |
| 28 | import com.fasterxml.jackson.databind.node.MissingNode; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 29 | import com.fasterxml.jackson.databind.node.NumericNode; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 30 | import com.fasterxml.jackson.databind.node.ObjectNode; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 31 | import com.fasterxml.jackson.databind.node.TextNode; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 32 | import com.google.common.base.MoreObjects; |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 33 | import org.slf4j.Logger; |
| 34 | import org.slf4j.LoggerFactory; |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 35 | |
| 36 | import java.util.Objects; |
| 37 | |
| 38 | /** |
| 39 | * Class for json data model tree. |
| 40 | */ |
| 41 | public final class JsonDataModelTree implements DataModelTree { |
| 42 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 43 | private static final Logger log = LoggerFactory.getLogger(JsonDataModelTree.class); |
| 44 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 45 | /** |
| 46 | * Root node of json data model tree. |
| 47 | */ |
| 48 | private JsonNode root; |
| 49 | |
| 50 | /** |
| 51 | * Constructor of JsonDataModelTree. |
| 52 | */ |
| 53 | public JsonDataModelTree() { |
| 54 | this.root = JsonNodeFactory.instance.objectNode(); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Constructor of JsonDataModelTree. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 59 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 60 | * @param root root node of json data model tree |
| 61 | */ |
| 62 | public JsonDataModelTree(JsonNode root) { |
| 63 | this.root = root; |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public DataModelTree subtree(String path) { |
| 68 | JsonNode node = root.at(path); |
| 69 | if (Objects.isNull(node) || node.isMissingNode()) { |
| 70 | return null; |
| 71 | } |
| 72 | return new JsonDataModelTree(node); |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public void attach(String path, DataModelTree tree) throws WorkflowException { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 77 | |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 78 | if (root == null || root instanceof MissingNode) { |
| 79 | throw new WorkflowException("Invalid root node"); |
| 80 | } |
| 81 | |
| 82 | JsonPointer ptr = JsonPointer.compile(path); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 83 | |
| 84 | if (!(tree instanceof JsonDataModelTree)) { |
| 85 | throw new WorkflowException("Invalid subTree(" + tree + ")"); |
| 86 | } |
| 87 | JsonNode attachingNode = ((JsonDataModelTree) tree).root(); |
| 88 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 89 | attach(ptr, attachingNode); |
| 90 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 91 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 92 | private void attach(JsonPointer ptr, JsonNode attachingNode) throws WorkflowException { |
| 93 | |
| 94 | JsonNode node = root.at(ptr); |
| 95 | if (!(node instanceof MissingNode)) { |
| 96 | throw new WorkflowException("Path(" + ptr + ") has already subtree(" + node + ")"); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 97 | } |
| 98 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 99 | if (ptr.last().getMatchingIndex() != -1) { |
| 100 | |
| 101 | alloc(ptr.head(), Nodetype.ARRAY); |
| 102 | JsonNode parentNode = root.at(ptr.head()); |
| 103 | if (!parentNode.isArray()) { |
| 104 | throw new WorkflowException("Invalid parentNode type(" + parentNode.getNodeType() + " != Array)"); |
| 105 | } |
| 106 | int index = ptr.last().getMatchingIndex(); |
| 107 | ((ArrayNode) parentNode).insert(index, attachingNode); |
| 108 | |
| 109 | } else if (ptr.last().getMatchingProperty() != null) { |
| 110 | |
| 111 | alloc(ptr.head(), Nodetype.MAP); |
| 112 | JsonNode parentNode = root.at(ptr.head()); |
| 113 | if (!parentNode.isObject()) { |
| 114 | throw new WorkflowException("Invalid parentNode type(" + parentNode.getNodeType() + " != Object)"); |
| 115 | } |
| 116 | String key = ptr.last().getMatchingProperty(); |
| 117 | ((ObjectNode) parentNode).put(key, attachingNode); |
| 118 | |
| 119 | } else { |
| 120 | throw new WorkflowException("Invalid path(" + ptr + ")"); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void remove(String path) throws WorkflowException { |
| 126 | JsonPointer ptr = JsonPointer.compile(path); |
| 127 | remove(ptr); |
| 128 | } |
| 129 | |
| 130 | private void remove(JsonPointer ptr) throws WorkflowException { |
| 131 | |
| 132 | JsonNode node = root.at(ptr); |
| 133 | if (node instanceof MissingNode) { |
| 134 | log.warn("{} does not have valid node", ptr); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | if (ptr.last().getMatchingIndex() != -1) { |
| 139 | |
| 140 | JsonNode parentNode = root.at(ptr.head()); |
| 141 | if (!parentNode.isArray()) { |
| 142 | throw new WorkflowException("Invalid parentNode type(" + parentNode.getNodeType() + " != Array)"); |
| 143 | } |
| 144 | int index = ptr.last().getMatchingIndex(); |
| 145 | ((ArrayNode) parentNode).remove(index); |
| 146 | |
| 147 | } else if (ptr.last().getMatchingProperty() != null) { |
| 148 | |
| 149 | JsonNode parentNode = root.at(ptr.head()); |
| 150 | if (!parentNode.isObject()) { |
| 151 | throw new WorkflowException("Invalid parentNode type(" + parentNode.getNodeType() + " != Object)"); |
| 152 | } |
| 153 | String key = ptr.last().getMatchingProperty(); |
| 154 | ((ObjectNode) parentNode).remove(key); |
| 155 | |
| 156 | } else { |
| 157 | throw new WorkflowException("Invalid path(" + ptr + ")"); |
| 158 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | @Override |
| 162 | public JsonDataModelTree alloc(String path, Nodetype leaftype) throws WorkflowException { |
| 163 | if (root == null || root instanceof MissingNode) { |
| 164 | throw new WorkflowException("Invalid root node"); |
| 165 | } |
| 166 | |
| 167 | JsonPointer ptr = JsonPointer.compile(path); |
| 168 | return alloc(ptr, leaftype); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Allocates json data model tree on json pointer path with specific leaf type. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 173 | * |
| 174 | * @param ptr json pointer to allocate |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 175 | * @param leaftype type of leaf node |
| 176 | * @return json data model tree |
| 177 | * @throws WorkflowException workflow exception |
| 178 | */ |
| 179 | private JsonDataModelTree alloc(JsonPointer ptr, Nodetype leaftype) throws WorkflowException { |
| 180 | if (root == null || root instanceof MissingNode) { |
| 181 | throw new WorkflowException("Invalid root node"); |
| 182 | } |
| 183 | |
| 184 | switch (leaftype) { |
| 185 | case MAP: |
| 186 | alloc(root, ptr, JsonNodeType.OBJECT); |
| 187 | break; |
| 188 | case ARRAY: |
| 189 | alloc(root, ptr, JsonNodeType.ARRAY); |
| 190 | break; |
| 191 | default: |
| 192 | throw new WorkflowException("Not supported leaftype(" + leaftype + ")"); |
| 193 | } |
| 194 | return this; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Gets root json node. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 199 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 200 | * @return root json node |
| 201 | * @throws WorkflowException workflow exception |
| 202 | */ |
| 203 | public JsonNode root() throws WorkflowException { |
| 204 | return nodeAt(""); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Gets root json node as ObjectNode (MAP type). |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 209 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 210 | * @return root json node as ObjectNode |
| 211 | * @throws WorkflowException workflow exception |
| 212 | */ |
| 213 | public ObjectNode rootObject() throws WorkflowException { |
| 214 | return objectAt(""); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Gets root json node as ArrayNode (Array type). |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 219 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 220 | * @return root json node as ArrayNode |
| 221 | * @throws WorkflowException workflow exception |
| 222 | */ |
| 223 | public ArrayNode rootArray() throws WorkflowException { |
| 224 | return arrayAt(""); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Gets json node on specific path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 229 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 230 | * @param path path of json node |
| 231 | * @return json node on specific path |
| 232 | * @throws WorkflowException workflow exception |
| 233 | */ |
| 234 | public JsonNode nodeAt(String path) throws WorkflowException { |
| 235 | JsonPointer ptr = JsonPointer.compile(path); |
| 236 | return nodeAt(ptr); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Gets json node on specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 241 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 242 | * @param ptr json pointer |
| 243 | * @return json node on specific json pointer. |
| 244 | * @throws WorkflowException workflow exception |
| 245 | */ |
| 246 | public JsonNode nodeAt(JsonPointer ptr) throws WorkflowException { |
| 247 | if (root == null || root instanceof MissingNode) { |
| 248 | throw new WorkflowException("Invalid root node"); |
| 249 | } |
| 250 | JsonNode node = root.at(ptr); |
| 251 | return node; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Gets json node on specific path as ObjectNode. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 256 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 257 | * @param path path of json node |
| 258 | * @return ObjectNode type json node on specific path |
| 259 | * @throws WorkflowException workflow exception |
| 260 | */ |
| 261 | public ObjectNode objectAt(String path) throws WorkflowException { |
| 262 | JsonPointer ptr = JsonPointer.compile(path); |
| 263 | return objectAt(ptr); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Gets json node on specific json pointer as ObjectNode. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 268 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 269 | * @param ptr json pointer |
| 270 | * @return ObjectNode type json node on specific json pointer. |
| 271 | * @throws WorkflowException workflow exception |
| 272 | */ |
| 273 | public ObjectNode objectAt(JsonPointer ptr) throws WorkflowException { |
| 274 | if (root == null || root instanceof MissingNode) { |
| 275 | throw new WorkflowException("Invalid root node"); |
| 276 | } |
| 277 | JsonNode node = root.at(ptr); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 278 | if (node instanceof MissingNode) { |
| 279 | return null; |
| 280 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 281 | if (!(node instanceof ObjectNode)) { |
| 282 | throw new WorkflowException("Invalid node(" + node + ") at " + ptr); |
| 283 | } |
| 284 | return (ObjectNode) node; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Gets json node on specific path as ArrayNode. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 289 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 290 | * @param path path of json node |
| 291 | * @return ArrayNode type json node on specific path |
| 292 | * @throws WorkflowException workflow exception |
| 293 | */ |
| 294 | public ArrayNode arrayAt(String path) throws WorkflowException { |
| 295 | JsonPointer ptr = JsonPointer.compile(path); |
| 296 | return arrayAt(ptr); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Gets json node on specific json pointer as ArrayNode. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 301 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 302 | * @param ptr json pointer |
| 303 | * @return ArrayNode type json node on specific json pointer. |
| 304 | * @throws WorkflowException workflow exception |
| 305 | */ |
| 306 | public ArrayNode arrayAt(JsonPointer ptr) throws WorkflowException { |
| 307 | if (root == null || root instanceof MissingNode) { |
| 308 | throw new WorkflowException("Invalid root node"); |
| 309 | } |
| 310 | JsonNode node = root.at(ptr); |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 311 | if (node instanceof MissingNode) { |
| 312 | return null; |
| 313 | } |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 314 | if (!(node instanceof ArrayNode)) { |
| 315 | throw new WorkflowException("Invalid node(" + node + ") at " + ptr); |
| 316 | } |
| 317 | return (ArrayNode) node; |
| 318 | } |
| 319 | |
| 320 | /** |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 321 | * Gets text node on specific path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 322 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 323 | * @param path path of json node |
| 324 | * @return text on specific path |
| 325 | * @throws WorkflowException workflow exception |
| 326 | */ |
| 327 | public String textAt(String path) throws WorkflowException { |
| 328 | JsonPointer ptr = JsonPointer.compile(path); |
| 329 | return textAt(ptr); |
| 330 | } |
| 331 | |
| 332 | /** |
| 333 | * Gets text on specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 334 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 335 | * @param ptr json pointer |
| 336 | * @return text on specific json pointer |
| 337 | * @throws WorkflowException workflow exception |
| 338 | */ |
| 339 | public String textAt(JsonPointer ptr) throws WorkflowException { |
| 340 | if (root == null || root instanceof MissingNode) { |
| 341 | throw new WorkflowException("Invalid root node"); |
| 342 | } |
| 343 | JsonNode node = root.at(ptr); |
| 344 | if (node instanceof MissingNode) { |
| 345 | return null; |
| 346 | } |
| 347 | if (!(node instanceof TextNode)) { |
| 348 | throw new WorkflowException("Invalid node(" + node + ") at " + ptr); |
| 349 | } |
| 350 | return ((TextNode) node).asText(); |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * Gets integer node on specific path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 355 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 356 | * @param path path of json node |
| 357 | * @return integer on specific path |
| 358 | * @throws WorkflowException workflow exception |
| 359 | */ |
| 360 | public Integer intAt(String path) throws WorkflowException { |
| 361 | JsonPointer ptr = JsonPointer.compile(path); |
| 362 | return intAt(ptr); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * Gets integer on specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 367 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 368 | * @param ptr json pointer |
| 369 | * @return integer on specific json pointer |
| 370 | * @throws WorkflowException workflow exception |
| 371 | */ |
| 372 | public Integer intAt(JsonPointer ptr) throws WorkflowException { |
| 373 | if (root == null || root instanceof MissingNode) { |
| 374 | throw new WorkflowException("Invalid root node"); |
| 375 | } |
| 376 | JsonNode node = root.at(ptr); |
| 377 | if (node instanceof MissingNode) { |
| 378 | return null; |
| 379 | } |
| 380 | if (!(node instanceof NumericNode)) { |
| 381 | throw new WorkflowException("Invalid node(" + node + ") at " + ptr); |
| 382 | } |
| 383 | return ((NumericNode) node).asInt(); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Gets boolean on specific path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 388 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 389 | * @param path path of json node |
| 390 | * @return boolean on specific path |
| 391 | * @throws WorkflowException workflow exception |
| 392 | */ |
| 393 | public Boolean booleanAt(String path) throws WorkflowException { |
| 394 | JsonPointer ptr = JsonPointer.compile(path); |
| 395 | return booleanAt(ptr); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * Gets boolean on specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 400 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 401 | * @param ptr json pointer |
| 402 | * @return boolean on specific json pointer |
| 403 | * @throws WorkflowException workflow exception |
| 404 | */ |
| 405 | public Boolean booleanAt(JsonPointer ptr) throws WorkflowException { |
| 406 | if (root == null || root instanceof MissingNode) { |
| 407 | throw new WorkflowException("Invalid root node"); |
| 408 | } |
| 409 | JsonNode node = root.at(ptr); |
| 410 | if (node instanceof MissingNode) { |
| 411 | return null; |
| 412 | } |
| 413 | if (!(node instanceof BooleanNode)) { |
| 414 | throw new WorkflowException("Invalid node(" + node + ") at " + ptr); |
| 415 | } |
| 416 | return ((BooleanNode) node).asBoolean(); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Sets text on specific json path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 421 | * |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 422 | * @param path json path |
| 423 | * @param text text to set |
| 424 | * @throws WorkflowException workflow exception |
| 425 | */ |
| 426 | public void setAt(String path, String text) throws WorkflowException { |
| 427 | JsonPointer ptr = JsonPointer.compile(path); |
| 428 | setAt(ptr, text); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Sets text on the specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 433 | * |
| 434 | * @param ptr json pointer |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 435 | * @param text text to set |
| 436 | * @throws WorkflowException workflow exception |
| 437 | */ |
| 438 | public void setAt(JsonPointer ptr, String text) throws WorkflowException { |
| 439 | TextNode textNode = TextNode.valueOf(text); |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 440 | |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 441 | attach(ptr, textNode); |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Sets boolean on specific json path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 446 | * |
| 447 | * @param path json path |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 448 | * @param isTrue boolean to set |
| 449 | * @throws WorkflowException workflow exception |
| 450 | */ |
| 451 | public void setAt(String path, Boolean isTrue) throws WorkflowException { |
| 452 | JsonPointer ptr = JsonPointer.compile(path); |
| 453 | setAt(ptr, isTrue); |
| 454 | } |
| 455 | |
| 456 | /** |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 457 | * Sets text on the specific json pointer. |
| 458 | * |
| 459 | * @param ptr json pointer |
| 460 | * @param jsonNode jsonNode to set |
| 461 | * @throws WorkflowException workflow exception |
| 462 | */ |
| 463 | public void setAt(JsonPointer ptr, JsonNode jsonNode) throws WorkflowException { |
| 464 | JsonNode node = jsonNode; |
| 465 | attach(ptr, node); |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Sets boolean on specific json path. |
| 470 | * |
| 471 | * @param path json path |
| 472 | * @param jsonNode jsonNode to set |
| 473 | * @throws WorkflowException workflow exception |
| 474 | */ |
| 475 | public void setAt(String path, JsonNode jsonNode) throws WorkflowException { |
| 476 | JsonPointer ptr = JsonPointer.compile(path); |
| 477 | setAt(ptr, jsonNode); |
| 478 | } |
| 479 | |
| 480 | |
| 481 | /** |
| 482 | * Sets text on the specific json pointer. |
| 483 | * |
| 484 | * @param ptr json pointer |
| 485 | * @param arrayNode arrayNode to set |
| 486 | * @throws WorkflowException workflow exception |
| 487 | */ |
| 488 | public void setAt(JsonPointer ptr, ArrayNode arrayNode) throws WorkflowException { |
| 489 | ArrayNode node = arrayNode; |
| 490 | attach(ptr, node); |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * Sets boolean on specific json path. |
| 495 | * |
| 496 | * @param path json path |
| 497 | * @param arrayNode arrayNode to set |
| 498 | * @throws WorkflowException workflow exception |
| 499 | */ |
| 500 | public void setAt(String path, ArrayNode arrayNode) throws WorkflowException { |
| 501 | JsonPointer ptr = JsonPointer.compile(path); |
| 502 | setAt(ptr, arrayNode); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Sets text on the specific json pointer. |
| 507 | * |
| 508 | * @param ptr json pointer |
| 509 | * @param objectNode objectNode to set |
| 510 | * @throws WorkflowException workflow exception |
| 511 | */ |
| 512 | public void setAt(JsonPointer ptr, ObjectNode objectNode) throws WorkflowException { |
| 513 | ObjectNode node = objectNode; |
| 514 | attach(ptr, node); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * Sets boolean on specific json path. |
| 519 | * |
| 520 | * @param path json path |
| 521 | * @param objectNode objectNode to set |
| 522 | * @throws WorkflowException workflow exception |
| 523 | */ |
| 524 | public void setAt(String path, ObjectNode objectNode) throws WorkflowException { |
| 525 | JsonPointer ptr = JsonPointer.compile(path); |
| 526 | setAt(ptr, objectNode); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /** |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 531 | * Sets boolean on the specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 532 | * |
| 533 | * @param ptr json pointer |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 534 | * @param isTrue boolean to set |
| 535 | * @throws WorkflowException workflow exception |
| 536 | */ |
| 537 | public void setAt(JsonPointer ptr, Boolean isTrue) throws WorkflowException { |
| 538 | BooleanNode booleanNode = BooleanNode.valueOf(isTrue); |
| 539 | attach(ptr, booleanNode); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Sets integer on specific json path. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 544 | * |
| 545 | * @param path json path |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 546 | * @param number number to set |
| 547 | * @throws WorkflowException workflow exception |
| 548 | */ |
| 549 | public void setAt(String path, Integer number) throws WorkflowException { |
| 550 | JsonPointer ptr = JsonPointer.compile(path); |
| 551 | setAt(ptr, number); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * Sets integer on the specific json pointer. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 556 | * |
| 557 | * @param ptr json pointer |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 558 | * @param number number to set |
| 559 | * @throws WorkflowException workflow exception |
| 560 | */ |
| 561 | public void setAt(JsonPointer ptr, Integer number) throws WorkflowException { |
| 562 | IntNode intNode = IntNode.valueOf(number); |
| 563 | attach(ptr, intNode); |
| 564 | } |
| 565 | |
| 566 | /** |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 567 | * Allocates json data model tree on json pointer path with specific leaf type. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 568 | * |
| 569 | * @param node current json node in the json tree path |
| 570 | * @param ptr json pointer |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 571 | * @param leaftype leaf type to be allocated |
| 572 | * @return allocated json node |
| 573 | * @throws WorkflowException workflow exception |
| 574 | */ |
| 575 | private JsonNode alloc(JsonNode node, JsonPointer ptr, JsonNodeType leaftype) throws WorkflowException { |
| 576 | |
| 577 | if (ptr.matches()) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 578 | if (node == null || node instanceof MissingNode) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 579 | node = createEmpty(leaftype); |
| 580 | } else { |
| 581 | //TODO: checking existing node type is matched with leaftype |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 582 | if (!Objects.equals(node.getNodeType(), leaftype)) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 583 | throw new WorkflowException("Requesting leaftype(" + leaftype + ") is not matched with " |
| 584 | + "existing nodetype(" + node.getNodeType() + ") for " + ptr); |
| 585 | } |
| 586 | } |
| 587 | return node; |
| 588 | } |
| 589 | |
| 590 | if (ptr.getMatchingIndex() != -1) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 591 | if (node == null || node instanceof MissingNode) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 592 | node = createEmpty(JsonNodeType.ARRAY); |
| 593 | } |
| 594 | JsonNode child = alloc(node.get(ptr.getMatchingIndex()), ptr.tail(), leaftype); |
| 595 | if (!node.has(ptr.getMatchingIndex())) { |
| 596 | ((ArrayNode) node).insert(ptr.getMatchingIndex(), child); |
| 597 | } |
| 598 | } else if (ptr.getMatchingProperty() != null) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 599 | if (node == null || node instanceof MissingNode) { |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 600 | node = createEmpty(JsonNodeType.OBJECT); |
| 601 | } |
| 602 | JsonNode child = alloc(node.get(ptr.getMatchingProperty()), ptr.tail(), leaftype); |
| 603 | if (!node.has(ptr.getMatchingProperty())) { |
| 604 | ((ObjectNode) node).put(ptr.getMatchingProperty(), child); |
| 605 | } |
| 606 | } |
| 607 | return node; |
| 608 | } |
| 609 | |
| 610 | /** |
| 611 | * Creating empty json node. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 612 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 613 | * @param type json node type to create |
| 614 | * @return created json node |
| 615 | * @throws WorkflowException workflow exception |
| 616 | */ |
| 617 | private JsonNode createEmpty(JsonNodeType type) throws WorkflowException { |
| 618 | if (type == JsonNodeType.OBJECT) { |
| 619 | return JsonNodeFactory.instance.objectNode(); |
| 620 | } else if (type == JsonNodeType.ARRAY) { |
| 621 | return JsonNodeFactory.instance.arrayNode(); |
| 622 | } else if (type == JsonNodeType.STRING) { |
| 623 | return JsonNodeFactory.instance.textNode(""); |
| 624 | } else { |
| 625 | throw new WorkflowException("Not supported JsonNodetype(" + type + ")"); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Gets the pretty json formatted string of this json data model tree. |
m.rahil | 0925188 | 2019-04-15 22:58:33 +0530 | [diff] [blame] | 631 | * |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 632 | * @return pretty json formatted string of this json data model tree |
| 633 | */ |
| 634 | public String formattedRootString() { |
| 635 | String str = ""; |
| 636 | try { |
| 637 | str = (new ObjectMapper()).writerWithDefaultPrettyPrinter().writeValueAsString(root); |
| 638 | } catch (JsonProcessingException e) { |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 639 | log.error("Exception: ", e); |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 640 | } |
| 641 | return str; |
| 642 | } |
| 643 | |
| 644 | @Override |
| 645 | public String toString() { |
| 646 | return MoreObjects.toStringHelper(getClass()) |
jaegonkim | e0f45b5 | 2018-10-09 20:23:26 +0900 | [diff] [blame] | 647 | .add("json", root) |
jaegonkim | 6a7b524 | 2018-09-12 23:09:42 +0900 | [diff] [blame] | 648 | .toString(); |
| 649 | } |
| 650 | } |
| 651 | |