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.att.eelf.configuration.EELFLogger
\r
21 import com.att.eelf.configuration.EELFManager
\r
22 import com.fasterxml.jackson.annotation.JsonInclude
\r
23 import com.fasterxml.jackson.core.type.TypeReference
\r
24 import com.fasterxml.jackson.databind.JsonNode
\r
25 import com.fasterxml.jackson.databind.SerializationFeature
\r
26 import com.fasterxml.jackson.databind.node.ArrayNode
\r
27 import com.fasterxml.jackson.databind.node.NullNode
\r
28 import com.fasterxml.jackson.databind.node.ObjectNode
\r
29 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
\r
30 import org.apache.commons.io.FileUtils
\r
31 import org.apache.commons.io.IOUtils
\r
32 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
\r
33 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
\r
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
\r
35 import org.onap.ccsdk.apps.controllerblueprints.core.format
\r
37 import java.nio.charset.Charset
\r
42 * @author Brinda Santh
\r
44 object JacksonUtils {
\r
45 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
\r
47 inline fun <reified T : Any> readValue(content: String): T =
\r
48 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
56 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
\r
57 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
58 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
59 return readValue(content, valueType)
\r
63 fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
\r
64 val content: String = IOUtils.toString(JacksonUtils::class.java.classLoader.getResourceAsStream(fileName), Charset.defaultCharset())
\r
65 ?: throw BluePrintException(String.format("Failed to read json file : %s", fileName))
\r
66 return readValue(content, valueType)
\r
70 fun jsonNodeFromObject(from: kotlin.Any): JsonNode = jacksonObjectMapper().convertValue(from, JsonNode::class.java)
\r
73 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
\r
74 val content: String = IOUtils.toString(JacksonUtils::class.java.classLoader.getResourceAsStream(fileName), Charset.defaultCharset())
\r
75 ?: throw BluePrintException(String.format("Failed to read json file : %s", fileName))
\r
76 return jsonNode(content)
\r
80 fun jsonNodeFromFile(fileName: String): JsonNode {
\r
81 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
82 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
83 return jsonNode(content)
\r
87 fun jsonNode(content: String): JsonNode {
\r
88 return jacksonObjectMapper().readTree(content)
\r
92 fun getJson(any: kotlin.Any): String {
\r
93 return getJson(any, false)
\r
97 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
\r
98 val objectMapper = jacksonObjectMapper()
\r
99 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
\r
101 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
\r
103 return objectMapper.writeValueAsString(any)
\r
107 fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {
\r
108 val objectMapper = jacksonObjectMapper()
\r
109 val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
\r
110 return objectMapper.readValue<List<T>>(content, javaType)
\r
114 fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {
\r
115 val content: String = FileUtils.readFileToString(File(fileName), Charset.defaultCharset())
\r
116 ?: throw BluePrintException(format("Failed to read json file : {}", fileName))
\r
117 return getListFromJson(content, valueType)
\r
121 fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {
\r
122 val content: String = IOUtils.toString(JacksonUtils::class.java.classLoader.getResourceAsStream(fileName), Charset.defaultCharset())
\r
123 ?: throw BluePrintException(String.format("Failed to read json file : %s", fileName))
\r
124 return getListFromJson(content, valueType)
\r
128 fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {
\r
129 val objectMapper = jacksonObjectMapper()
\r
130 val typeRef = object : TypeReference<MutableMap<String, T>>() {}
\r
131 return objectMapper.readValue(content, typeRef)
\r
135 fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
\r
136 if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
\r
137 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
\r
138 } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
\r
139 return checkJsonNodeValueOfCollectionType(type, jsonNode)
\r
145 fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
\r
146 when (primitiveType) {
\r
147 BluePrintConstants.DATA_TYPE_STRING -> {
\r
148 return jsonNode.isTextual
\r
150 BluePrintConstants.DATA_TYPE_BOOLEAN -> {
\r
151 return jsonNode.isBoolean
\r
153 BluePrintConstants.DATA_TYPE_INTEGER -> {
\r
154 return jsonNode.isInt
\r
156 BluePrintConstants.DATA_TYPE_FLOAT -> {
\r
157 return jsonNode.isDouble
\r
159 BluePrintConstants.DATA_TYPE_TIMESTAMP -> {
\r
160 return jsonNode.isTextual
\r
168 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
\r
170 BluePrintConstants.DATA_TYPE_LIST ->
\r
171 return jsonNode.isArray
\r
172 BluePrintConstants.DATA_TYPE_MAP ->
\r
173 return jsonNode.isContainerNode
\r
181 fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
\r
182 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
183 objectNode.put(key, value as Boolean)
\r
184 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
185 objectNode.put(key, value as Int)
\r
186 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
187 objectNode.put(key, value as Float)
\r
188 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
\r
189 objectNode.put(key, value as String)
\r
191 objectNode.put(key, value as String)
\r
196 fun populatePrimitiveValues(value: Any, primitiveType: String, objectNode: ArrayNode) {
\r
197 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
198 objectNode.add(value as Boolean)
\r
199 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
200 objectNode.add(value as Int)
\r
201 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
202 objectNode.add(value as Float)
\r
203 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == primitiveType) {
\r
204 objectNode.add(value as String)
\r
206 objectNode.add(value as String)
\r
211 fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
\r
212 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
213 objectNode.put(key, false)
\r
214 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
215 objectNode.put(key, 0)
\r
216 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
217 objectNode.put(key, 0.0)
\r
219 objectNode.put(key, "")
\r
224 fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
\r
225 if (BluePrintConstants.DATA_TYPE_BOOLEAN == primitiveType) {
\r
226 arrayNode.add(false)
\r
227 } else if (BluePrintConstants.DATA_TYPE_INTEGER == primitiveType) {
\r
229 } else if (BluePrintConstants.DATA_TYPE_FLOAT == primitiveType) {
\r
237 fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
\r
238 if (nodeValue == null || nodeValue is NullNode) {
\r
239 objectNode.set(key, nodeValue)
\r
240 } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
\r
241 if (BluePrintConstants.DATA_TYPE_BOOLEAN == type) {
\r
242 objectNode.put(key, nodeValue.asBoolean())
\r
243 } else if (BluePrintConstants.DATA_TYPE_INTEGER == type) {
\r
244 objectNode.put(key, nodeValue.asInt())
\r
245 } else if (BluePrintConstants.DATA_TYPE_FLOAT == type) {
\r
246 objectNode.put(key, nodeValue.floatValue())
\r
247 } else if (BluePrintConstants.DATA_TYPE_TIMESTAMP == type) {
\r
248 objectNode.put(key, nodeValue.asText())
\r
250 objectNode.put(key, nodeValue.asText())
\r
253 objectNode.set(key, nodeValue)
\r