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