diff options
author | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-03-11 15:53:12 -0400 |
---|---|---|
committer | Alexis de Talhouët <adetalhouet89@gmail.com> | 2019-03-11 16:01:00 -0400 |
commit | 319aab8c07caf4be6f68a18e8d62dde3a3b7d571 (patch) | |
tree | 89d03acc2a84b41879697f9184fb993f501e5bfc /ms/controllerblueprints/modules/blueprint-core | |
parent | 00db9f08a80d7c049f93abbc5742072c7c27afef (diff) |
Mesh input-key-mapping using velocity
Change-Id: Ic7190c86fc4e3f66fe7223c1c3575279c2c1d105
Issue-ID: CCSDK-1131
Signed-off-by: Alexis de Talhouët <adetalhouet89@gmail.com>
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core')
2 files changed, 51 insertions, 27 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintTemplateService.kt index d175fddea..4fa69cad8 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintTemplateService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/service/BluePrintTemplateService.kt @@ -34,34 +34,32 @@ open class BluePrintTemplateService { companion object { /** - * Generate Content from Velocity Template and JSON Content. + * Generate Content from Velocity Template and JSON Content or property map. */ - fun generateContent(template: String, json: String, + fun generateContent(template: String, json: String = "", ignoreJsonNull: Boolean = false, - additionalContext: MutableMap<String, Any> = hashMapOf()): String { + additionalContext: Map<String, Any> = hashMapOf()): String { Velocity.init() val mapper = ObjectMapper() val nodeFactory = BluePrintJsonNodeFactory() - mapper.setNodeFactory(nodeFactory) - - val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java) - ?: throw BluePrintProcessorException("couldn't get json node from json") - - if (ignoreJsonNull) - JacksonUtils.removeJsonNullNode(jsonNode) + mapper.nodeFactory = nodeFactory val velocityContext = VelocityContext() velocityContext.put("StringUtils", StringUtils::class.java) velocityContext.put("BooleanUtils", BooleanUtils::class.java) - /** - * Add the Custom Velocity Context API - */ + + // Add the Custom Velocity Context API additionalContext.forEach { name, value -> velocityContext.put(name, value) } - /** - * Add the JSON Data to the context - */ - jsonNode.fields().forEach { entry -> - velocityContext.put(entry.key, entry.value) + + // Add the JSON Data to the context + if (json.isNotEmpty()) { + val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java) + ?: throw BluePrintProcessorException("couldn't get json node from json") + if (ignoreJsonNull) + JacksonUtils.removeJsonNullNode(jsonNode) + jsonNode.fields().forEach { entry -> + velocityContext.put(entry.key, entry.value) + } } val stringWriter = StringWriter() diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt index 932f0edce..e0341b8a4 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/apps/controllerblueprints/core/utils/JacksonUtils.kt @@ -22,8 +22,13 @@ import com.fasterxml.jackson.annotation.JsonInclude import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.SerializationFeature import com.fasterxml.jackson.databind.node.ArrayNode +import com.fasterxml.jackson.databind.node.BooleanNode +import com.fasterxml.jackson.databind.node.DoubleNode +import com.fasterxml.jackson.databind.node.FloatNode +import com.fasterxml.jackson.databind.node.IntNode import com.fasterxml.jackson.databind.node.NullNode import com.fasterxml.jackson.databind.node.ObjectNode +import com.fasterxml.jackson.databind.node.TextNode import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async @@ -46,7 +51,7 @@ class JacksonUtils { companion object { private val log: EELFLogger = EELFManager.getInstance().getLogger(this::class.toString()) inline fun <reified T : Any> readValue(content: String): T = - jacksonObjectMapper().readValue(content, T::class.java) + jacksonObjectMapper().readValue(content, T::class.java) fun <T> readValue(content: String, valueType: Class<T>): T? { return jacksonObjectMapper().readValue(content, valueType) @@ -84,7 +89,7 @@ class JacksonUtils { return runBlocking { withContext(Dispatchers.Default) { IOUtils.toString(JacksonUtils::class.java.classLoader - .getResourceAsStream(fileName), Charset.defaultCharset()) + .getResourceAsStream(fileName), Charset.defaultCharset()) } } } @@ -184,7 +189,7 @@ class JacksonUtils { fun <T> getInstanceFromMap(properties: MutableMap<String, JsonNode>, classType: Class<T>): T { return readValue(getJson(properties), classType) - ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)") + ?: throw BluePrintProcessorException("failed to transform content ($properties) to type ($classType)") } fun checkJsonNodeValueOfType(type: String, jsonNode: JsonNode): Boolean { @@ -228,14 +233,35 @@ class JacksonUtils { } } + fun getValue(value: JsonNode): Any { + return when (value) { + is BooleanNode -> value.booleanValue() + is IntNode -> value.intValue() + is FloatNode -> value.floatValue() + is DoubleNode -> value.doubleValue() + is TextNode -> value.textValue() + else -> value + } + } + + fun getValue(value: Any, type: String): Any { + return when (type.toLowerCase()) { + BluePrintConstants.DATA_TYPE_BOOLEAN -> (value as BooleanNode).booleanValue() + BluePrintConstants.DATA_TYPE_INTEGER -> (value as IntNode).intValue() + BluePrintConstants.DATA_TYPE_FLOAT -> (value as FloatNode).floatValue() + BluePrintConstants.DATA_TYPE_DOUBLE -> (value as DoubleNode).doubleValue() + BluePrintConstants.DATA_TYPE_STRING -> (value as TextNode).textValue() + else -> (value as JsonNode) + } + } + fun populatePrimitiveValues(key: String, value: Any, primitiveType: String, objectNode: ObjectNode) { when (primitiveType.toLowerCase()) { - BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, value as Boolean) - BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, value as Int) - BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, value as Float) - BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, value as Double) - BluePrintConstants.DATA_TYPE_TIMESTAMP -> objectNode.put(key, value as String) - else -> objectNode.put(key, value as String) + BluePrintConstants.DATA_TYPE_BOOLEAN -> objectNode.put(key, (value as BooleanNode).booleanValue()) + BluePrintConstants.DATA_TYPE_INTEGER -> objectNode.put(key, (value as IntNode).intValue()) + BluePrintConstants.DATA_TYPE_FLOAT -> objectNode.put(key, (value as FloatNode).floatValue()) + BluePrintConstants.DATA_TYPE_DOUBLE -> objectNode.put(key, (value as DoubleNode).doubleValue()) + else -> objectNode.put(key, (value as TextNode).textValue()) } } |