Merge "Not logged or rethrow this exception."
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / util / NodeUtils.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.onap.aai.datarouter.util;
24
25 import java.io.IOException;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Iterator;
29 import java.util.Map;
30
31 import com.fasterxml.jackson.core.JsonProcessingException;
32 import com.fasterxml.jackson.databind.JsonNode;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.fasterxml.jackson.databind.ObjectWriter;
35 import com.fasterxml.jackson.databind.SerializationFeature;
36
37 public class NodeUtils {
38   /**
39   * Generate unique sha digest. This method is copy over from NodeUtils class in AAIUI
40   *
41   * @param keys the keys
42   * @return the string
43   */
44   public static String generateUniqueShaDigest(String... keys) {
45     if ((keys == null) || keys.length == 0) {
46       return null;
47     }
48   
49     final String keysStr = Arrays.asList(keys).toString();
50     final String hashedId = org.apache.commons.codec.digest.DigestUtils.sha256Hex(keysStr);
51   
52     return hashedId;
53   }
54   
55   /**
56    * Extract field value from object.
57    *
58    * @param node the node
59    * @param fieldName the field name
60    * @return the string
61    */
62   public static String extractFieldValueFromObject(JsonNode node, String fieldName) {
63
64     if (node == null) {
65       return null;
66     }
67
68     if (node.isObject()) {
69
70       JsonNode valueNode = node.get(fieldName);
71
72       if (valueNode != null) {
73
74         if (valueNode.isValueNode()) {
75           return valueNode.asText();
76         }
77       }
78
79     }
80     return null;
81
82   }
83
84   /**
85    * Convert json str to json node.
86    *
87    * @param jsonStr the json str
88    * @return the json node
89    * @throws IOException Signals that an I/O exception has occurred.
90    */
91   public static JsonNode convertJsonStrToJsonNode(String jsonStr) throws IOException {
92     ObjectMapper mapper = new ObjectMapper();
93     if (jsonStr == null || jsonStr.length() == 0) {
94       return null;
95     }
96
97     return mapper.readTree(jsonStr);
98   }
99   
100   /**
101    * Extract objects by key.
102    *
103    * @param node the node
104    * @param searchKey the search key
105    * @param foundObjects the found objects
106    */
107   public static void extractObjectsByKey(JsonNode node, String searchKey,
108       Collection<JsonNode> foundObjects) {
109
110     if ( node == null ) {
111       return;
112     }
113     
114     if (node.isObject()) {
115       Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
116
117       while (nodeIterator.hasNext()) {
118         Map.Entry<String, JsonNode> entry = nodeIterator.next();
119         if (!entry.getValue().isValueNode()) {
120           extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
121         }
122
123         String name = entry.getKey();
124         if (name.equalsIgnoreCase(searchKey)) {
125
126           JsonNode entryNode = entry.getValue();
127
128           if (entryNode.isArray()) {
129
130             Iterator<JsonNode> arrayItemsIterator = entryNode.elements();
131             while (arrayItemsIterator.hasNext()) {
132               foundObjects.add(arrayItemsIterator.next());
133             }
134
135           } else {
136             foundObjects.add(entry.getValue());
137           }
138
139
140         }
141       }
142     } else if (node.isArray()) {
143       Iterator<JsonNode> arrayItemsIterator = node.elements();
144       while (arrayItemsIterator.hasNext()) {
145         extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
146       }
147
148     }
149
150   }
151   
152   /**
153    * Convert object to json.
154    *
155    * @param object the object
156    * @param pretty the pretty
157    * @return the string
158    * @throws JsonProcessingException the json processing exception
159    */
160   public static String convertObjectToJson(Object object, boolean pretty)
161       throws JsonProcessingException {
162     ObjectWriter ow;
163
164     ObjectMapper mapper = new ObjectMapper();
165     mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
166     
167     if (pretty) {
168       ow = mapper.writer().withDefaultPrettyPrinter();
169
170     } else {
171       ow = mapper.writer();
172     }
173
174     return ow.writeValueAsString(object);
175   }
176
177 }