2 * ============LICENSE_START=======================================================
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
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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.datarouter.util;
25 import java.io.IOException;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Iterator;
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;
37 public class NodeUtils {
39 * Generate unique sha digest. This method is copy over from NodeUtils class in AAIUI
41 * @param keys the keys
44 public static String generateUniqueShaDigest(String... keys) {
45 if ((keys == null) || keys.length == 0) {
49 final String keysStr = Arrays.asList(keys).toString();
50 final String hashedId = org.apache.commons.codec.digest.DigestUtils.sha256Hex(keysStr);
56 * Extract field value from object.
58 * @param node the node
59 * @param fieldName the field name
62 public static String extractFieldValueFromObject(JsonNode node, String fieldName) {
68 if (node.isObject()) {
70 JsonNode valueNode = node.get(fieldName);
72 if (valueNode != null) {
74 if (valueNode.isValueNode()) {
75 return valueNode.asText();
85 * Convert json str to json node.
87 * @param jsonStr the json str
88 * @return the json node
89 * @throws IOException Signals that an I/O exception has occurred.
91 public static JsonNode convertJsonStrToJsonNode(String jsonStr) throws IOException {
92 ObjectMapper mapper = new ObjectMapper();
93 if (jsonStr == null || jsonStr.length() == 0) {
97 return mapper.readTree(jsonStr);
101 * Extract objects by key.
103 * @param node the node
104 * @param searchKey the search key
105 * @param foundObjects the found objects
107 public static void extractObjectsByKey(JsonNode node, String searchKey,
108 Collection<JsonNode> foundObjects) {
110 if ( node == null ) {
114 if (node.isObject()) {
115 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
117 while (nodeIterator.hasNext()) {
118 Map.Entry<String, JsonNode> entry = nodeIterator.next();
119 if (!entry.getValue().isValueNode()) {
120 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
123 String name = entry.getKey();
124 if (name.equalsIgnoreCase(searchKey)) {
126 JsonNode entryNode = entry.getValue();
128 if (entryNode.isArray()) {
130 Iterator<JsonNode> arrayItemsIterator = entryNode.elements();
131 while (arrayItemsIterator.hasNext()) {
132 foundObjects.add(arrayItemsIterator.next());
136 foundObjects.add(entry.getValue());
142 } else if (node.isArray()) {
143 Iterator<JsonNode> arrayItemsIterator = node.elements();
144 while (arrayItemsIterator.hasNext()) {
145 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
153 * Convert object to json.
155 * @param object the object
156 * @param pretty the pretty
158 * @throws JsonProcessingException the json processing exception
160 public static String convertObjectToJson(Object object, boolean pretty)
161 throws JsonProcessingException {
164 ObjectMapper mapper = new ObjectMapper();
165 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
168 ow = mapper.writer().withDefaultPrettyPrinter();
171 ow = mapper.writer();
174 return ow.writeValueAsString(object);