Merge "Resource resolution should return a string"
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / utils / JacksonUtils.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2018-2019 IBM.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 package org.onap.ccsdk.cds.controllerblueprints.core.utils
18
19 import com.fasterxml.jackson.annotation.JsonInclude
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.SerializationFeature
22 import com.fasterxml.jackson.databind.node.*
23 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.runBlocking
26 import kotlinx.coroutines.withContext
27 import org.apache.commons.io.IOUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.*
29 import java.io.File
30 import java.io.InputStream
31 import java.nio.charset.Charset
32
33 /**
34  *
35  *
36  * @author Brinda Santh
37  */
38 class JacksonUtils {
39     companion object {
40
41         val objectMapper = jacksonObjectMapper()
42
43         inline fun <reified T : Any> readValue(content: String): T =
44                 objectMapper.readValue(content, T::class.java)
45
46         inline fun <reified T : Any> readValue(stream: InputStream): T =
47                 objectMapper.readValue(stream, T::class.java)
48
49         fun <T> readValue(content: String, valueType: Class<T>): T? {
50             return objectMapper.readValue(content, valueType)
51         }
52
53         fun <T> readValue(stream: InputStream, valueType: Class<T>): T? {
54             return objectMapper.readValue(stream, valueType)
55         }
56
57         fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {
58             return objectMapper.treeToValue(node, valueType)
59         }
60
61         fun getContent(fileName: String): String = runBlocking {
62             try {
63                 normalizedFile(fileName).readNBText()
64             } catch (e: Exception) {
65                 throw BluePrintException("couldn't get file ($fileName) content : ${e.message}")
66             }
67         }
68
69         fun getClassPathFileContent(fileName: String): String {
70             return runBlocking {
71                 withContext(Dispatchers.Default) {
72                     IOUtils.toString(JacksonUtils::class.java.classLoader
73                             .getResourceAsStream(fileName), Charset.defaultCharset())
74                 }
75             }
76         }
77
78         fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
79             val content: String = getContent(fileName)
80             return readValue(content, valueType)
81         }
82
83         fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
84             val content: String = getClassPathFileContent(fileName)
85             return readValue(content, valueType)
86         }
87
88         fun objectNodeFromObject(from: kotlin.Any): ObjectNode {
89             return objectMapper.convertValue(from, ObjectNode::class.java)
90         }
91
92         fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
93             return objectMapper.convertValue(from, JsonNode::class.java)
94         }
95
96         fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
97             val content: String = getClassPathFileContent(fileName)
98             return jsonNode(content)
99         }
100
101         fun jsonNodeFromFile(fileName: String): JsonNode {
102             val content: String = getContent(fileName)
103             return jsonNode(content)
104         }
105
106         fun jsonNode(content: String): JsonNode {
107             return jacksonObjectMapper().readTree(content)
108         }
109
110         fun getJson(any: kotlin.Any): String {
111             return getJson(any, false)
112         }
113
114         fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {
115             val wrapperMap = hashMapOf<String, Any>()
116             wrapperMap[wrapper] = any
117             return getJson(wrapperMap, pretty)
118         }
119
120         fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
121             val objectMapper = jacksonObjectMapper()
122             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
123             if (pretty) {
124                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
125             }
126             return objectMapper.writeValueAsString(any)
127         }
128
129         fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {
130             val objectMapper = jacksonObjectMapper()
131             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
132             if (pretty) {
133                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
134             }
135             return objectMapper.valueToTree(any)
136         }
137
138         fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T> {
139             return getListFromJson(node.toString(), valueType)
140         }
141
142         fun <T> getListFromJson(content: String, valueType: Class<T>): List<T> {
143             val objectMapper = jacksonObjectMapper()
144             val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
145             return objectMapper.readValue<List<T>>(content, javaType)
146         }
147
148         fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T> {
149             val content: String = getContent(fileName)
150             return getListFromJson(content, valueType)
151         }
152
153         fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T> {
154             val content: String = getClassPathFileContent(fileName)
155             return getListFromJson(content, valueType)
156         }
157
158         fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T> {
159             val objectMapper = jacksonObjectMapper()
160             val mapType = objectMapper.typeFactory.constructMapType(Map::class.java, String::class.java, valueType)
161             return objectMapper.readValue(content, mapType)
162         }
163
164         fun <T> getMapFromFile(file: File, valueType: Class<T>): MutableMap<String, T> = runBlocking {
165             val content: String = file.readNBText()
166             getMapFromJson(content, valueType)
167         }
168
169         fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T> = getMapFromFile(File(fileName), valueType)
170
171         fun <T> getInstanceFromMap(properties: MutableMap<String, JsonNode>, classType: Class<T>): T {
172             return readValue(getJson(properties), classType)
173                     ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)")
174         }
175
176         fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
177             if (BluePrintTypes.validPrimitiveTypes().contains(type.toLowerCase())) {
178                 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
179             } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
180                 return checkJsonNodeValueOfCollectionType(type, jsonNode)
181             }
182             return false
183         }
184
185         fun checkIfPrimitiveType(primitiveType: String): Boolean {
186             return when (primitiveType.toLowerCase()) {
187                 BluePrintConstants.DATA_TYPE_STRING -> true
188                 BluePrintConstants.DATA_TYPE_BOOLEAN -> true
189                 BluePrintConstants.DATA_TYPE_INTEGER -> true
190                 BluePrintConstants.DATA_TYPE_FLOAT -> true
191                 BluePrintConstants.DATA_TYPE_DOUBLE -> true
192                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> true
193                 else -> false
194             }
195         }
196
197         fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
198             return when (primitiveType.toLowerCase()) {
199                 BluePrintConstants.DATA_TYPE_STRING -> jsonNode.isTextual
200                 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNode.isBoolean
201                 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNode.isInt
202                 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNode.isDouble
203                 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNode.isDouble
204                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> jsonNode.isTextual
205                 else -> false
206             }
207         }
208
209         fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
210             return when (type.toLowerCase()) {
211                 BluePrintConstants.DATA_TYPE_LIST -> jsonNode.isArray
212                 BluePrintConstants.DATA_TYPE_MAP -> jsonNode.isContainerNode
213                 else -> false
214             }
215         }
216
217         fun getValue(value: JsonNode): Any {
218             return when (value) {
219                 is BooleanNode -> value.booleanValue()
220                 is IntNode -> value.intValue()
221                 is FloatNode -> value.floatValue()
222                 is DoubleNode -> value.doubleValue()
223                 is TextNode -> value.textValue()
224                 else -> value
225             }
226         }
227
228         fun getValue(value: Any, type: String): Any {
229             return when (type.toLowerCase()) {
230                 BluePrintConstants.DATA_TYPE_BOOLEAN -> (value as BooleanNode).booleanValue()
231                 BluePrintConstants.DATA_TYPE_INTEGER -> (value as IntNode).intValue()
232                 BluePrintConstants.DATA_TYPE_FLOAT -> (value as FloatNode).floatValue()
233                 BluePrintConstants.DATA_TYPE_DOUBLE -> (value as DoubleNode).doubleValue()
234                 BluePrintConstants.DATA_TYPE_STRING -> (value as TextNode).textValue()
235                 else -> (value as JsonNode)
236             }
237         }
238
239         fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
240             when (primitiveType.toLowerCase()) {
241                 BluePrintConstants.DATA_TYPE_STRING,
242                 BluePrintConstants.DATA_TYPE_BOOLEAN,
243                 BluePrintConstants.DATA_TYPE_INTEGER,
244                 BluePrintConstants.DATA_TYPE_FLOAT,
245                 BluePrintConstants.DATA_TYPE_DOUBLE,
246                 BluePrintConstants.DATA_TYPE_TIMESTAMP ->
247                     objectNode.set(key, value.asJsonPrimitive())
248                 else -> objectNode.set(key, value.asJsonType())
249             }
250         }
251
252         fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {
253             when (primitiveType.toLowerCase()) {
254                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)
255                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)
256                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)
257                 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double)
258                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)
259                 else -> arrayNode.add(value as String)
260             }
261         }
262
263         fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
264             when (primitiveType.toLowerCase()) {
265                 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)
266                 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)
267                 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)
268                 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0)
269                 else -> objectNode.put(key, "")
270             }
271         }
272
273         fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
274             when (primitiveType.toLowerCase()) {
275                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)
276                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)
277                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)
278                 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0)
279                 else -> arrayNode.add("")
280             }
281         }
282
283         fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
284             if (nodeValue == null || nodeValue is NullNode) {
285                 objectNode.set(key, nodeValue)
286             } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
287                 populatePrimitiveValues(key, nodeValue, type, objectNode)
288             } else {
289                 objectNode.set(key, nodeValue)
290             }
291         }
292
293         fun convertPrimitiveResourceValue(type: String, value: String): JsonNode {
294             return when (type.toLowerCase()) {
295                 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value))
296                 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value))
297                 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value))
298                 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value))
299                 else -> getJsonNode(value)
300             }
301         }
302     }
303 }