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