2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.sparky.util;
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.JsonNode;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.collect.Lists;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
37 * The Class TreeWalker.
39 public class TreeWalker {
42 * Convert json to node.
44 * @param json the json
45 * @return the json node
46 * @throws JsonProcessingException the json processing exception
47 * @throws IOException Signals that an I/O exception has occurred.
49 public JsonNode convertJsonToNode(String json) throws JsonProcessingException, IOException {
50 ObjectMapper mapper = new ObjectMapper();
56 return mapper.readTree(json);
63 * @param paths the paths
64 * @param root the root
66 public void walkTree(List<String> paths, JsonNode root) {
67 walker(paths, null, root);
73 * @param paths the paths
74 * @param nodename the nodename
75 * @param node the node
77 private void walker(List<String> paths, String nodename, JsonNode node) {
84 * if ( nodename != null ) { paths.add(nodename); }
87 // System.out.println("path: " + nameToPrint);
88 if (node.isObject()) {
89 Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
91 ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
92 // System.out.println("Walk Tree - root:" + node + ", elements
93 // keys:" + nodesList);
95 if (nodesList.isEmpty()) {
97 if (nodename != null) {
103 for (Map.Entry<String, JsonNode> nodEntry : nodesList) {
104 String name = nodEntry.getKey();
105 JsonNode newNode = nodEntry.getValue();
107 if (newNode.isValueNode()) {
108 if (nodename == null) {
109 paths.add(name + "=" + newNode.asText());
111 paths.add(nodename + "." + name + "=" + newNode.asText());
115 if (nodename == null) {
116 walker(paths, name, newNode);
118 walker(paths, nodename + "." + name, newNode);
124 } else if (node.isArray()) {
125 Iterator<JsonNode> arrayItemsIterator = node.elements();
126 ArrayList<JsonNode> arrayItemsList = Lists.newArrayList(arrayItemsIterator);
127 for (JsonNode arrayNode : arrayItemsList) {
128 walker(paths, nodename, arrayNode);
130 } else if (node.isValueNode()) {
131 paths.add(nodename + "=" + node.asText());