2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018-2019 IBM.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.controllerblueprints.core.utils
19 import com.fasterxml.jackson.annotation.JsonInclude
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.SerializationFeature
22 import com.fasterxml.jackson.databind.node.*
23 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
24 import kotlinx.coroutines.Dispatchers
25 import kotlinx.coroutines.runBlocking
26 import kotlinx.coroutines.withContext
27 import org.apache.commons.io.IOUtils
28 import org.onap.ccsdk.cds.controllerblueprints.core.*
30 import java.io.InputStream
31 import java.nio.charset.Charset
36 * @author Brinda Santh
41 val objectMapper = jacksonObjectMapper()
43 inline fun <reified T : Any> readValue(content: String): T =
44 objectMapper.readValue(content, T::class.java)
46 inline fun <reified T : Any> readValue(stream: InputStream): T =
47 objectMapper.readValue(stream, T::class.java)
49 fun <T> readValue(content: String, valueType: Class<T>): T? {
50 return objectMapper.readValue(content, valueType)
53 fun <T> readValue(stream: InputStream, valueType: Class<T>): T? {
54 return objectMapper.readValue(stream, valueType)
57 fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {
58 return objectMapper.treeToValue(node, valueType)
61 fun getContent(fileName: String): String = runBlocking {
63 normalizedFile(fileName).readNBText()
64 } catch (e: Exception) {
65 throw BluePrintException("couldn't get file ($fileName) content : ${e.message}")
69 fun getClassPathFileContent(fileName: String): String {
71 withContext(Dispatchers.Default) {
72 IOUtils.toString(JacksonUtils::class.java.classLoader
73 .getResourceAsStream(fileName), Charset.defaultCharset())
78 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
79 val content: String = getContent(fileName)
80 return readValue(content, valueType)
83 fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
84 val content: String = getClassPathFileContent(fileName)
85 return readValue(content, valueType)
88 fun objectNodeFromObject(from: kotlin.Any): ObjectNode {
89 return objectMapper.convertValue(from, ObjectNode::class.java)
92 fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
93 return objectMapper.convertValue(from, JsonNode::class.java)
96 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
97 val content: String = getClassPathFileContent(fileName)
98 return jsonNode(content)
101 fun jsonNodeFromFile(fileName: String): JsonNode {
102 val content: String = getContent(fileName)
103 return jsonNode(content)
106 fun jsonNode(content: String): JsonNode {
107 return jacksonObjectMapper().readTree(content)
110 fun getJson(any: kotlin.Any): String {
111 return getJson(any, false)
114 fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {
115 val wrapperMap = hashMapOf<String, Any>()
116 wrapperMap[wrapper] = any
117 return getJson(wrapperMap, pretty)
120 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
121 val objectMapper = jacksonObjectMapper()
122 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
124 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
126 return objectMapper.writeValueAsString(any)
129 fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {
130 val objectMapper = jacksonObjectMapper()
131 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
133 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
135 return objectMapper.valueToTree(any)
138 fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T> {
139 return getListFromJson(node.toString(), valueType)
142 fun <T> getListFromJson(content: String, valueType: Class<T>): List<T> {
143 val objectMapper = jacksonObjectMapper()
144 val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
145 return objectMapper.readValue<List<T>>(content, javaType)
148 fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T> {
149 val content: String = getContent(fileName)
150 return getListFromJson(content, valueType)
153 fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T> {
154 val content: String = getClassPathFileContent(fileName)
155 return getListFromJson(content, valueType)
158 fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T> {
159 val objectMapper = jacksonObjectMapper()
160 val mapType = objectMapper.typeFactory.constructMapType(Map::class.java, String::class.java, valueType)
161 return objectMapper.readValue(content, mapType)
164 fun <T> getMapFromFile(file: File, valueType: Class<T>): MutableMap<String, T> = runBlocking {
165 val content: String = file.readNBText()
166 getMapFromJson(content, valueType)
169 fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T> = getMapFromFile(File(fileName), valueType)
171 fun <T> getInstanceFromMap(properties: MutableMap<String, JsonNode>, classType: Class<T>): T {
172 return readValue(getJson(properties), classType)
173 ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)")
176 fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
177 if (BluePrintTypes.validPrimitiveTypes().contains(type.toLowerCase())) {
178 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
179 } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
180 return checkJsonNodeValueOfCollectionType(type, jsonNode)
185 fun checkIfPrimitiveType(primitiveType: String): Boolean {
186 return when (primitiveType.toLowerCase()) {
187 BluePrintConstants.DATA_TYPE_STRING -> true
188 BluePrintConstants.DATA_TYPE_BOOLEAN -> true
189 BluePrintConstants.DATA_TYPE_INTEGER -> true
190 BluePrintConstants.DATA_TYPE_FLOAT -> true
191 BluePrintConstants.DATA_TYPE_DOUBLE -> true
192 BluePrintConstants.DATA_TYPE_TIMESTAMP -> true
197 fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
198 return when (primitiveType.toLowerCase()) {
199 BluePrintConstants.DATA_TYPE_STRING -> jsonNode.isTextual
200 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNode.isBoolean
201 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNode.isInt
202 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNode.isDouble
203 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNode.isDouble
204 BluePrintConstants.DATA_TYPE_TIMESTAMP -> jsonNode.isTextual
209 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
210 return when (type.toLowerCase()) {
211 BluePrintConstants.DATA_TYPE_LIST -> jsonNode.isArray
212 BluePrintConstants.DATA_TYPE_MAP -> jsonNode.isContainerNode
217 fun getValue(value: JsonNode): Any {
218 return when (value) {
219 is BooleanNode -> value.booleanValue()
220 is IntNode -> value.intValue()
221 is FloatNode -> value.floatValue()
222 is DoubleNode -> value.doubleValue()
223 is TextNode -> value.textValue()
228 fun getValue(value: Any, type: String): Any {
229 return when (type.toLowerCase()) {
230 BluePrintConstants.DATA_TYPE_BOOLEAN -> (value as BooleanNode).booleanValue()
231 BluePrintConstants.DATA_TYPE_INTEGER -> (value as IntNode).intValue()
232 BluePrintConstants.DATA_TYPE_FLOAT -> (value as FloatNode).floatValue()
233 BluePrintConstants.DATA_TYPE_DOUBLE -> (value as DoubleNode).doubleValue()
234 BluePrintConstants.DATA_TYPE_STRING -> (value as TextNode).textValue()
235 else -> (value as JsonNode)
239 fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
240 when (primitiveType.toLowerCase()) {
241 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, (value as BooleanNode).booleanValue())
242 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, (value as IntNode).intValue())
243 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, (value as FloatNode).floatValue())
244 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, (value as DoubleNode).doubleValue())
245 else -> objectNode.put(key, (value as TextNode).textValue())
249 fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {
250 when (primitiveType.toLowerCase()) {
251 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)
252 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)
253 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)
254 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double)
255 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)
256 else -> arrayNode.add(value as String)
260 fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
261 when (primitiveType.toLowerCase()) {
262 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)
263 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)
264 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)
265 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0)
266 else -> objectNode.put(key, "")
270 fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
271 when (primitiveType.toLowerCase()) {
272 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)
273 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)
274 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)
275 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0)
276 else -> arrayNode.add("")
280 fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
281 if (nodeValue == null || nodeValue is NullNode) {
282 objectNode.set(key, nodeValue)
283 } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
284 populatePrimitiveValues(key, nodeValue, type, objectNode)
286 objectNode.set(key, nodeValue)
290 fun convertPrimitiveResourceValue(type: String, value: String): JsonNode {
291 return when (type.toLowerCase()) {
292 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value))
293 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value))
294 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value))
295 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value))
296 else -> getJsonNode(value)