Fix OSGi wiring issues
[ccsdk/features.git] / blueprints-processor / plugin / model-provider / src / main / java / org / onap / ccsdk / config / model / utils / TransformationUtils.java
1 /*\r
2  * Copyright © 2017-2018 AT&T Intellectual Property.\r
3  * Modifications Copyright © 2018 IBM.\r
4  * \r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  * \r
9  * http://www.apache.org/licenses/LICENSE-2.0\r
10  * \r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 \r
18 package org.onap.ccsdk.config.model.utils;\r
19 \r
20 import java.io.IOException;\r
21 import java.util.ArrayList;\r
22 import java.util.HashMap;\r
23 import java.util.Iterator;\r
24 import java.util.List;\r
25 import java.util.Map;\r
26 import java.util.Properties;\r
27 import java.util.TreeMap;\r
28 import com.att.eelf.configuration.EELFLogger;\r
29 import com.att.eelf.configuration.EELFManager;\r
30 import com.fasterxml.jackson.annotation.JsonInclude.Include;\r
31 import com.fasterxml.jackson.core.JsonProcessingException;\r
32 import com.fasterxml.jackson.core.type.TypeReference;\r
33 import com.fasterxml.jackson.databind.JsonNode;\r
34 import com.fasterxml.jackson.databind.ObjectMapper;\r
35 import com.fasterxml.jackson.databind.SerializationFeature;\r
36 import com.fasterxml.jackson.databind.node.ArrayNode;\r
37 import com.fasterxml.jackson.databind.type.CollectionType;\r
38 import com.fasterxml.jackson.module.jsonSchema.JsonSchema;\r
39 import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;\r
40 \r
41 /**\r
42  * TransformationUtils.java Purpose: Provide Configuration Generator TransformationUtils Information\r
43  *\r
44  * @version 1.0\r
45  */\r
46 public class TransformationUtils {\r
47 \r
48     private static EELFLogger logger = EELFManager.getInstance().getLogger(TransformationUtils.class);\r
49 \r
50     private TransformationUtils() {\r
51 \r
52     }\r
53 \r
54     /**\r
55      * This is a getJson method\r
56      *\r
57      * @param configparameters\r
58      * @return String\r
59      */\r
60     public static String getJson(Map<String, Object> configparameters) {\r
61         String jsonContent = null;\r
62         try {\r
63             ObjectMapper mapper = new ObjectMapper();\r
64             jsonContent = mapper.writeValueAsString(configparameters);\r
65         } catch (JsonProcessingException e) {\r
66             logger.warn(e.getMessage());\r
67         }\r
68         return jsonContent;\r
69     }\r
70 \r
71     /**\r
72      * This is a getJsonNode method\r
73      *\r
74      * @param configparameters\r
75      * @return String\r
76      */\r
77     public static JsonNode getJsonNode(Map<String, String> configparameters) {\r
78         JsonNode jsonContent = null;\r
79         try {\r
80             ObjectMapper mapper = new ObjectMapper();\r
81             jsonContent = mapper.valueToTree(configparameters);\r
82         } catch (Exception e) {\r
83             logger.warn(e.getMessage());\r
84         }\r
85         return jsonContent;\r
86     }\r
87 \r
88     /**\r
89      * This is a getJson method\r
90      *\r
91      * @param instance\r
92      * @param pretty\r
93      * @return Map<String , Object>\r
94      */\r
95     public static String getJson(Object instance, boolean pretty) {\r
96         try {\r
97             ObjectMapper mapper = new ObjectMapper();\r
98             mapper.setSerializationInclusion(Include.NON_NULL);\r
99             if (pretty) {\r
100                 mapper.enable(SerializationFeature.INDENT_OUTPUT);\r
101             }\r
102             return mapper.writeValueAsString(instance);\r
103         } catch (JsonProcessingException e) {\r
104             logger.warn(e.getMessage());\r
105         }\r
106         return null;\r
107     }\r
108 \r
109     /**\r
110      * This is a getJson method\r
111      *\r
112      * @param instance\r
113      * @return String\r
114      */\r
115     public static String getJson(Object instance) {\r
116         return getJson(instance, false);\r
117     }\r
118 \r
119     /**\r
120      * This is a getJsonNodeForobject method\r
121      *\r
122      * @param instance\r
123      * @return JsonNode\r
124      */\r
125     public static JsonNode getJsonNodeForObject(Object instance) {\r
126         try {\r
127             ObjectMapper mapper = new ObjectMapper();\r
128             mapper.setSerializationInclusion(Include.NON_NULL);\r
129             return mapper.convertValue(instance, JsonNode.class);\r
130         } catch (Exception e) {\r
131             logger.warn(e.getMessage());\r
132         }\r
133         return null;\r
134     }\r
135 \r
136     /**\r
137      * This is a getJsonNodeForString method\r
138      *\r
139      * @param content\r
140      * @return JsonNode\r
141      */\r
142     public static JsonNode getJsonNodeForString(String content) {\r
143         try {\r
144             ObjectMapper mapper = new ObjectMapper();\r
145             mapper.setSerializationInclusion(Include.NON_NULL);\r
146             return mapper.readTree(content);\r
147         } catch (Exception e) {\r
148             logger.warn(e.getMessage());\r
149         }\r
150         return null;\r
151     }\r
152 \r
153     /**\r
154      * This is a getMapfromJson method\r
155      *\r
156      * @param content\r
157      * @return Map<String , Object>\r
158      */\r
159     public static Map<String, Object> getMapfromJson(String content) {\r
160         try {\r
161             ObjectMapper mapper = new ObjectMapper();\r
162             return mapper.readValue(content, new TypeReference<Map<String, String>>() {});\r
163         } catch (Exception e) {\r
164             logger.warn("failed in getMapfromJson for the content ({}) with error message ({}).", content,\r
165                     e.getMessage());\r
166         }\r
167         return null;\r
168     }\r
169 \r
170     /**\r
171      * This is a getMapfromJson method\r
172      *\r
173      * @param content\r
174      * @return Map<String , Object>\r
175      */\r
176     public static Map<String, Object> getMapfromJsonString(String content) {\r
177         try {\r
178             ObjectMapper mapper = new ObjectMapper();\r
179             return mapper.readValue(content, new TypeReference<Map<String, Object>>() {});\r
180         } catch (Exception e) {\r
181             logger.warn("failed in getMapfromJson for the content ({}) with error message ({}).", content,\r
182                     e.getMessage());\r
183         }\r
184         return null;\r
185     }\r
186 \r
187     /**\r
188      * This is a getListfromJson method\r
189      *\r
190      * @param content\r
191      * @return Map<String , Object>\r
192      */\r
193     @SuppressWarnings("squid:S1168")\r
194     public static <T> List<T> getListfromJson(String content, Class<T> valueType) {\r
195         try {\r
196             ObjectMapper mapper = new ObjectMapper();\r
197             CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, valueType);\r
198             return mapper.readValue(content, javaType);\r
199         } catch (Exception e) {\r
200             logger.warn("failed in getListfromJson for the content ({}) with error message ({}).", content,\r
201                     e.getMessage());\r
202         }\r
203         return null;\r
204     }\r
205 \r
206     /**\r
207      * This is a getJsonSchema method\r
208      *\r
209      * @param valueType\r
210      * @return String\r
211      */\r
212     public static String getJsonSchema(Class clazz) {\r
213         try {\r
214             ObjectMapper mapper = new ObjectMapper();\r
215             JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);\r
216             JsonSchema schema = schemaGen.generateSchema(clazz);\r
217             return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);\r
218 \r
219         } catch (Exception e) {\r
220             logger.warn("failed in getJsonSchema  with error message ({}).", e.getMessage());\r
221         }\r
222         return null;\r
223     }\r
224 \r
225     /**\r
226      * This is a readValue method\r
227      *\r
228      * @param content\r
229      * @param valueType\r
230      * @return <T>\r
231      */\r
232 \r
233     public static <T> T readValue(String content, Class<T> valueType) {\r
234         try {\r
235             ObjectMapper mapper = new ObjectMapper();\r
236             return mapper.readValue(content, valueType);\r
237         } catch (Exception e) {\r
238             logger.warn("failed in readValue for the content ({}) with error message ({}).", content, e.getMessage());\r
239         }\r
240         return null;\r
241     }\r
242 \r
243     /**\r
244      * @param node\r
245      * @param valueType\r
246      * @param <T>\r
247      * @return\r
248      */\r
249     public static <T> T treeToValue(JsonNode node, Class<T> valueType) {\r
250         try {\r
251             ObjectMapper mapper = new ObjectMapper();\r
252             return mapper.treeToValue(node, valueType);\r
253         } catch (Exception e) {\r
254             logger.warn("failed in readValue for the content ({}) with error message ({}).", node, e.getMessage());\r
255         }\r
256         return null;\r
257     }\r
258 \r
259     /**\r
260      * @param node\r
261      * @param valueType\r
262      * @param <T>\r
263      * @return List<T>\r
264      */\r
265     public static <T> List<T> treeToListValue(JsonNode node, Class<T> valueType) {\r
266         List<T> resultList = new ArrayList<>();\r
267         if (node instanceof ArrayNode) {\r
268             for (JsonNode subnode : node) {\r
269                 if (subnode != null) {\r
270                     resultList.add(treeToValue(subnode, valueType));\r
271                 }\r
272             }\r
273         }\r
274         return resultList;\r
275     }\r
276 \r
277     /**\r
278      * This is a removeJsonNullNode method\r
279      *\r
280      * @param node\r
281      */\r
282     public static void removeJsonNullNode(JsonNode node) {\r
283         Iterator<JsonNode> it = node.iterator();\r
284         while (it.hasNext()) {\r
285             JsonNode child = it.next();\r
286             if (child.isNull()) {\r
287                 it.remove();\r
288             } else {\r
289                 removeJsonNullNode(child);\r
290             }\r
291         }\r
292     }\r
293 \r
294     public static void printProperty(Properties property) {\r
295         if (property != null) {\r
296             Map<String, String> sortedMap = new TreeMap(property);\r
297             StringBuilder buffer = new StringBuilder();\r
298             sortedMap.entrySet().forEach(message -> {\r
299                 buffer.append("\n" + message.toString());\r
300             });\r
301             logger.debug("Property : ({})", buffer);\r
302         }\r
303     }\r
304 \r
305     public static void printMap(Map<String, String> map) {\r
306         if (map != null) {\r
307             Map<String, String> sortedMap = new TreeMap(map);\r
308             StringBuilder buffer = new StringBuilder();\r
309             sortedMap.entrySet().forEach(message -> {\r
310                 buffer.append("\n" + message.toString());\r
311             });\r
312             logger.debug("Map: ({})", buffer);\r
313         }\r
314     }\r
315 \r
316     @SuppressWarnings("squid:S00112")\r
317     public static Map<String, String> convertJson2RootProperties(Map<String, String> context, String jsonContent)\r
318             throws Exception {\r
319         if (context == null) {\r
320             context = new HashMap<>();\r
321         }\r
322 \r
323         ObjectMapper mapper = new ObjectMapper();\r
324         JsonNode rootArray = mapper.readTree(jsonContent);\r
325         return convertJson2RootProperties(context, rootArray);\r
326     }\r
327 \r
328     public static Map<String, String> convertJson2RootProperties(Map<String, String> context, JsonNode rootArray) {\r
329         Map<String, String> sortedMap = null;\r
330 \r
331         if (context == null) {\r
332             context = new HashMap<>();\r
333         }\r
334         if (rootArray != null) {\r
335             Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
336             while (fields.hasNext()) {\r
337                 Map.Entry<String, JsonNode> entry = fields.next();\r
338                 if (entry != null && entry.getValue() != null) {\r
339                     if (entry.getValue().isTextual()) {\r
340                         context.put(entry.getKey(), entry.getValue().textValue());\r
341                     } else {\r
342                         context.put(entry.getKey(), entry.getValue().toString());\r
343                     }\r
344                 }\r
345             }\r
346         }\r
347         sortedMap = new TreeMap<>(context);\r
348         return sortedMap;\r
349     }\r
350 \r
351     @SuppressWarnings("squid:S00112")\r
352     public static Map<String, String> convertJson2Properties(Map<String, String> context, String jsonContent,\r
353             List<String> blockKeys) throws Exception {\r
354         Map<String, String> sortedMap = null;\r
355 \r
356         if (context == null) {\r
357             context = new HashMap<>();\r
358         }\r
359 \r
360         ObjectMapper mapper = new ObjectMapper();\r
361         JsonNode rootArray = mapper.readTree(jsonContent);\r
362 \r
363         if (rootArray != null) {\r
364             Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
365             while (fields.hasNext()) {\r
366                 Map.Entry<String, JsonNode> entry = fields.next();\r
367                 processJsonNode(context, blockKeys, entry.getKey(), entry.getValue());\r
368             }\r
369         }\r
370         sortedMap = new TreeMap<>(context);\r
371         return sortedMap;\r
372     }\r
373 \r
374     public static Map<String, String> convertJson2Properties(Map<String, String> context, JsonNode rootArray,\r
375             List<String> blockKeys) throws IOException {\r
376         Map<String, String> sortedMap = null;\r
377 \r
378         if (context == null) {\r
379             context = new HashMap<>();\r
380         }\r
381 \r
382         if (blockKeys == null) {\r
383             blockKeys = new ArrayList<>();\r
384         }\r
385 \r
386         if (rootArray != null) {\r
387             Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
388             while (fields.hasNext()) {\r
389                 Map.Entry<String, JsonNode> entry = fields.next();\r
390                 processJsonNode(context, blockKeys, entry.getKey(), entry.getValue());\r
391             }\r
392         }\r
393         sortedMap = new TreeMap<>(context);\r
394         return sortedMap;\r
395     }\r
396 \r
397     private static void processJsonNode(Map<String, String> propertyMap, List<String> blockKeys, String nodeName,\r
398             JsonNode node) throws IOException {\r
399 \r
400         logger.trace("Block Key ({})", nodeName);\r
401         if (node == null) {\r
402             return;\r
403         }\r
404 \r
405         String keyName = null;\r
406         if (blockKeys != null && blockKeys.contains(nodeName)) {\r
407             if (node.isArray() || node.isObject()) {\r
408                 propertyMap.put(nodeName, node.toString());\r
409             } else {\r
410                 propertyMap.put(nodeName, node.asText());\r
411             }\r
412         } else if (node.isArray()) {\r
413             for (int i = 0; i < node.size(); i++) {\r
414                 keyName = nodeName + "[" + i + "]";\r
415                 processJsonNode(propertyMap, blockKeys, keyName, node.get(i));\r
416             }\r
417         } else if (node.isObject()) {\r
418             Iterator<Map.Entry<String, JsonNode>> fields = node.fields();\r
419             while (fields.hasNext()) {\r
420                 Map.Entry<String, JsonNode> entry = fields.next();\r
421                 keyName = nodeName + "." + entry.getKey();\r
422                 processJsonNode(propertyMap, blockKeys, keyName, entry.getValue());\r
423             }\r
424         } else {\r
425             propertyMap.put(nodeName, node.asText());\r
426         }\r
427     }\r
428 \r
429 }\r