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