2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.util;
28 import com.fasterxml.jackson.core.JsonProcessingException;
29 import com.fasterxml.jackson.databind.JsonNode;
30 import com.fasterxml.jackson.databind.ObjectMapper;
31 import com.google.common.collect.Lists;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
40 * The Class TreeWalker.
42 public class TreeWalker {
45 * Convert json to node.
47 * @param json the json
48 * @return the json node
49 * @throws JsonProcessingException the json processing exception
50 * @throws IOException Signals that an I/O exception has occurred.
52 public JsonNode convertJsonToNode(String json) throws JsonProcessingException, IOException {
53 ObjectMapper mapper = new ObjectMapper();
59 return mapper.readTree(json);
66 * @param paths the paths
67 * @param root the root
69 public void walkTree(List<String> paths, JsonNode root) {
70 walker(paths, null, root);
76 * @param paths the paths
77 * @param nodename the nodename
78 * @param node the node
80 private void walker(List<String> paths, String nodename, JsonNode node) {
87 * if ( nodename != null ) { paths.add(nodename); }
90 // System.out.println("path: " + nameToPrint);
91 if (node.isObject()) {
92 Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
94 ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
95 // System.out.println("Walk Tree - root:" + node + ", elements
96 // keys:" + nodesList);
98 if (nodesList.isEmpty()) {
100 if (nodename != null) {
106 for (Map.Entry<String, JsonNode> nodEntry : nodesList) {
107 String name = nodEntry.getKey();
108 JsonNode newNode = nodEntry.getValue();
110 if (newNode.isValueNode()) {
111 if (nodename == null) {
112 paths.add(name + "=" + newNode.asText());
114 paths.add(nodename + "." + name + "=" + newNode.asText());
118 if (nodename == null) {
119 walker(paths, name, newNode);
121 walker(paths, nodename + "." + name, newNode);
127 } else if (node.isArray()) {
128 Iterator<JsonNode> arrayItemsIterator = node.elements();
129 ArrayList<JsonNode> arrayItemsList = Lists.newArrayList(arrayItemsIterator);
130 for (JsonNode arrayNode : arrayItemsList) {
131 walker(paths, nodename, arrayNode);
133 } else if (node.isValueNode()) {
134 paths.add(nodename + "=" + node.asText());