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