6321a8385799236937353810294eeae32cb8ab24
[ccsdk/cds.git] / components / core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / utils / JacksonUtils.kt
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 package org.onap.ccsdk.apps.controllerblueprints.core.utils\r
18 \r
19 import com.att.eelf.configuration.EELFLogger\r
20 import com.att.eelf.configuration.EELFManager\r
21 import com.fasterxml.jackson.annotation.JsonInclude\r
22 import com.fasterxml.jackson.core.type.TypeReference\r
23 import com.fasterxml.jackson.databind.JsonNode\r
24 import com.fasterxml.jackson.databind.SerializationFeature\r
25 import com.fasterxml.jackson.databind.node.ArrayNode\r
26 import com.fasterxml.jackson.databind.node.NullNode\r
27 import com.fasterxml.jackson.databind.node.ObjectNode\r
28 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper\r
29 import kotlinx.coroutines.Dispatchers\r
30 import kotlinx.coroutines.async\r
31 import kotlinx.coroutines.runBlocking\r
32 import kotlinx.coroutines.withContext\r
33 import org.apache.commons.io.IOUtils\r
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants\r
35 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException\r
36 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes\r
37 import java.io.File\r
38 import java.nio.charset.Charset\r
39 \r
40 /**\r
41  *\r
42  *\r
43  * @author Brinda Santh\r
44  */\r
45 class JacksonUtils {\r
46     companion object {\r
47         private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())\r
48         inline fun <reified T : Any> readValue(content: String): T =\r
49                 jacksonObjectMapper().readValue(content, T::class.java)\r
50 \r
51         fun <T> readValue(content: String, valueType: Class<T>): T? {\r
52             return jacksonObjectMapper().readValue(content, valueType)\r
53         }\r
54 \r
55         fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {\r
56             return jacksonObjectMapper().treeToValue(node, valueType)\r
57         }\r
58 \r
59         fun removeJsonNullNode(node: JsonNode) {\r
60             val it = node.iterator()\r
61             while (it.hasNext()) {\r
62                 val child = it.next()\r
63                 if (child.isNull) {\r
64                     it.remove()\r
65                 } else {\r
66                     removeJsonNullNode(child)\r
67                 }\r
68             }\r
69         }\r
70 \r
71         fun getContent(fileName: String): String = runBlocking {\r
72             async {\r
73                 try {\r
74                     File(fileName).readText(Charsets.UTF_8)\r
75                 } catch (e: Exception) {\r
76                     throw BluePrintException("couldn't get file ($fileName) content : ${e.message}")\r
77                 }\r
78             }.await()\r
79         }\r
80 \r
81         fun getClassPathFileContent(fileName: String): String {\r
82             return runBlocking {\r
83                 withContext(Dispatchers.Default) {\r
84                     IOUtils.toString(JacksonUtils::class.java.classLoader\r
85                             .getResourceAsStream(fileName), Charset.defaultCharset())\r
86                 }\r
87             }\r
88         }\r
89 \r
90         fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {\r
91             val content: String = getContent(fileName)\r
92             return readValue(content, valueType)\r
93         }\r
94 \r
95         fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {\r
96             val content: String = getClassPathFileContent(fileName)\r
97             return readValue(content, valueType)\r
98         }\r
99 \r
100         fun jsonNodeFromObject(from: kotlin.Any): JsonNode {\r
101             return jacksonObjectMapper().convertValue(from, JsonNode::class.java)\r
102         }\r
103 \r
104         fun jsonNodeFromClassPathFile(fileName: String): JsonNode {\r
105             val content: String = getClassPathFileContent(fileName)\r
106             return jsonNode(content)\r
107         }\r
108 \r
109         fun jsonNodeFromFile(fileName: String): JsonNode {\r
110             val content: String = getContent(fileName)\r
111             return jsonNode(content)\r
112         }\r
113 \r
114         fun jsonNode(content: String): JsonNode {\r
115             return jacksonObjectMapper().readTree(content)\r
116         }\r
117 \r
118         fun getJson(any: kotlin.Any): String {\r
119             return getJson(any, false)\r
120         }\r
121 \r
122         fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {\r
123             val wrapperMap = hashMapOf<String, Any>()\r
124             wrapperMap[wrapper] = any\r
125             return getJson(wrapperMap, pretty)\r
126         }\r
127 \r
128         fun getJson(any: kotlin.Any, pretty: Boolean = false): String {\r
129             val objectMapper = jacksonObjectMapper()\r
130             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
131             if (pretty) {\r
132                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
133             }\r
134             return objectMapper.writeValueAsString(any)\r
135         }\r
136 \r
137         fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {\r
138             val objectMapper = jacksonObjectMapper()\r
139             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
140             if (pretty) {\r
141                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
142             }\r
143             return objectMapper.valueToTree(any)\r
144         }\r
145 \r
146         fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T>? {\r
147             return getListFromJson(node.toString(), valueType)\r
148         }\r
149 \r
150         fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {\r
151             val objectMapper = jacksonObjectMapper()\r
152             val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)\r
153             return objectMapper.readValue<List<T>>(content, javaType)\r
154         }\r
155 \r
156         fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {\r
157             val content: String = getContent(fileName)\r
158             return getListFromJson(content, valueType)\r
159         }\r
160 \r
161         fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {\r
162             val content: String = getClassPathFileContent(fileName)\r
163             return getListFromJson(content, valueType)\r
164         }\r
165 \r
166         fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {\r
167             val objectMapper = jacksonObjectMapper()\r
168             val typeRef = object : TypeReference<MutableMap<String, T>>() {}\r
169             return objectMapper.readValue(content, typeRef)\r
170         }\r
171 \r
172         fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T>? {\r
173             val content: String = getContent(fileName)\r
174             return getMapFromJson(content, valueType)\r
175         }\r
176 \r
177         fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {\r
178             if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
179                 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)\r
180             } else if (BluePrintTypes.validCollectionTypes().contains(type)) {\r
181                 return checkJsonNodeValueOfCollectionType(type, jsonNode)\r
182             }\r
183             return false\r
184         }\r
185 \r
186         fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {\r
187             when (primitiveType) {\r
188                 BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual\r
189                 BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean\r
190                 BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt\r
191                 BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble\r
192                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual\r
193                 else -> return false\r
194             }\r
195         }\r
196 \r
197         fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {\r
198             when (type) {\r
199                 BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray\r
200                 BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode\r
201                 else -> return false\r
202             }\r
203         }\r
204 \r
205         fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {\r
206             when (primitiveType) {\r
207                 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean)\r
208                 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int)\r
209                 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float)\r
210                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String)\r
211                 else -> objectNode.put(key, value as String)\r
212             }\r
213         }\r
214 \r
215         fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {\r
216             when (primitiveType) {\r
217                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)\r
218                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)\r
219                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)\r
220                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)\r
221                 else -> arrayNode.add(value as String)\r
222             }\r
223         }\r
224 \r
225         fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {\r
226             when (primitiveType) {\r
227                 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)\r
228                 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)\r
229                 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)\r
230                 else -> objectNode.put(key, "")\r
231             }\r
232         }\r
233 \r
234         fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {\r
235             when (primitiveType) {\r
236                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)\r
237                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)\r
238                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)\r
239                 else -> arrayNode.add("")\r
240             }\r
241         }\r
242 \r
243         fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {\r
244             if (nodeValue == null || nodeValue is NullNode) {\r
245                 objectNode.set(key, nodeValue)\r
246             } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
247                 populatePrimitiveValues(key, nodeValue, type, objectNode)\r
248             } else {\r
249                 objectNode.set(key, nodeValue)\r
250             }\r
251         }\r
252 \r
253         fun convertPrimitiveResourceValue(type: String, value: String): JsonNode? {\r
254             when (type) {\r
255                 BluePrintConstants.DATA_TYPE_BOOLEAN -> return JacksonUtils.getJsonNode(value as Boolean)\r
256                 BluePrintConstants.DATA_TYPE_INTEGER -> return JacksonUtils.getJsonNode(value as Int)\r
257                 BluePrintConstants.DATA_TYPE_FLOAT -> return JacksonUtils.getJsonNode(value as Float)\r
258                 else -> return JacksonUtils.getJsonNode(value)\r
259             }\r
260         }\r
261     }\r
262 }