2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sparky.util;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.google.common.collect.Lists;
35 * The Class TreeWalker.
37 public class TreeWalker {
40 * Convert json to node.
42 * @param json the json
43 * @return the json node
44 * @throws JsonProcessingException the json processing exception
45 * @throws IOException Signals that an I/O exception has occurred.
47 public JsonNode convertJsonToNode(String json) throws JsonProcessingException, IOException {
48 ObjectMapper mapper = new ObjectMapper();
54 return mapper.readTree(json);
61 * @param paths the paths
62 * @param root the root
64 public void walkTree(List<String> paths, JsonNode root) {
65 walker(paths, null, root);
71 * @param paths the paths
72 * @param nodename the nodename
73 * @param node the node
75 private void walker(List<String> paths, String nodename, JsonNode node) {
82 * if ( nodename != null ) { paths.add(nodename); }
85 // System.out.println("path: " + nameToPrint);
86 if (node.isObject()) {
87 Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
89 ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
90 // System.out.println("Walk Tree - root:" + node + ", elements
91 // keys:" + nodesList);
93 if (nodesList.isEmpty()) {
95 if (nodename != null) {
101 for (Map.Entry<String, JsonNode> nodEntry : nodesList) {
102 String name = nodEntry.getKey();
103 JsonNode newNode = nodEntry.getValue();
105 if (newNode.isValueNode()) {
106 if (nodename == null) {
107 paths.add(name + "=" + newNode.asText());
109 paths.add(nodename + "." + name + "=" + newNode.asText());
113 if (nodename == null) {
114 walker(paths, name, newNode);
116 walker(paths, nodename + "." + name, newNode);
122 } else if (node.isArray()) {
123 Iterator<JsonNode> arrayItemsIterator = node.elements();
124 ArrayList<JsonNode> arrayItemsList = Lists.newArrayList(arrayItemsIterator);
125 for (JsonNode arrayNode : arrayItemsList) {
126 walker(paths, nodename, arrayNode);
128 } else if (node.isValueNode()) {
129 paths.add(nodename + "=" + node.asText());