blob: aec19ae9c2d09d7e7d1f57a3300395983b7bb373 [file] [log] [blame]
jaegonkim6a7b5242018-09-12 23:09:42 +09001/*
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 */
16package org.onosproject.workflow.api;
17
18
19import com.fasterxml.jackson.core.JsonPointer;
20import com.fasterxml.jackson.core.JsonProcessingException;
21import com.fasterxml.jackson.databind.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
jaegonkime0f45b52018-10-09 20:23:26 +090024import com.fasterxml.jackson.databind.node.BooleanNode;
25import com.fasterxml.jackson.databind.node.IntNode;
jaegonkim6a7b5242018-09-12 23:09:42 +090026import com.fasterxml.jackson.databind.node.JsonNodeFactory;
27import com.fasterxml.jackson.databind.node.JsonNodeType;
28import com.fasterxml.jackson.databind.node.MissingNode;
jaegonkime0f45b52018-10-09 20:23:26 +090029import com.fasterxml.jackson.databind.node.NumericNode;
jaegonkim6a7b5242018-09-12 23:09:42 +090030import com.fasterxml.jackson.databind.node.ObjectNode;
jaegonkime0f45b52018-10-09 20:23:26 +090031import com.fasterxml.jackson.databind.node.TextNode;
jaegonkim6a7b5242018-09-12 23:09:42 +090032import com.google.common.base.MoreObjects;
jaegonkime0f45b52018-10-09 20:23:26 +090033import org.slf4j.Logger;
34import org.slf4j.LoggerFactory;
jaegonkim6a7b5242018-09-12 23:09:42 +090035
36import java.util.Objects;
37
38/**
39 * Class for json data model tree.
40 */
41public final class JsonDataModelTree implements DataModelTree {
42
jaegonkime0f45b52018-10-09 20:23:26 +090043 private static final Logger log = LoggerFactory.getLogger(JsonDataModelTree.class);
44
jaegonkim6a7b5242018-09-12 23:09:42 +090045 /**
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.rahil09251882019-04-15 22:58:33 +053059 *
jaegonkim6a7b5242018-09-12 23:09:42 +090060 * @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 {
jaegonkime0f45b52018-10-09 20:23:26 +090077
jaegonkim6a7b5242018-09-12 23:09:42 +090078 if (root == null || root instanceof MissingNode) {
79 throw new WorkflowException("Invalid root node");
80 }
81
82 JsonPointer ptr = JsonPointer.compile(path);
jaegonkim6a7b5242018-09-12 23:09:42 +090083
84 if (!(tree instanceof JsonDataModelTree)) {
85 throw new WorkflowException("Invalid subTree(" + tree + ")");
86 }
87 JsonNode attachingNode = ((JsonDataModelTree) tree).root();
88
jaegonkime0f45b52018-10-09 20:23:26 +090089 attach(ptr, attachingNode);
90 }
jaegonkim6a7b5242018-09-12 23:09:42 +090091
jaegonkime0f45b52018-10-09 20:23:26 +090092 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 + ")");
jaegonkim6a7b5242018-09-12 23:09:42 +090097 }
98
jaegonkime0f45b52018-10-09 20:23:26 +090099 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 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900159 }
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.rahil09251882019-04-15 22:58:33 +0530173 *
174 * @param ptr json pointer to allocate
jaegonkim6a7b5242018-09-12 23:09:42 +0900175 * @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.rahil09251882019-04-15 22:58:33 +0530199 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900200 * @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.rahil09251882019-04-15 22:58:33 +0530209 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900210 * @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.rahil09251882019-04-15 22:58:33 +0530219 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900220 * @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.rahil09251882019-04-15 22:58:33 +0530229 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900230 * @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.rahil09251882019-04-15 22:58:33 +0530241 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900242 * @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.rahil09251882019-04-15 22:58:33 +0530256 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900257 * @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.rahil09251882019-04-15 22:58:33 +0530268 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900269 * @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);
jaegonkime0f45b52018-10-09 20:23:26 +0900278 if (node instanceof MissingNode) {
279 return null;
280 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900281 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.rahil09251882019-04-15 22:58:33 +0530289 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900290 * @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.rahil09251882019-04-15 22:58:33 +0530301 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900302 * @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);
jaegonkime0f45b52018-10-09 20:23:26 +0900311 if (node instanceof MissingNode) {
312 return null;
313 }
jaegonkim6a7b5242018-09-12 23:09:42 +0900314 if (!(node instanceof ArrayNode)) {
315 throw new WorkflowException("Invalid node(" + node + ") at " + ptr);
316 }
317 return (ArrayNode) node;
318 }
319
320 /**
jaegonkime0f45b52018-10-09 20:23:26 +0900321 * Gets text node on specific path.
m.rahil09251882019-04-15 22:58:33 +0530322 *
jaegonkime0f45b52018-10-09 20:23:26 +0900323 * @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.rahil09251882019-04-15 22:58:33 +0530334 *
jaegonkime0f45b52018-10-09 20:23:26 +0900335 * @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.rahil09251882019-04-15 22:58:33 +0530355 *
jaegonkime0f45b52018-10-09 20:23:26 +0900356 * @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.rahil09251882019-04-15 22:58:33 +0530367 *
jaegonkime0f45b52018-10-09 20:23:26 +0900368 * @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.rahil09251882019-04-15 22:58:33 +0530388 *
jaegonkime0f45b52018-10-09 20:23:26 +0900389 * @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.rahil09251882019-04-15 22:58:33 +0530400 *
jaegonkime0f45b52018-10-09 20:23:26 +0900401 * @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.rahil09251882019-04-15 22:58:33 +0530421 *
jaegonkime0f45b52018-10-09 20:23:26 +0900422 * @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.rahil09251882019-04-15 22:58:33 +0530433 *
434 * @param ptr json pointer
jaegonkime0f45b52018-10-09 20:23:26 +0900435 * @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.rahil09251882019-04-15 22:58:33 +0530440
jaegonkime0f45b52018-10-09 20:23:26 +0900441 attach(ptr, textNode);
442 }
443
444 /**
445 * Sets boolean on specific json path.
m.rahil09251882019-04-15 22:58:33 +0530446 *
447 * @param path json path
jaegonkime0f45b52018-10-09 20:23:26 +0900448 * @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.rahil09251882019-04-15 22:58:33 +0530457 * 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 /**
jaegonkime0f45b52018-10-09 20:23:26 +0900531 * Sets boolean on the specific json pointer.
m.rahil09251882019-04-15 22:58:33 +0530532 *
533 * @param ptr json pointer
jaegonkime0f45b52018-10-09 20:23:26 +0900534 * @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.rahil09251882019-04-15 22:58:33 +0530544 *
545 * @param path json path
jaegonkime0f45b52018-10-09 20:23:26 +0900546 * @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.rahil09251882019-04-15 22:58:33 +0530556 *
557 * @param ptr json pointer
jaegonkime0f45b52018-10-09 20:23:26 +0900558 * @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 /**
jaegonkim6a7b5242018-09-12 23:09:42 +0900567 * Allocates json data model tree on json pointer path with specific leaf type.
m.rahil09251882019-04-15 22:58:33 +0530568 *
569 * @param node current json node in the json tree path
570 * @param ptr json pointer
jaegonkim6a7b5242018-09-12 23:09:42 +0900571 * @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()) {
jaegonkime0f45b52018-10-09 20:23:26 +0900578 if (node == null || node instanceof MissingNode) {
jaegonkim6a7b5242018-09-12 23:09:42 +0900579 node = createEmpty(leaftype);
580 } else {
581 //TODO: checking existing node type is matched with leaftype
jaegonkime0f45b52018-10-09 20:23:26 +0900582 if (!Objects.equals(node.getNodeType(), leaftype)) {
jaegonkim6a7b5242018-09-12 23:09:42 +0900583 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) {
jaegonkime0f45b52018-10-09 20:23:26 +0900591 if (node == null || node instanceof MissingNode) {
jaegonkim6a7b5242018-09-12 23:09:42 +0900592 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) {
jaegonkime0f45b52018-10-09 20:23:26 +0900599 if (node == null || node instanceof MissingNode) {
jaegonkim6a7b5242018-09-12 23:09:42 +0900600 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.rahil09251882019-04-15 22:58:33 +0530612 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900613 * @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.rahil09251882019-04-15 22:58:33 +0530631 *
jaegonkim6a7b5242018-09-12 23:09:42 +0900632 * @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) {
jaegonkime0f45b52018-10-09 20:23:26 +0900639 log.error("Exception: ", e);
jaegonkim6a7b5242018-09-12 23:09:42 +0900640 }
641 return str;
642 }
643
644 @Override
645 public String toString() {
646 return MoreObjects.toStringHelper(getClass())
jaegonkime0f45b52018-10-09 20:23:26 +0900647 .add("json", root)
jaegonkim6a7b5242018-09-12 23:09:42 +0900648 .toString();
649 }
650}
651