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