2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
18 package org.onap.ccsdk.config.model.utils;
\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
42 * TransformationUtils.java Purpose: Provide Configuration Generator TransformationUtils Information
\r
46 public class TransformationUtils {
\r
48 private static EELFLogger logger = EELFManager.getInstance().getLogger(TransformationUtils.class);
\r
50 private TransformationUtils() {
\r
55 * This is a getJson method
\r
57 * @param configparameters
\r
60 public static String getJson(Map<String, Object> configparameters) {
\r
61 String jsonContent = null;
\r
63 ObjectMapper mapper = new ObjectMapper();
\r
64 jsonContent = mapper.writeValueAsString(configparameters);
\r
65 } catch (JsonProcessingException e) {
\r
66 logger.warn(e.getMessage());
\r
72 * This is a getJsonNode method
\r
74 * @param configparameters
\r
77 public static JsonNode getJsonNode(Map<String, String> configparameters) {
\r
78 JsonNode jsonContent = null;
\r
80 ObjectMapper mapper = new ObjectMapper();
\r
81 jsonContent = mapper.valueToTree(configparameters);
\r
82 } catch (Exception e) {
\r
83 logger.warn(e.getMessage());
\r
89 * This is a getJson method
\r
93 * @return Map<String , Object>
\r
95 public static String getJson(Object instance, boolean pretty) {
\r
97 ObjectMapper mapper = new ObjectMapper();
\r
98 mapper.setSerializationInclusion(Include.NON_NULL);
\r
100 mapper.enable(SerializationFeature.INDENT_OUTPUT);
\r
102 return mapper.writeValueAsString(instance);
\r
103 } catch (JsonProcessingException e) {
\r
104 logger.warn(e.getMessage());
\r
110 * This is a getJson method
\r
115 public static String getJson(Object instance) {
\r
116 return getJson(instance, false);
\r
120 * This is a getJsonNodeForobject method
\r
125 public static JsonNode getJsonNodeForObject(Object instance) {
\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
137 * This is a getJsonNodeForString method
\r
142 public static JsonNode getJsonNodeForString(String content) {
\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
154 * This is a getMapfromJson method
\r
157 * @return Map<String , Object>
\r
159 public static Map<String, Object> getMapfromJson(String content) {
\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
171 * This is a getMapfromJson method
\r
174 * @return Map<String , Object>
\r
176 public static Map<String, Object> getMapfromJsonString(String content) {
\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
188 * This is a getListfromJson method
\r
191 * @return Map<String , Object>
\r
193 @SuppressWarnings("squid:S1168")
\r
194 public static <T> List<T> getListfromJson(String content, Class<T> valueType) {
\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
207 * This is a getJsonSchema method
\r
212 public static String getJsonSchema(Class clazz) {
\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
219 } catch (Exception e) {
\r
220 logger.warn("failed in getJsonSchema with error message ({}).", e.getMessage());
\r
226 * This is a readValue method
\r
233 public static <T> T readValue(String content, Class<T> valueType) {
\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
249 public static <T> T treeToValue(JsonNode node, Class<T> valueType) {
\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
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
278 * This is a removeJsonNullNode method
\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
289 removeJsonNullNode(child);
\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
301 logger.debug("Property : ({})", buffer);
\r
305 public static void printMap(Map<String, String> map) {
\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
312 logger.debug("Map: ({})", buffer);
\r
316 @SuppressWarnings("squid:S00112")
\r
317 public static Map<String, String> convertJson2RootProperties(Map<String, String> context, String jsonContent)
\r
319 if (context == null) {
\r
320 context = new HashMap<>();
\r
323 ObjectMapper mapper = new ObjectMapper();
\r
324 JsonNode rootArray = mapper.readTree(jsonContent);
\r
325 return convertJson2RootProperties(context, rootArray);
\r
328 public static Map<String, String> convertJson2RootProperties(Map<String, String> context, JsonNode rootArray) {
\r
329 Map<String, String> sortedMap = null;
\r
331 if (context == null) {
\r
332 context = new HashMap<>();
\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
342 context.put(entry.getKey(), entry.getValue().toString());
\r
347 sortedMap = new TreeMap<>(context);
\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
356 if (context == null) {
\r
357 context = new HashMap<>();
\r
360 ObjectMapper mapper = new ObjectMapper();
\r
361 JsonNode rootArray = mapper.readTree(jsonContent);
\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
370 sortedMap = new TreeMap<>(context);
\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
378 if (context == null) {
\r
379 context = new HashMap<>();
\r
382 if (blockKeys == null) {
\r
383 blockKeys = new ArrayList<>();
\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
393 sortedMap = new TreeMap<>(context);
\r
397 private static void processJsonNode(Map<String, String> propertyMap, List<String> blockKeys, String nodeName,
\r
398 JsonNode node) throws IOException {
\r
400 logger.trace("Block Key ({})", nodeName);
\r
401 if (node == null) {
\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
410 propertyMap.put(nodeName, node.asText());
\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
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
425 propertyMap.put(nodeName, node.asText());
\r