2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.datarouter.util;
27 import java.io.IOException;
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.Iterator;
33 import com.fasterxml.jackson.core.JsonProcessingException;
34 import com.fasterxml.jackson.databind.JsonNode;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import com.fasterxml.jackson.databind.ObjectWriter;
37 import com.fasterxml.jackson.databind.SerializationFeature;
39 public class NodeUtils {
41 * Generate unique sha digest. This method is copy over from NodeUtils class in AAIUI
43 * @param keys the keys
46 public static String generateUniqueShaDigest(String... keys) {
47 if ((keys == null) || keys.length == 0) {
51 final String keysStr = Arrays.asList(keys).toString();
52 final String hashedId = org.apache.commons.codec.digest.DigestUtils.sha256Hex(keysStr);
58 * Extract field value from object.
60 * @param node the node
61 * @param fieldName the field name
64 public static String extractFieldValueFromObject(JsonNode node, String fieldName) {
70 if (node.isObject()) {
72 JsonNode valueNode = node.get(fieldName);
74 if (valueNode != null) {
76 if (valueNode.isValueNode()) {
77 return valueNode.asText();
87 * Convert json str to json node.
89 * @param jsonStr the json str
90 * @return the json node
91 * @throws IOException Signals that an I/O exception has occurred.
93 public static JsonNode convertJsonStrToJsonNode(String jsonStr) throws IOException {
94 ObjectMapper mapper = new ObjectMapper();
95 if (jsonStr == null || jsonStr.length() == 0) {
99 return mapper.readTree(jsonStr);
103 * Extract objects by key.
105 * @param node the node
106 * @param searchKey the search key
107 * @param foundObjects the found objects
109 public static void extractObjectsByKey(JsonNode node, String searchKey,
110 Collection<JsonNode> foundObjects) {
112 if ( node == null ) {
116 if (node.isObject()) {
117 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
119 while (nodeIterator.hasNext()) {
120 Map.Entry<String, JsonNode> entry = nodeIterator.next();
121 if (!entry.getValue().isValueNode()) {
122 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
125 String name = entry.getKey();
126 if (name.equalsIgnoreCase(searchKey)) {
128 JsonNode entryNode = entry.getValue();
130 if (entryNode.isArray()) {
132 Iterator<JsonNode> arrayItemsIterator = entryNode.elements();
133 while (arrayItemsIterator.hasNext()) {
134 foundObjects.add(arrayItemsIterator.next());
138 foundObjects.add(entry.getValue());
144 } else if (node.isArray()) {
145 Iterator<JsonNode> arrayItemsIterator = node.elements();
146 while (arrayItemsIterator.hasNext()) {
147 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
155 * Convert object to json.
157 * @param object the object
158 * @param pretty the pretty
160 * @throws JsonProcessingException the json processing exception
162 public static String convertObjectToJson(Object object, boolean pretty)
163 throws JsonProcessingException {
164 ObjectWriter ow = null;
166 ObjectMapper mapper = new ObjectMapper();
167 mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
170 ow = mapper.writer().withDefaultPrettyPrinter();
173 ow = mapper.writer();
176 return ow.writeValueAsString(object);