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
18 package org.onap.ccsdk.apps.controllerblueprints.core.utils
\r
20 import com.fasterxml.jackson.annotation.JsonInclude
\r
21 import com.fasterxml.jackson.core.type.TypeReference
\r
22 import com.fasterxml.jackson.databind.JsonNode
\r
23 import com.fasterxml.jackson.databind.SerializationFeature
\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.module.kotlin.jacksonObjectMapper
\r
28 import org.apache.commons.io.FileUtils
\r
29 import org.apache.commons.io.IOUtils
\r
30 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
\r
31 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
32 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
\r
33 import org.onap.ccsdk.apps.controllerblueprints.core.format
\r
34 import org.slf4j.LoggerFactory
\r
36 import java.nio.charset.Charset
\r
41 * @author Brinda Santh
\r
43 object JacksonUtils {
\r
44 private val log = LoggerFactory.getLogger(JacksonUtils::class.java)
\r
46 inline fun <reified T : Any> readValue(content: String): T =
\r
47 jacksonObjectMapper().readValue(content, T::class.java)
\r
50 fun <T> readValue(content: String, valueType: Class<T>): T? {
\r
51 return jacksonObjectMapper().readValue(content, valueType)
\r
55 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
\r
56 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
57 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
58 return readValue(content, valueType)
\r
62 fun jsonNodeFromObject(from: kotlin.Any): JsonNode = jacksonObjectMapper().convertValue(from, JsonNode::class.java)
\r
65 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
\r
66 val content: String = IOUtils.toString(JacksonUtils::class.java.classLoader.getResourceAsStream(fileName), Charset.defaultCharset())
\r
67 ?: throw BluePrintException(String.format("Failed to read json file : %s", fileName))
\r
68 return jsonNode(content)
\r
72 fun jsonNodeFromFile(fileName: String): JsonNode {
\r
73 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
74 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
75 return jsonNode(content)
\r
79 fun jsonNode(content: String): JsonNode {
\r
80 return jacksonObjectMapper().readTree(content)
\r
84 fun getJson(any: kotlin.Any): String {
\r
85 return getJson(any, false)
\r
89 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
\r
90 val objectMapper = jacksonObjectMapper()
\r
91 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
\r
93 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
95 return objectMapper.writeValueAsString(any)
\r
99 fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {
\r
100 val objectMapper = jacksonObjectMapper()
\r
101 val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
\r
102 return objectMapper.readValue<List<T>>(content, javaType)
\r
106 fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {
\r
107 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
108 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
109 return getListFromJson(content, valueType)
\r
113 fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {
\r
114 val content: String = IOUtils.toString(JacksonUtils::class.java.classLoader.getResourceAsStream(fileName), Charset.defaultCharset())
\r
115 ?: throw BluePrintException(String.format("Failed to read json file : %s", fileName))
\r
116 return getListFromJson(content, valueType)
\r
120 fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {
\r
121 val objectMapper = jacksonObjectMapper()
\r
122 val typeRef = object : TypeReference<MutableMap<String, T>>() {}
\r
123 return objectMapper.readValue(content, typeRef)
\r
127 fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
\r
128 if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
\r
129 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
\r
130 } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
\r
131 return checkJsonNodeValueOfCollectionType(type, jsonNode)
\r
137 fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
\r
138 when (primitiveType) {
\r
139 BluePrintConstants.DATA_TYPE_STRING -> {
\r
140 return jsonNode.isTextual
\r
142 BluePrintConstants.DATA_TYPE_BOOLEAN -> {
\r
143 return jsonNode.isBoolean
\r
145 BluePrintConstants.DATA_TYPE_INTEGER -> {
\r
146 return jsonNode.isInt
\r
148 BluePrintConstants.DATA_TYPE_FLOAT -> {
\r
149 return jsonNode.isDouble
\r
151 BluePrintConstants.DATA_TYPE_TIMESTAMP -> {
\r
152 return jsonNode.isTextual
\r
160 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
\r
162 BluePrintConstants.DATA_TYPE_LIST ->
\r
163 return jsonNode.isArray
\r
164 BluePrintConstants.DATA_TYPE_MAP ->
\r
165 return jsonNode.isContainerNode
\r
173 fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
\r
174 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
175 objectNode.put(key, value as Boolean)
\r
176 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
177 objectNode.put(key, value as Int)
\r
178 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
179 objectNode.put(key, value as Float)
\r
180 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
\r
181 objectNode.put(key, value as String)
\r
183 objectNode.put(key, value as String)
\r
188 fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) {
\r
189 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
190 objectNode.add(value as Boolean)
\r
191 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
192 objectNode.add(value as Int)
\r
193 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
194 objectNode.add(value as Float)
\r
195 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
\r
196 objectNode.add(value as String)
\r
198 objectNode.add(value as String)
\r
203 fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
\r
204 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
205 objectNode.put(key, false)
\r
206 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
207 objectNode.put(key, 0)
\r
208 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
209 objectNode.put(key, 0.0)
\r
211 objectNode.put(key, "")
\r
216 fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
\r
217 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
218 arrayNode.add(false)
\r
219 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
221 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
229 fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
\r
230 if (nodeValue == null || nodeValue is NullNode) {
\r
231 objectNode.set(key, nodeValue)
\r
232 } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
\r
233 if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) {
\r
234 objectNode.put(key, nodeValue.asBoolean())
\r
235 } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) {
\r
236 objectNode.put(key, nodeValue.asInt())
\r
237 } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) {
\r
238 objectNode.put(key, nodeValue.floatValue())
\r
239 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {
\r
240 objectNode.put(key, nodeValue.asText())
\r
242 objectNode.put(key, nodeValue.asText())
\r
245 objectNode.set(key, nodeValue)
\r