Resource Resoulution Service
[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.node.ArrayNode\r
25 import com.fasterxml.jackson.databind.node.NullNode\r
26 import com.fasterxml.jackson.databind.node.ObjectNode\r
27 import com.fasterxml.jackson.databind.SerializationFeature\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 getContent(fileName: String): String = runBlocking {\r
60             async {\r
61                 try {\r
62                     File(fileName).readText(Charsets.UTF_8)\r
63                 } catch (e: Exception) {\r
64                     throw BluePrintException("couldn't get file ($fileName) content : ${e.message}")\r
65                 }\r
66             }.await()\r
67         }\r
68 \r
69         fun getClassPathFileContent(fileName: String): String {\r
70             return runBlocking {\r
71                 withContext(Dispatchers.Default) {\r
72                     IOUtils.toString(JacksonUtils::class.java.classLoader\r
73                             .getResourceAsStream(fileName), Charset.defaultCharset())\r
74                 }\r
75             }\r
76         }\r
77 \r
78         fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {\r
79             val content: String = getContent(fileName)\r
80             return readValue(content, valueType)\r
81         }\r
82 \r
83         fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {\r
84             val content: String = getClassPathFileContent(fileName)\r
85             return readValue(content, valueType)\r
86         }\r
87 \r
88         fun jsonNodeFromObject(from: kotlin.Any): JsonNode {\r
89             return jacksonObjectMapper().convertValue(from, JsonNode::class.java)\r
90         }\r
91 \r
92         fun jsonNodeFromClassPathFile(fileName: String): JsonNode {\r
93             val content: String = getClassPathFileContent(fileName)\r
94             return jsonNode(content)\r
95         }\r
96 \r
97         fun jsonNodeFromFile(fileName: String): JsonNode {\r
98             val content: String = getContent(fileName)\r
99             return jsonNode(content)\r
100         }\r
101 \r
102         fun jsonNode(content: String): JsonNode {\r
103             return jacksonObjectMapper().readTree(content)\r
104         }\r
105 \r
106         fun getJson(any: kotlin.Any): String {\r
107             return getJson(any, false)\r
108         }\r
109 \r
110         fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {\r
111             val wrapperMap = hashMapOf<String, Any>()\r
112             wrapperMap[wrapper] = any\r
113             return getJson(wrapperMap, pretty)\r
114         }\r
115 \r
116         fun getJson(any: kotlin.Any, pretty: Boolean = false): String {\r
117             val objectMapper = jacksonObjectMapper()\r
118             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
119             if (pretty) {\r
120                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
121             }\r
122             return objectMapper.writeValueAsString(any)\r
123         }\r
124 \r
125         fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {\r
126             val objectMapper = jacksonObjectMapper()\r
127             objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)\r
128             if (pretty) {\r
129                 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)\r
130             }\r
131             return objectMapper.valueToTree(any)\r
132         }\r
133 \r
134         fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T>? {\r
135             return getListFromJson(node.toString(), valueType)\r
136         }\r
137 \r
138         fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {\r
139             val objectMapper = jacksonObjectMapper()\r
140             val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)\r
141             return objectMapper.readValue<List<T>>(content, javaType)\r
142         }\r
143 \r
144         fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {\r
145             val content: String = getContent(fileName)\r
146             return getListFromJson(content, valueType)\r
147         }\r
148 \r
149         fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {\r
150             val content: String = getClassPathFileContent(fileName)\r
151             return getListFromJson(content, valueType)\r
152         }\r
153 \r
154         fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {\r
155             val objectMapper = jacksonObjectMapper()\r
156             val typeRef = object : TypeReference<MutableMap<String, T>>() {}\r
157             return objectMapper.readValue(content, typeRef)\r
158         }\r
159 \r
160         fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T>? {\r
161             val content: String = getContent(fileName)\r
162             return getMapFromJson(content, valueType)\r
163         }\r
164 \r
165         fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {\r
166             if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
167                 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)\r
168             } else if (BluePrintTypes.validCollectionTypes().contains(type)) {\r
169                 return checkJsonNodeValueOfCollectionType(type, jsonNode)\r
170             }\r
171             return false\r
172         }\r
173 \r
174         fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {\r
175             when (primitiveType) {\r
176                 BluePrintConstants.DATA_TYPE_STRING -> return jsonNode.isTextual\r
177                 BluePrintConstants.DATA_TYPE_BOOLEAN -> return jsonNode.isBoolean\r
178                 BluePrintConstants.DATA_TYPE_INTEGER -> return jsonNode.isInt\r
179                 BluePrintConstants.DATA_TYPE_FLOAT -> return jsonNode.isDouble\r
180                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> return jsonNode.isTextual\r
181                 else -> return false\r
182             }\r
183         }\r
184 \r
185         fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {\r
186             when (type) {\r
187                 BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray\r
188                 BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode\r
189                 else -> return false\r
190             }\r
191         }\r
192 \r
193         fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {\r
194             when (primitiveType) {\r
195                 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean)\r
196                 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int)\r
197                 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float)\r
198                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String)\r
199                 else -> objectNode.put(key, value as String)\r
200             }\r
201         }\r
202 \r
203         fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {\r
204             when (primitiveType) {\r
205                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)\r
206                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)\r
207                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)\r
208                 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)\r
209                 else -> arrayNode.add(value as String)\r
210             }\r
211         }\r
212 \r
213         fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {\r
214             when (primitiveType) {\r
215                 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)\r
216                 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)\r
217                 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)\r
218                 else -> objectNode.put(key, "")\r
219             }\r
220         }\r
221 \r
222         fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {\r
223             when (primitiveType) {\r
224                 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)\r
225                 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)\r
226                 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)\r
227                 else -> arrayNode.add("")\r
228             }\r
229         }\r
230 \r
231         fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {\r
232             if (nodeValue == null || nodeValue is NullNode) {\r
233                 objectNode.set(key, nodeValue)\r
234             } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {\r
235                 populatePrimitiveValues(key, nodeValue, type, objectNode)\r
236             } else {\r
237                 objectNode.set(key, nodeValue)\r
238             }\r
239         }\r
240 \r
241         fun convertPrimitiveResourceValue(type: String, value: String): JsonNode? {\r
242             when (type) {\r
243                 BluePrintConstants.DATA_TYPE_BOOLEAN -> return JacksonUtils.getJsonNode(value as Boolean)\r
244                 BluePrintConstants.DATA_TYPE_INTEGER -> return JacksonUtils.getJsonNode(value as Int)\r
245                 BluePrintConstants.DATA_TYPE_FLOAT -> return JacksonUtils.getJsonNode(value as Float)\r
246                 else -> return JacksonUtils.getJsonNode(value)\r
247             }\r
248         }\r
249     }\r
250 }