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 com.fasterxml.jackson.databind.JsonNode;
29 import org.json.JSONObject;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.Iterator;
35 import java.util.List;
38 public class RouterServiceUtil {
40 public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) {
42 JSONObject jsonObject = new JSONObject(jsonPayload);
43 Map<String, String> map = new HashMap<String, String>();
44 Iterator iter = jsonObject.keys();
45 while (iter.hasNext()) {
46 String key = (String) iter.next();
47 String value = jsonObject.getString(key);
53 public static String getNodeFieldAsText(JsonNode node, String fieldName) {
55 String fieldValue = null;
57 JsonNode valueNode = node.get(fieldName);
59 if (valueNode != null) {
60 fieldValue = valueNode.asText();
66 public static String concatArray(List<String> list) {
67 return concatArray(list, " ");
70 public static String concatArray(List<String> list, String delimiter) {
72 if (list == null || list.size() == 0) {
76 StringBuilder result = new StringBuilder(64);
78 boolean firstValue = true;
80 for (String item : list) {
86 result.append(delimiter).append(item);
90 return result.toString();
94 public static String concatArray(String[] values) {
96 if (values == null || values.length == 0) {
100 StringBuilder result = new StringBuilder(64);
102 boolean firstValue = true;
104 for (String item : values) {
110 result.append(".").append(item);
115 return result.toString();
119 public static String recursivelyLookupJsonPayload(JsonNode node, String key) {
121 if (node.isObject()) {
122 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
124 while (nodeIterator.hasNext()) {
125 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
126 if (!entry.getValue().isValueNode()) {
127 value = recursivelyLookupJsonPayload(entry.getValue(), key);
133 String name = entry.getKey();
134 if (name.equalsIgnoreCase(key)) {
135 return entry.getValue().asText();
138 } else if (node.isArray()) {
139 Iterator<JsonNode> arrayItemsIterator = node.elements();
140 while (arrayItemsIterator.hasNext()) {
141 value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key);
150 public static void extractObjectsByKey(JsonNode node, String searchKey,
151 Collection<JsonNode> foundObjects) {
153 if (node.isObject()) {
154 Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
156 while (nodeIterator.hasNext()) {
157 Map.Entry<String, JsonNode> entry = (Map.Entry<String, JsonNode>) nodeIterator.next();
158 if (!entry.getValue().isValueNode()) {
159 extractObjectsByKey(entry.getValue(), searchKey, foundObjects);
162 String name = entry.getKey();
163 if (name.equalsIgnoreCase(searchKey)) {
165 JsonNode entryValue = entry.getValue();
167 if (entryValue.isArray()) {
169 Iterator<JsonNode> arrayItemsIterator = entryValue.elements();
170 while (arrayItemsIterator.hasNext()) {
171 foundObjects.add(arrayItemsIterator.next());
175 foundObjects.add(entry.getValue());
179 } else if (node.isArray()) {
180 Iterator<JsonNode> arrayItemsIterator = node.elements();
181 while (arrayItemsIterator.hasNext()) {
182 extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
187 public static void convertArrayIntoList(JsonNode node, Collection<JsonNode> instances) {
189 if (node.isArray()) {
190 Iterator<JsonNode> arrayItemsIterator = node.elements();
191 while (arrayItemsIterator.hasNext()) {
192 instances.add(arrayItemsIterator.next());
199 public static void extractFieldValuesFromObject(JsonNode node,
200 Collection<String> attributesToExtract, Collection<String> fieldValues) {
202 if (node.isObject()) {
204 JsonNode valueNode = null;
206 for (String attrToExtract : attributesToExtract) {
208 valueNode = node.get(attrToExtract);
210 if (valueNode != null) {
212 if (valueNode.isValueNode()) {
213 fieldValues.add(valueNode.asText());
221 public static String objToJson(Object obj) {
222 JSONObject jsonObject = new JSONObject(obj);
223 String json = jsonObject.toString();