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.
25 package org.onap.aai.sparky.util;
27 import java.io.IOException;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.google.common.collect.Lists;
39 * The Class TreeWalker.
41 public class TreeWalker {
44 * Convert json to node.
46 * @param json the json
47 * @return the json node
48 * @throws JsonProcessingException the json processing exception
49 * @throws IOException Signals that an I/O exception has occurred.
51 public JsonNode convertJsonToNode(String json) throws JsonProcessingException, IOException {
52 ObjectMapper mapper = new ObjectMapper();
58 return mapper.readTree(json);
65 * @param paths the paths
66 * @param root the root
68 public void walkTree(List<String> paths, JsonNode root) {
69 walker(paths, null, root);
75 * @param paths the paths
76 * @param nodename the nodename
77 * @param node the node
79 private void walker(List<String> paths, String nodename, JsonNode node) {
86 * if ( nodename != null ) { paths.add(nodename); }
89 // System.out.println("path: " + nameToPrint);
90 if (node.isObject()) {
91 Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
93 ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
94 // System.out.println("Walk Tree - root:" + node + ", elements
95 // keys:" + nodesList);
97 if (nodesList.isEmpty()) {
99 if (nodename != null) {
105 for (Map.Entry<String, JsonNode> nodEntry : nodesList) {
106 String name = nodEntry.getKey();
107 JsonNode newNode = nodEntry.getValue();
109 if (newNode.isValueNode()) {
110 if (nodename == null) {
111 paths.add(name + "=" + newNode.asText());
113 paths.add(nodename + "." + name + "=" + newNode.asText());
117 if (nodename == null) {
118 walker(paths, name, newNode);
120 walker(paths, nodename + "." + name, newNode);
126 } else if (node.isArray()) {
127 Iterator<JsonNode> arrayItemsIterator = node.elements();
128 ArrayList<JsonNode> arrayItemsList = Lists.newArrayList(arrayItemsIterator);
129 for (JsonNode arrayNode : arrayItemsList) {
130 walker(paths, nodename, arrayNode);
132 } else if (node.isValueNode()) {
133 paths.add(nodename + "=" + node.asText());