2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.util;
23 import java.io.IOException;
24 import java.util.Arrays;
25 import java.util.Collection;
26 import java.util.Iterator;
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;
35 public class NodeUtils {
37 * Generate unique sha digest. This method is copy over from NodeUtils class in AAIUI
39 * @param keys the keys
42 public static String generateUniqueShaDigest(String... keys) {
43 if ((keys == null) || keys.length == 0) {
47 final String keysStr = Arrays.asList(keys).toString();
48 final String hashedId = org.apache.commons.codec.digest.DigestUtils.sha256Hex(keysStr);
54 * Extract field value from object.
56 * @param node the node
57 * @param fieldName the field name
60 public static String extractFieldValueFromObject(JsonNode node, String fieldName) {
66 if (node.isObject()) {
68 JsonNode valueNode = node.get(fieldName);
70 if (valueNode != null) {
72 if (valueNode.isValueNode()) {
73 return valueNode.asText();
83 * Convert json str to json node.
85 * @param jsonStr the json str
86 * @return the json node
87 * @throws IOException Signals that an I/O exception has occurred.
89 public static JsonNode convertJsonStrToJsonNode(String jsonStr) throws IOException {
90 ObjectMapper mapper = new ObjectMapper();
91 if (jsonStr == null || jsonStr.length() == 0) {
95 return mapper.readTree(jsonStr);
99 * Extract objects by key.
101 * @param node the node
102 * @param searchKey the search key
103 * @param foundObjects the found objects
105 public static void extractObjectsByKey(JsonNode node, String searchKey,
106 Collection<JsonNode> foundObjects) {
108 if ( node == null ) {
112 if (node.isObject()) {
113 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
115 while (nodeIterator.hasNext()) {
116 Map.Entry<String, JsonNode> entry = nodeIterator.next();
117 if (!entry.getValue().isValueNode()) {
118 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
121 String name = entry.getKey();
122 if (name.equalsIgnoreCase(searchKey)) {
124 JsonNode entryNode = entry.getValue();
126 if (entryNode.isArray()) {
128 Iterator<JsonNode> arrayItemsIterator = entryNode.elements();
129 while (arrayItemsIterator.hasNext()) {
130 foundObjects.add(arrayItemsIterator.next());
134 foundObjects.add(entry.getValue());
140 } else if (node.isArray()) {
141 Iterator<JsonNode> arrayItemsIterator = node.elements();
142 while (arrayItemsIterator.hasNext()) {
143 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
151 * Convert object to json.
153 * @param object the object
154 * @param pretty the pretty
156 * @throws JsonProcessingException the json processing exception
158 public static String convertObjectToJson(Object object, boolean pretty)
159 throws JsonProcessingException {
162 ObjectMapper mapper = new ObjectMapper();
163 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
166 ow = mapper.writer().withDefaultPrettyPrinter();
169 ow = mapper.writer();
172 return ow.writeValueAsString(object);