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.databind.JsonNode
23 import com.fasterxml.jackson.databind.SerializationFeature
24 import com.fasterxml.jackson.databind.node.ArrayNode
25 import com.fasterxml.jackson.databind.node.NullNode
26 import com.fasterxml.jackson.databind.node.ObjectNode
27 import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
28 import kotlinx.coroutines.Dispatchers
29 import kotlinx.coroutines.async
30 import kotlinx.coroutines.runBlocking
31 import kotlinx.coroutines.withContext
32 import org.apache.commons.io.IOUtils
33 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
34 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintException
35 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
36 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintTypes
38 import java.nio.charset.Charset
43 * @author Brinda Santh
47 private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString())
48 inline fun <reified T : Any> readValue(content: String): T =
49 jacksonObjectMapper().readValue(content, T::class.java)
51 fun <T> readValue(content: String, valueType: Class<T>): T? {
52 return jacksonObjectMapper().readValue(content, valueType)
55 fun <T> readValue(node: JsonNode, valueType: Class<T>): T? {
56 return jacksonObjectMapper().treeToValue(node, valueType)
59 fun removeJsonNullNode(node: JsonNode) {
60 val it = node.iterator()
61 while (it.hasNext()) {
66 removeJsonNullNode(child)
71 fun getContent(fileName: String): String = getContent(File(fileName))
73 fun getContent(file: File): String = runBlocking {
76 file.readText(Charsets.UTF_8)
77 } catch (e: Exception) {
78 throw BluePrintException("couldn't get file (${file.absolutePath}) content : ${e.message}")
83 fun getClassPathFileContent(fileName: String): String {
85 withContext(Dispatchers.Default) {
86 IOUtils.toString(JacksonUtils::class.java.classLoader
87 .getResourceAsStream(fileName), Charset.defaultCharset())
92 fun <T> readValueFromFile(fileName: String, valueType: Class<T>): T? {
93 val content: String = getContent(fileName)
94 return readValue(content, valueType)
97 fun <T> readValueFromClassPathFile(fileName: String, valueType: Class<T>): T? {
98 val content: String = getClassPathFileContent(fileName)
99 return readValue(content, valueType)
102 fun objectNodeFromObject(from: kotlin.Any): ObjectNode {
103 return jacksonObjectMapper().convertValue(from, ObjectNode::class.java)
106 fun jsonNodeFromObject(from: kotlin.Any): JsonNode {
107 return jacksonObjectMapper().convertValue(from, JsonNode::class.java)
110 fun jsonNodeFromClassPathFile(fileName: String): JsonNode {
111 val content: String = getClassPathFileContent(fileName)
112 return jsonNode(content)
115 fun jsonNodeFromFile(fileName: String): JsonNode {
116 val content: String = getContent(fileName)
117 return jsonNode(content)
120 fun jsonNode(content: String): JsonNode {
121 return jacksonObjectMapper().readTree(content)
124 fun getJson(any: kotlin.Any): String {
125 return getJson(any, false)
128 fun getWrappedJson(wrapper: String, any: kotlin.Any, pretty: Boolean = false): String {
129 val wrapperMap = hashMapOf<String, Any>()
130 wrapperMap[wrapper] = any
131 return getJson(wrapperMap, pretty)
134 fun getJson(any: kotlin.Any, pretty: Boolean = false): String {
135 val objectMapper = jacksonObjectMapper()
136 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
138 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
140 return objectMapper.writeValueAsString(any)
143 fun getJsonNode(any: kotlin.Any?, pretty: Boolean = false): JsonNode {
144 val objectMapper = jacksonObjectMapper()
145 objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL)
147 objectMapper.enable(SerializationFeature.INDENT_OUTPUT)
149 return objectMapper.valueToTree(any)
152 fun <T> getListFromJsonNode(node: JsonNode, valueType: Class<T>): List<T> {
153 return getListFromJson(node.toString(), valueType)
156 fun <T> getListFromJson(content: String, valueType: Class<T>): List<T> {
157 val objectMapper = jacksonObjectMapper()
158 val javaType = objectMapper.typeFactory.constructCollectionType(List::class.java, valueType)
159 return objectMapper.readValue<List<T>>(content, javaType)
162 fun <T> getListFromFile(fileName: String, valueType: Class<T>): List<T> {
163 val content: String = getContent(fileName)
164 return getListFromJson(content, valueType)
167 fun <T> getListFromClassPathFile(fileName: String, valueType: Class<T>): List<T> {
168 val content: String = getClassPathFileContent(fileName)
169 return getListFromJson(content, valueType)
172 fun <T> getMapFromJson(content: String, valueType: Class<T>): MutableMap<String, T> {
173 val objectMapper = jacksonObjectMapper()
174 val mapType = objectMapper.typeFactory.constructMapType(Map::class.java, String::class.java, valueType)
175 return objectMapper.readValue(content, mapType)
178 fun <T> getMapFromFile(file: File, valueType: Class<T>): MutableMap<String, T> {
179 val content: String = getContent(file)
180 return getMapFromJson(content, valueType)
183 fun <T> getMapFromFile(fileName: String, valueType: Class<T>): MutableMap<String, T> = getMapFromFile(File(fileName), valueType)
185 fun <T> getInstanceFromMap(properties: MutableMap<String, JsonNode>, classType: Class<T>): T {
186 return readValue(getJson(properties), classType)
187 ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)")
190 fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean {
191 if (BluePrintTypes.validPrimitiveTypes().contains(type.toLowerCase())) {
192 return checkJsonNodeValueOfPrimitiveType(type, jsonNode)
193 } else if (BluePrintTypes.validCollectionTypes().contains(type)) {
194 return checkJsonNodeValueOfCollectionType(type, jsonNode)
199 fun checkIfPrimitiveType(primitiveType: String): Boolean {
200 return when (primitiveType.toLowerCase()) {
201 BluePrintConstants.DATA_TYPE_STRING -> true
202 BluePrintConstants.DATA_TYPE_BOOLEAN -> true
203 BluePrintConstants.DATA_TYPE_INTEGER -> true
204 BluePrintConstants.DATA_TYPE_FLOAT -> true
205 BluePrintConstants.DATA_TYPE_DOUBLE -> true
206 BluePrintConstants.DATA_TYPE_TIMESTAMP -> true
211 fun checkJsonNodeValueOfPrimitiveType(primitiveType: String, jsonNode: JsonNode): Boolean {
212 return when (primitiveType.toLowerCase()) {
213 BluePrintConstants.DATA_TYPE_STRING -> jsonNode.isTextual
214 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNode.isBoolean
215 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNode.isInt
216 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNode.isDouble
217 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNode.isDouble
218 BluePrintConstants.DATA_TYPE_TIMESTAMP -> jsonNode.isTextual
223 fun checkJsonNodeValueOfCollectionType(type: String, jsonNode: JsonNode): Boolean {
224 return when (type.toLowerCase()) {
225 BluePrintConstants.DATA_TYPE_LIST -> jsonNode.isArray
226 BluePrintConstants.DATA_TYPE_MAP -> jsonNode.isContainerNode
231 fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) {
232 when (primitiveType.toLowerCase()) {
233 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean)
234 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int)
235 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float)
236 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, value as Double)
237 BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String)
238 else -> objectNode.put(key, value as String)
242 fun populatePrimitiveValues(value: Any, primitiveType: String, arrayNode: ArrayNode) {
243 when (primitiveType.toLowerCase()) {
244 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(value as Boolean)
245 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(value as Int)
246 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(value as Float)
247 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(value as Double)
248 BluePrintConstants.DATA_TYPE_TIMESTAMP -> arrayNode.add(value as String)
249 else -> arrayNode.add(value as String)
253 fun populatePrimitiveDefaultValues(key: String, primitiveType: String, objectNode: ObjectNode) {
254 when (primitiveType.toLowerCase()) {
255 BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, false)
256 BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, 0)
257 BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, 0.0)
258 BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, 0.0)
259 else -> objectNode.put(key, "")
263 fun populatePrimitiveDefaultValuesForArrayNode(primitiveType: String, arrayNode: ArrayNode) {
264 when (primitiveType.toLowerCase()) {
265 BluePrintConstants.DATA_TYPE_BOOLEAN -> arrayNode.add(false)
266 BluePrintConstants.DATA_TYPE_INTEGER -> arrayNode.add(0)
267 BluePrintConstants.DATA_TYPE_FLOAT -> arrayNode.add(0.0)
268 BluePrintConstants.DATA_TYPE_DOUBLE -> arrayNode.add(0.0)
269 else -> arrayNode.add("")
273 fun populateJsonNodeValues(key: String, nodeValue: JsonNode?, type: String, objectNode: ObjectNode) {
274 if (nodeValue == null || nodeValue is NullNode) {
275 objectNode.set(key, nodeValue)
276 } else if (BluePrintTypes.validPrimitiveTypes().contains(type)) {
277 populatePrimitiveValues(key, nodeValue, type, objectNode)
279 objectNode.set(key, nodeValue)
283 fun convertPrimitiveResourceValue(type: String, value: String): JsonNode {
284 return when (type.toLowerCase()) {
285 BluePrintConstants.DATA_TYPE_BOOLEAN -> jsonNodeFromObject(java.lang.Boolean.valueOf(value))
286 BluePrintConstants.DATA_TYPE_INTEGER -> jsonNodeFromObject(Integer.valueOf(value))
287 BluePrintConstants.DATA_TYPE_FLOAT -> jsonNodeFromObject(java.lang.Float.valueOf(value))
288 BluePrintConstants.DATA_TYPE_DOUBLE -> jsonNodeFromObject(java.lang.Double.valueOf(value))
289 else -> getJsonNode(value)