+++ /dev/null
-/*\r
- * Copyright © 2017-2018 AT&T Intellectual Property.\r
- * Modifications Copyright © 2018 IBM.\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-package org.onap.ccsdk.features.model.utils;\r
-\r
-import java.io.IOException;\r
-import java.util.ArrayList;\r
-import java.util.HashMap;\r
-import java.util.Iterator;\r
-import java.util.List;\r
-import java.util.Map;\r
-import java.util.Properties;\r
-import java.util.TreeMap;\r
-import com.att.eelf.configuration.EELFLogger;\r
-import com.att.eelf.configuration.EELFManager;\r
-import com.fasterxml.jackson.annotation.JsonInclude.Include;\r
-import com.fasterxml.jackson.core.JsonProcessingException;\r
-import com.fasterxml.jackson.core.type.TypeReference;\r
-import com.fasterxml.jackson.databind.JsonNode;\r
-import com.fasterxml.jackson.databind.ObjectMapper;\r
-import com.fasterxml.jackson.databind.SerializationFeature;\r
-import com.fasterxml.jackson.databind.node.ArrayNode;\r
-import com.fasterxml.jackson.databind.type.CollectionType;\r
-import com.fasterxml.jackson.module.jsonSchema.JsonSchema;\r
-import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;\r
-\r
-/**\r
- * TransformationUtils.java Purpose: Provide Configuration Generator TransformationUtils Information\r
- *\r
- * @version 1.0\r
- */\r
-public class TransformationUtils {\r
-\r
- private static EELFLogger logger = EELFManager.getInstance().getLogger(TransformationUtils.class);\r
-\r
- private TransformationUtils() {\r
-\r
- }\r
-\r
- /**\r
- * This is a getJson method\r
- *\r
- * @param configparameters\r
- * @return String\r
- */\r
- public static String getJson(Map<String, Object> configparameters) {\r
- String jsonContent = null;\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- jsonContent = mapper.writeValueAsString(configparameters);\r
- } catch (JsonProcessingException e) {\r
- logger.warn(e.getMessage());\r
- }\r
- return jsonContent;\r
- }\r
-\r
- /**\r
- * This is a getJsonNode method\r
- *\r
- * @param configparameters\r
- * @return String\r
- */\r
- public static JsonNode getJsonNode(Map<String, String> configparameters) {\r
- JsonNode jsonContent = null;\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- jsonContent = mapper.valueToTree(configparameters);\r
- } catch (Exception e) {\r
- logger.warn(e.getMessage());\r
- }\r
- return jsonContent;\r
- }\r
-\r
- /**\r
- * This is a getJson method\r
- *\r
- * @param instance\r
- * @param pretty\r
- * @return Map<String , Object>\r
- */\r
- public static String getJson(Object instance, boolean pretty) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- mapper.setSerializationInclusion(Include.NON_NULL);\r
- if (pretty) {\r
- mapper.enable(SerializationFeature.INDENT_OUTPUT);\r
- }\r
- return mapper.writeValueAsString(instance);\r
- } catch (JsonProcessingException e) {\r
- logger.warn(e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getJson method\r
- *\r
- * @param instance\r
- * @return String\r
- */\r
- public static String getJson(Object instance) {\r
- return getJson(instance, false);\r
- }\r
-\r
- /**\r
- * This is a getJsonNodeForobject method\r
- *\r
- * @param instance\r
- * @return JsonNode\r
- */\r
- public static JsonNode getJsonNodeForObject(Object instance) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- mapper.setSerializationInclusion(Include.NON_NULL);\r
- return mapper.convertValue(instance, JsonNode.class);\r
- } catch (Exception e) {\r
- logger.warn(e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getJsonNodeForString method\r
- *\r
- * @param content\r
- * @return JsonNode\r
- */\r
- public static JsonNode getJsonNodeForString(String content) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- mapper.setSerializationInclusion(Include.NON_NULL);\r
- return mapper.readTree(content);\r
- } catch (Exception e) {\r
- logger.warn(e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getMapfromJson method\r
- *\r
- * @param content\r
- * @return Map<String , Object>\r
- */\r
- public static Map<String, Object> getMapfromJson(String content) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- return mapper.readValue(content, new TypeReference<Map<String, String>>() {});\r
- } catch (Exception e) {\r
- logger.warn("failed in getMapfromJson for the content ({}) with error message ({}).", content,\r
- e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getMapfromJson method\r
- *\r
- * @param content\r
- * @return Map<String , Object>\r
- */\r
- public static Map<String, Object> getMapfromJsonString(String content) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- return mapper.readValue(content, new TypeReference<Map<String, Object>>() {});\r
- } catch (Exception e) {\r
- logger.warn("failed in getMapfromJson for the content ({}) with error message ({}).", content,\r
- e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getListfromJson method\r
- *\r
- * @param content\r
- * @return Map<String , Object>\r
- */\r
- @SuppressWarnings("squid:S1168")\r
- public static <T> List<T> getListfromJson(String content, Class<T> valueType) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- CollectionType javaType = mapper.getTypeFactory().constructCollectionType(List.class, valueType);\r
- return mapper.readValue(content, javaType);\r
- } catch (Exception e) {\r
- logger.warn("failed in getListfromJson for the content ({}) with error message ({}).", content,\r
- e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a getJsonSchema method\r
- *\r
- * @param valueType\r
- * @return String\r
- */\r
- public static String getJsonSchema(Class clazz) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- JsonSchemaGenerator schemaGen = new JsonSchemaGenerator(mapper);\r
- JsonSchema schema = schemaGen.generateSchema(clazz);\r
- return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);\r
-\r
- } catch (Exception e) {\r
- logger.warn("failed in getJsonSchema with error message ({}).", e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * This is a readValue method\r
- *\r
- * @param content\r
- * @param valueType\r
- * @return <T>\r
- */\r
-\r
- public static <T> T readValue(String content, Class<T> valueType) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- return mapper.readValue(content, valueType);\r
- } catch (Exception e) {\r
- logger.warn("failed in readValue for the content ({}) with error message ({}).", content, e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * @param node\r
- * @param valueType\r
- * @param <T>\r
- * @return\r
- */\r
- public static <T> T treeToValue(JsonNode node, Class<T> valueType) {\r
- try {\r
- ObjectMapper mapper = new ObjectMapper();\r
- return mapper.treeToValue(node, valueType);\r
- } catch (Exception e) {\r
- logger.warn("failed in readValue for the content ({}) with error message ({}).", node, e.getMessage());\r
- }\r
- return null;\r
- }\r
-\r
- /**\r
- * @param node\r
- * @param valueType\r
- * @param <T>\r
- * @return List<T>\r
- */\r
- public static <T> List<T> treeToListValue(JsonNode node, Class<T> valueType) {\r
- List<T> resultList = new ArrayList<>();\r
- if (node instanceof ArrayNode) {\r
- for (JsonNode subnode : node) {\r
- if (subnode != null) {\r
- resultList.add(treeToValue(subnode, valueType));\r
- }\r
- }\r
- }\r
- return resultList;\r
- }\r
-\r
- /**\r
- * This is a removeJsonNullNode method\r
- *\r
- * @param node\r
- */\r
- public static void removeJsonNullNode(JsonNode node) {\r
- Iterator<JsonNode> it = node.iterator();\r
- while (it.hasNext()) {\r
- JsonNode child = it.next();\r
- if (child.isNull()) {\r
- it.remove();\r
- } else {\r
- removeJsonNullNode(child);\r
- }\r
- }\r
- }\r
-\r
- public static void printProperty(Properties property) {\r
- if (property != null) {\r
- Map<String, String> sortedMap = new TreeMap(property);\r
- StringBuilder buffer = new StringBuilder();\r
- sortedMap.entrySet().forEach(message -> {\r
- buffer.append("\n" + message.toString());\r
- });\r
- logger.debug("Property : ({})", buffer);\r
- }\r
- }\r
-\r
- public static void printMap(Map<String, String> map) {\r
- if (map != null) {\r
- Map<String, String> sortedMap = new TreeMap(map);\r
- StringBuilder buffer = new StringBuilder();\r
- sortedMap.entrySet().forEach(message -> {\r
- buffer.append("\n" + message.toString());\r
- });\r
- logger.debug("Map: ({})", buffer);\r
- }\r
- }\r
-\r
- @SuppressWarnings("squid:S00112")\r
- public static Map<String, String> convertJson2RootProperties(Map<String, String> context, String jsonContent)\r
- throws Exception {\r
- if (context == null) {\r
- context = new HashMap<>();\r
- }\r
-\r
- ObjectMapper mapper = new ObjectMapper();\r
- JsonNode rootArray = mapper.readTree(jsonContent);\r
- return convertJson2RootProperties(context, rootArray);\r
- }\r
-\r
- public static Map<String, String> convertJson2RootProperties(Map<String, String> context, JsonNode rootArray) {\r
- Map<String, String> sortedMap = null;\r
-\r
- if (context == null) {\r
- context = new HashMap<>();\r
- }\r
- if (rootArray != null) {\r
- Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
- while (fields.hasNext()) {\r
- Map.Entry<String, JsonNode> entry = fields.next();\r
- if (entry != null && entry.getValue() != null) {\r
- if (entry.getValue().isTextual()) {\r
- context.put(entry.getKey(), entry.getValue().textValue());\r
- } else {\r
- context.put(entry.getKey(), entry.getValue().toString());\r
- }\r
- }\r
- }\r
- }\r
- sortedMap = new TreeMap<>(context);\r
- return sortedMap;\r
- }\r
-\r
- @SuppressWarnings("squid:S00112")\r
- public static Map<String, String> convertJson2Properties(Map<String, String> context, String jsonContent,\r
- List<String> blockKeys) throws Exception {\r
- Map<String, String> sortedMap = null;\r
-\r
- if (context == null) {\r
- context = new HashMap<>();\r
- }\r
-\r
- ObjectMapper mapper = new ObjectMapper();\r
- JsonNode rootArray = mapper.readTree(jsonContent);\r
-\r
- if (rootArray != null) {\r
- Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
- while (fields.hasNext()) {\r
- Map.Entry<String, JsonNode> entry = fields.next();\r
- processJsonNode(context, blockKeys, entry.getKey(), entry.getValue());\r
- }\r
- }\r
- sortedMap = new TreeMap<>(context);\r
- return sortedMap;\r
- }\r
-\r
- public static Map<String, String> convertJson2Properties(Map<String, String> context, JsonNode rootArray,\r
- List<String> blockKeys) throws IOException {\r
- Map<String, String> sortedMap = null;\r
-\r
- if (context == null) {\r
- context = new HashMap<>();\r
- }\r
-\r
- if (blockKeys == null) {\r
- blockKeys = new ArrayList<>();\r
- }\r
-\r
- if (rootArray != null) {\r
- Iterator<Map.Entry<String, JsonNode>> fields = rootArray.fields();\r
- while (fields.hasNext()) {\r
- Map.Entry<String, JsonNode> entry = fields.next();\r
- processJsonNode(context, blockKeys, entry.getKey(), entry.getValue());\r
- }\r
- }\r
- sortedMap = new TreeMap<>(context);\r
- return sortedMap;\r
- }\r
-\r
- private static void processJsonNode(Map<String, String> propertyMap, List<String> blockKeys, String nodeName,\r
- JsonNode node) throws IOException {\r
-\r
- logger.trace("Block Key ({})", nodeName);\r
- if (node == null) {\r
- return;\r
- }\r
-\r
- String keyName = null;\r
- if (blockKeys != null && blockKeys.contains(nodeName)) {\r
- if (node.isArray() || node.isObject()) {\r
- propertyMap.put(nodeName, node.toString());\r
- } else {\r
- propertyMap.put(nodeName, node.asText());\r
- }\r
- } else if (node.isArray()) {\r
- for (int i = 0; i < node.size(); i++) {\r
- keyName = nodeName + "[" + i + "]";\r
- processJsonNode(propertyMap, blockKeys, keyName, node.get(i));\r
- }\r
- } else if (node.isObject()) {\r
- Iterator<Map.Entry<String, JsonNode>> fields = node.fields();\r
- while (fields.hasNext()) {\r
- Map.Entry<String, JsonNode> entry = fields.next();\r
- keyName = nodeName + "." + entry.getKey();\r
- processJsonNode(propertyMap, blockKeys, keyName, entry.getValue());\r
- }\r
- } else {\r
- propertyMap.put(nodeName, node.asText());\r
- }\r
- }\r
-\r
-}\r