2 * Copyright © 2017-2018 AT&T Intellectual Property.
\r
3 * Modifications Copyright © 2018 IBM.
\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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.onap.ccsdk.apps.controllerblueprints.core.utils
\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
38 import java.nio.charset.Charset
\r
43 * @author Brinda Santh
\r
45 class JacksonUtils {
\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
51 fun <T> readValue(content: String, valueType: Class<T>): T? {
\r
52 return jacksonObjectMapper().readValue(content, valueType)
\r
55 fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {
\r
56 return jacksonObjectMapper().treeToValue(node, valueType)
\r
59 fun getContent(fileName: String): String = runBlocking {
\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
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
78 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
\r
79 val content: String = getContent(fileName)
\r
80 return readValue(content, valueType)
\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
88 fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
\r
89 return jacksonObjectMapper().convertValue(from, JsonNode::class.java)
\r
92 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
\r
93 val content: String = getClassPathFileContent(fileName)
\r
94 return jsonNode(content)
\r
97 fun jsonNodeFromFile(fileName: String): JsonNode {
\r
98 val content: String = getContent(fileName)
\r
99 return jsonNode(content)
\r
102 fun jsonNode(content: String): JsonNode {
\r
103 return jacksonObjectMapper().readTree(content)
\r
106 fun getJson(any: kotlin.Any): String {
\r
107 return getJson(any, false)
\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
116 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
\r
117 val objectMapper = jacksonObjectMapper()
\r
118 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
\r
120 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
122 return objectMapper.writeValueAsString(any)
\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
129 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
131 return objectMapper.valueToTree(any)
\r
134 fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T>? {
\r
135 return getListFromJson(node.toString(), valueType)
\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
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
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
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
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
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
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
185 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
\r
187 BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray
\r
188 BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode
\r
189 else -> return false
\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
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
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
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
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
237 objectNode.set(key, nodeValue)
\r
241 fun convertPrimitiveResourceValue(type: String, value: String): JsonNode? {
\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