Changing the license and trademark
[aai/sparky-be.git] / src / main / java / org / openecomp / sparky / util / TreeWalker.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.openecomp.sparky.util;
24
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;
29
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35
36 /**
37  * The Class TreeWalker.
38  */
39 public class TreeWalker {
40
41   /**
42    * Convert json to node.
43    *
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.
48    */
49   public JsonNode convertJsonToNode(String json) throws JsonProcessingException, IOException {
50     ObjectMapper mapper = new ObjectMapper();
51
52     if (json == null) {
53       return null;
54     }
55
56     return mapper.readTree(json);
57
58   }
59
60   /**
61    * Walk tree.
62    *
63    * @param paths the paths
64    * @param root the root
65    */
66   public void walkTree(List<String> paths, JsonNode root) {
67     walker(paths, null, root);
68   }
69
70   /**
71    * Walker.
72    *
73    * @param paths the paths
74    * @param nodename the nodename
75    * @param node the node
76    */
77   private void walker(List<String> paths, String nodename, JsonNode node) {
78
79     if (node == null) {
80       return;
81     }
82
83     /*
84      * if ( nodename != null ) { paths.add(nodename); }
85      */
86
87     // System.out.println("path: " + nameToPrint);
88     if (node.isObject()) {
89       Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
90
91       ArrayList<Map.Entry<String, JsonNode>> nodesList = Lists.newArrayList(iterator);
92       // System.out.println("Walk Tree - root:" + node + ", elements
93       // keys:" + nodesList);
94
95       if (nodesList.isEmpty()) {
96
97         if (nodename != null) {
98           paths.add(nodename);
99         }
100
101       } else {
102
103         for (Map.Entry<String, JsonNode> nodEntry : nodesList) {
104           String name = nodEntry.getKey();
105           JsonNode newNode = nodEntry.getValue();
106
107           if (newNode.isValueNode()) {
108             if (nodename == null) {
109               paths.add(name + "=" + newNode.asText());
110             } else {
111               paths.add(nodename + "." + name + "=" + newNode.asText());
112             }
113           } else {
114
115             if (nodename == null) {
116               walker(paths, name, newNode);
117             } else {
118               walker(paths, nodename + "." + name, newNode);
119             }
120           }
121
122         }
123       }
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);
129       }
130     } else if (node.isValueNode()) {
131       paths.add(nodename + "=" + node.asText());
132     }
133   }
134 }