b30c9f9c645909a2e9345c6e824b49d979790de0
[aai/data-router.git] / src / main / java / org / openecomp / datarouter / util / RouterServiceUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * DataRouter
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.datarouter.util;
26
27 import com.fasterxml.jackson.databind.JsonNode;
28
29 import org.json.JSONObject;
30
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;
36 import java.util.Map;
37
38 public class RouterServiceUtil {
39
40   public static Map<String, String> parseJsonPayloadIntoMap(String jsonPayload) {
41
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);
48       map.put(key, value);
49     }
50     return map;
51   }
52
53   public static String getNodeFieldAsText(JsonNode node, String fieldName) {
54
55     String fieldValue = null;
56
57     JsonNode valueNode = node.get(fieldName);
58
59     if (valueNode != null) {
60       fieldValue = valueNode.asText();
61     }
62
63     return fieldValue;
64   }
65
66   public static String concatArray(List<String> list) {
67     return concatArray(list, " ");
68   }
69
70   public static String concatArray(List<String> list, String delimiter) {
71
72     if (list == null || list.size() == 0) {
73       return "";
74     }
75
76     StringBuilder result = new StringBuilder(64);
77
78     boolean firstValue = true;
79
80     for (String item : list) {
81
82       if (firstValue) {
83         result.append(item);
84         firstValue = false;
85       } else {
86         result.append(delimiter).append(item);
87       }
88     }
89
90     return result.toString();
91
92   }
93
94   public static String concatArray(String[] values) {
95
96     if (values == null || values.length == 0) {
97       return "";
98     }
99
100     StringBuilder result = new StringBuilder(64);
101
102     boolean firstValue = true;
103
104     for (String item : values) {
105
106       if (firstValue) {
107         result.append(item);
108         firstValue = false;
109       } else {
110         result.append(".").append(item);
111       }
112
113     }
114
115     return result.toString();
116
117   }
118
119   public static String recursivelyLookupJsonPayload(JsonNode node, String key) {
120     String value = null;
121     if (node.isObject()) {
122       Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
123
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);
128           if (value != null) {
129             return value;
130           }
131         }
132
133         String name = entry.getKey();
134         if (name.equalsIgnoreCase(key)) {
135           return entry.getValue().asText();
136         }
137       }
138     } else if (node.isArray()) {
139       Iterator<JsonNode> arrayItemsIterator = node.elements();
140       while (arrayItemsIterator.hasNext()) {
141         value = recursivelyLookupJsonPayload(arrayItemsIterator.next(), key);
142         if (value != null) {
143           return value;
144         }
145       }
146     }
147     return value;
148   }
149
150   public static void extractObjectsByKey(JsonNode node, String searchKey,
151       Collection<JsonNode> foundObjects) {
152
153     if (node.isObject()) {
154       Iterator<Map.Entry<String, JsonNode>> nodeIterator = node.fields();
155
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);
160         }
161
162         String name = entry.getKey();
163         if (name.equalsIgnoreCase(searchKey)) {
164
165           JsonNode entryValue = entry.getValue();
166
167           if (entryValue.isArray()) {
168
169             Iterator<JsonNode> arrayItemsIterator = entryValue.elements();
170             while (arrayItemsIterator.hasNext()) {
171               foundObjects.add(arrayItemsIterator.next());
172             }
173
174           } else {
175             foundObjects.add(entry.getValue());
176           }
177         }
178       }
179     } else if (node.isArray()) {
180       Iterator<JsonNode> arrayItemsIterator = node.elements();
181       while (arrayItemsIterator.hasNext()) {
182         extractObjectsByKey(arrayItemsIterator.next(), searchKey, foundObjects);
183       }
184     }
185   }
186
187   public static void convertArrayIntoList(JsonNode node, Collection<JsonNode> instances) {
188
189     if (node.isArray()) {
190       Iterator<JsonNode> arrayItemsIterator = node.elements();
191       while (arrayItemsIterator.hasNext()) {
192         instances.add(arrayItemsIterator.next());
193       }
194     } else {
195       instances.add(node);
196     }
197   }
198
199   public static void extractFieldValuesFromObject(JsonNode node,
200       Collection<String> attributesToExtract, Collection<String> fieldValues) {
201
202     if (node.isObject()) {
203
204       JsonNode valueNode = null;
205
206       for (String attrToExtract : attributesToExtract) {
207
208         valueNode = node.get(attrToExtract);
209
210         if (valueNode != null) {
211
212           if (valueNode.isValueNode()) {
213             fieldValues.add(valueNode.asText());
214           }
215         }
216       }
217     }
218   }
219
220
221   public static String objToJson(Object obj) {
222     JSONObject jsonObject = new JSONObject(obj);
223     String json = jsonObject.toString();
224     return json;
225   }
226 }