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.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
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 removeJsonNullNode(node: JsonNode) {
\r
60 val it = node.iterator()
\r
61 while (it.hasNext()) {
\r
62 val child = it.next()
\r
66 removeJsonNullNode(child)
\r
71 fun getContent(fileName: String): String = runBlocking {
\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
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
90 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
\r
91 val content: String = getContent(fileName)
\r
92 return readValue(content, valueType)
\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
100 fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
\r
101 return jacksonObjectMapper().convertValue(from, JsonNode::class.java)
\r
104 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
\r
105 val content: String = getClassPathFileContent(fileName)
\r
106 return jsonNode(content)
\r
109 fun jsonNodeFromFile(fileName: String): JsonNode {
\r
110 val content: String = getContent(fileName)
\r
111 return jsonNode(content)
\r
114 fun jsonNode(content: String): JsonNode {
\r
115 return jacksonObjectMapper().readTree(content)
\r
118 fun getJson(any: kotlin.Any): String {
\r
119 return getJson(any, false)
\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
128 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
\r
129 val objectMapper = jacksonObjectMapper()
\r
130 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
\r
132 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
134 return objectMapper.writeValueAsString(any)
\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
141 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
143 return objectMapper.valueToTree(any)
\r
146 fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T>? {
\r
147 return getListFromJson(node.toString(), valueType)
\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
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
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
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
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
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
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
197 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
\r
199 BluePrintConstants.DATA_TYPE_LIST -> return jsonNode.isArray
\r
200 BluePrintConstants.DATA_TYPE_MAP -> return jsonNode.isContainerNode
\r
201 else -> return false
\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
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
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
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
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
249 objectNode.set(key, nodeValue)
\r
253 fun convertPrimitiveResourceValue(type: String, value: String): JsonNode? {
\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