2 * Copyright © 2017-2018 AT&T Intellectual Property.
3 * Modifications Copyright © 2018 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.apps.controllerblueprints.core.utils
19 import com.att.eelf.configuration.EELFLogger
20 import com.att.eelf.configuration.EELFManager
21 import com.fasterxml.jackson.annotation.JsonInclude
22 import com.fasterxml.jackson.core.type.TypeReference
23 import com.fasterxml.jackson.databind.JsonNode
24 import com.fasterxml.jackson.databind.SerializationFeature
25 import com.fasterxml.jackson.databind.node.ArrayNode
26 import com.fasterxml.jackson.databind.node.NullNode
27 import com.fasterxml.jackson.databind.node.ObjectNode
28 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
29 import kotlinx.coroutines.Dispatchers
30 import kotlinx.coroutines.async
31 import kotlinx.coroutines.runBlocking
32 import kotlinx.coroutines.withContext
33 import org.apache.commons.io.IOUtils
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
35 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
36 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
37 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
39 import java.nio.charset.Charset
44 * @author Brinda Santh
48 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
49 inline fun <reified T : Any> readValue(content: String): T =
50 jacksonObjectMapper().readValue(content, T::class.java)
52 fun <T> readValue(content: String, valueType: Class<T>): T? {
53 return jacksonObjectMapper().readValue(content, valueType)
56 fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {
57 return jacksonObjectMapper().treeToValue(node, valueType)
60 fun removeJsonNullNode(node: JsonNode) {
61 val it = node.iterator()
62 while (it.hasNext()) {
67 removeJsonNullNode(child)
72 fun getContent(fileName: String): String = runBlocking {
75 File(fileName).readText(Charsets.UTF_8)
76 } catch (e: Exception) {
77 throw BluePrintException("couldn't get file ($fileName) content : ${e.message}")
82 fun getClassPathFileContent(fileName: String): String {
84 withContext(Dispatchers.Default) {
85 IOUtils.toString(JacksonUtils::class.java.classLoader
86 .getResourceAsStream(fileName), Charset.defaultCharset())
91 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
92 val content: String = getContent(fileName)
93 return readValue(content, valueType)
96 fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
97 val content: String = getClassPathFileContent(fileName)
98 return readValue(content, valueType)
101 fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
102 return jacksonObjectMapper().convertValue(from, JsonNode::class.java)
105 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
106 val content: String = getClassPathFileContent(fileName)
107 return jsonNode(content)
110 fun jsonNodeFromFile(fileName: String): JsonNode {
111 val content: String = getContent(fileName)
112 return jsonNode(content)
115 fun jsonNode(content: String): JsonNode {
116 return jacksonObjectMapper().readTree(content)
119 fun getJson(any: kotlin.Any): String {
120 return getJson(any, false)
123 fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {
124 val wrapperMap = hashMapOf<String, Any>()
125 wrapperMap[wrapper] = any
126 return getJson(wrapperMap, pretty)
129 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
130 val objectMapper = jacksonObjectMapper()
131 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
133 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
135 return objectMapper.writeValueAsString(any)
138 fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {
139 val objectMapper = jacksonObjectMapper()
140 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
142 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
144 return objectMapper.valueToTree(any)
147 fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T>? {
148 return getListFromJson(node.toString(), valueType)
151 fun <T> getListFromJson(content: String, valueType: Class<T>): List<T>? {
152 val objectMapper = jacksonObjectMapper()
153 val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
154 return objectMapper.readValue<List<T>>(content, javaType)
157 fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T>? {
158 val content: String = getContent(fileName)
159 return getListFromJson(content, valueType)
162 fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T>? {
163 val content: String = getClassPathFileContent(fileName)
164 return getListFromJson(content, valueType)
167 fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T>? {
168 val objectMapper = jacksonObjectMapper()
169 val typeRef = object : TypeReference<MutableMap<String, T>>() {}
170 return objectMapper.readValue(content, typeRef)
173 fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T>? {
174 val content: String = getContent(fileName)
175 return getMapFromJson(content, valueType)
178 fun <T> getInstanceFromMap(properties: MutableMap<String, JsonNode>, classType: Class<T>): T {
179 return readValue(getJson(properties), classType)
180 ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)")
183 fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
184 if (BluePrintTypes.validPrimitiveTypes().contains(type.toLowerCase())) {
185 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
186 } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
187 return checkJsonNodeValueOfCollectionType(type, jsonNode)
192 fun checkIfPrimitiveType(primitiveType: String): Boolean {
193 return when (primitiveType.toLowerCase()) {
194 BluePrintConstants.DATA_TYPE_STRING -> true
195 BluePrintConstants.DATA_TYPE_BOOLEAN -> true
196 BluePrintConstants.DATA_TYPE_INTEGER -> true
197 BluePrintConstants.DATA_TYPE_FLOAT -> true
198 BluePrintConstants.DATA_TYPE_DOUBLE -> true
199 BluePrintConstants.DATA_TYPE_TIMESTAMP -> true
204 fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
205 return when (primitiveType.toLowerCase()) {
206 BluePrintConstants.DATA_TYPE_STRING -> jsonNode.isTextual
207 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNode.isBoolean
208 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNode.isInt
209 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNode.isDouble
210 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNode.isDouble
211 BluePrintConstants.DATA_TYPE_TIMESTAMP -> jsonNode.isTextual
216 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
217 return when (type.toLowerCase()) {
218 BluePrintConstants.DATA_TYPE_LIST -> jsonNode.isArray
219 BluePrintConstants.DATA_TYPE_MAP -> jsonNode.isContainerNode
224 fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
225 when (primitiveType.toLowerCase()) {
226 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean)
227 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int)
228 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float)
229 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, value as Double)
230 BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String)
231 else -> objectNode.put(key, value as String)
235 fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {
236 when (primitiveType.toLowerCase()) {
237 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)
238 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)
239 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)
240 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double)
241 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)
242 else -> arrayNode.add(value as String)
246 fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
247 when (primitiveType.toLowerCase()) {
248 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)
249 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)
250 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)
251 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0)
252 else -> objectNode.put(key, "")
256 fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
257 when (primitiveType.toLowerCase()) {
258 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)
259 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)
260 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)
261 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0)
262 else -> arrayNode.add("")
266 fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
267 if (nodeValue == null || nodeValue is NullNode) {
268 objectNode.set(key, nodeValue)
269 } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
270 populatePrimitiveValues(key, nodeValue, type, objectNode)
272 objectNode.set(key, nodeValue)
276 fun convertPrimitiveResourceValue(type: String, value: String): JsonNode {
277 return when (type.toLowerCase()) {
278 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value))
279 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value))
280 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value))
281 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value))
282 else -> getJsonNode(value)