aboutsummaryrefslogtreecommitdiffstats
path: root/ms/controllerblueprints/modules/blueprint-core/src
diff options
context:
space:
mode:
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core/src')
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt12
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt28
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt11
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt41
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt25
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt13
-rw-r--r--ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt58
-rwxr-xr-xms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-data.json3
-rwxr-xr-xms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-velocity-template.vtl2
9 files changed, 138 insertions, 55 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt
index 0e45232f2..583fc9d4d 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt
@@ -122,6 +122,18 @@ fun JsonNode.rootFieldsToMap(): MutableMap<String, JsonNode> {
}
}
+fun JsonNode.removeNullNode() {
+ val it = this.iterator()
+ while (it.hasNext()) {
+ val child = it.next()
+ if (child.isNull) {
+ it.remove()
+ } else {
+ child.removeNullNode()
+ }
+ }
+}
+
fun MutableMap<String, JsonNode>.putJsonElement(key: String, value: Any) {
val convertedValue = value.asJsonType()
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt
index ec1542cd3..86bf3ff56 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt
@@ -18,21 +18,43 @@ package org.onap.ccsdk.cds.controllerblueprints.core.interfaces
import com.fasterxml.jackson.core.io.CharTypes
import com.fasterxml.jackson.databind.node.JsonNodeFactory
import com.fasterxml.jackson.databind.node.TextNode
+import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
interface BlueprintTemplateService {
/**
* Generate dynamique content using Velocity Template or Jinja template
*
+ * @param bluePrintRuntimeService blueprint runtime
+ * @param nodeTemplateName node template
+ * @param artifactName Artifact Name
+ * @param jsonData json string data content to mash
+ * @param ignoreJsonNull Ignore Null value in the JSON content
+ * @param additionalContext (Key, value) mutable map for additional variables
+ * @return Content result
+ *
+ **/
+ suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>,
+ nodeTemplateName: String,
+ artifactName: String,
+ jsonData: String = "",
+ ignoreJsonNull: Boolean = false,
+ additionalContext: MutableMap<String, Any> = mutableMapOf()): String
+
+
+ /**
+ * Generate dynamique content using Velocity Template or Jinja template
+ *
* @param template template string content
- * @param json json string content
+ * @param templateType template type
+ * @param jsonData json string data content to mash
* @param ignoreJsonNull Ignore Null value in the JSON content
* @param additionalContext (Key, value) mutable map for additional variables
* @return Content result
*
**/
- suspend fun generateContent(template: String, json: String = "", ignoreJsonNull: Boolean = false,
- additionalContext: MutableMap<String, Any> = mutableMapOf()): String
+ suspend fun generateContent(template: String, templateType: String, jsonData: String = "", ignoreJsonNull: Boolean = false,
+ additionalContext: MutableMap<String, Any> = mutableMapOf()): String
}
/**
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
index f835cbe52..912667e0a 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
@@ -21,14 +21,13 @@ import com.fasterxml.jackson.databind.ObjectMapper
import com.hubspot.jinjava.Jinjava
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory
-import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService
-import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode
-object BluePrintJinjaTemplateService: BlueprintTemplateService {
+object BluePrintJinjaTemplateService {
- override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
- additionalContext: MutableMap<String, Any>): String {
+ fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
+ additionalContext: MutableMap<String, Any>): String {
// Load template
val jinJava = Jinjava()
val mapper = ObjectMapper()
@@ -40,7 +39,7 @@ object BluePrintJinjaTemplateService: BlueprintTemplateService {
val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
?: throw BluePrintProcessorException("couldn't get json node from json")
if (ignoreJsonNull)
- JacksonUtils.removeJsonNullNode(jsonNode)
+ jsonNode.removeNullNode()
jsonNode.fields().forEach { entry ->
additionalContext[entry.key] = entry.value
}
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt
index 4b6905bff..43e6d221f 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt
@@ -26,35 +26,50 @@ import org.apache.velocity.VelocityContext
import org.apache.velocity.app.Velocity
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory
-import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService
-import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode
import java.io.StringWriter
-object BluePrintVelocityTemplateService: BlueprintTemplateService {
+object BluePrintVelocityTemplateService {
/**
- * Generate Content from Velocity Template and JSON Content or property map.
+ * Generate Content from Velocity Template and JSON Content with injected API
*/
- override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, additionalContext:
- MutableMap<String, Any>): String {
- Velocity.init()
+ fun generateContent(template: String, json: String, ignoreJsonNull: Boolean = false,
+ additionalContext: MutableMap<String, Any> = mutableMapOf()): String {
+
+ // Customized Object Mapper to remove String double quotes
val mapper = ObjectMapper()
val nodeFactory = BluePrintJsonNodeFactory()
mapper.nodeFactory = nodeFactory
+ val jsonNode: JsonNode? = if (json.isNotEmpty()) {
+ mapper.readValue(json, JsonNode::class.java)
+ ?: throw BluePrintProcessorException("couldn't get json node from json")
+ } else {
+ null
+ }
+ return generateContent(template, jsonNode, ignoreJsonNull, additionalContext)
+ }
+
+ /**
+ * Generate Content from Velocity Template and JSON Node with injected API
+ */
+ fun generateContent(template: String, jsonNode: JsonNode?, ignoreJsonNull: Boolean = false,
+ additionalContext: MutableMap<String, Any> = mutableMapOf()): String {
+
+ Velocity.init()
+
val velocityContext = VelocityContext()
velocityContext.put("StringUtils", StringUtils::class.java)
velocityContext.put("BooleanUtils", BooleanUtils::class.java)
// Add the Custom Velocity Context API
- additionalContext.forEach { name, value -> velocityContext.put(name, value) }
+ additionalContext.forEach { (name, value) -> velocityContext.put(name, 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 (jsonNode != null) {
if (ignoreJsonNull)
- JacksonUtils.removeJsonNullNode(jsonNode)
+ jsonNode.removeNullNode()
jsonNode.fields().forEach { entry ->
velocityContext.put(entry.key, entry.value)
}
@@ -64,6 +79,6 @@ object BluePrintVelocityTemplateService: BlueprintTemplateService {
Velocity.evaluate(velocityContext, stringWriter, "TemplateData", template)
stringWriter.flush()
return stringWriter.toString()
+
}
}
-
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt
index 73457b986..45e2678ed 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt
@@ -20,19 +20,26 @@ import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
-class BluePrintTemplateService(val bluePrintRuntimeService: BluePrintRuntimeService<*>,
- val nodeTemplateName: String, val artifactName: String):
- BlueprintTemplateService {
+class BluePrintTemplateService : BlueprintTemplateService {
+
+ override suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>,
+ nodeTemplateName: String, artifactName: String, jsonData: String,
+ ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String {
- override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String {
val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
val templateType = artifactDefinition.type
+ val template = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
+ return generateContent(template, templateType, jsonData, ignoreJsonNull, additionalContext)
+ }
+
+ override suspend fun generateContent(template: String, templateType: String, jsonData: String, ignoreJsonNull: Boolean,
+ additionalContext: MutableMap<String, Any>): String {
return when (templateType) {
BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME -> {
- BluePrintJinjaTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext)
+ BluePrintJinjaTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext)
}
BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME -> {
- BluePrintVelocityTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext)
+ BluePrintVelocityTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext)
}
else -> {
throw BluePrintProcessorException("Unknown Artifact type, expecting ${BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME}" +
@@ -41,9 +48,11 @@ class BluePrintTemplateService(val bluePrintRuntimeService: BluePrintRuntimeServ
}
}
- suspend fun generateContentFromFiles(templatePath: String, jsonPath: String, ignoreJsonNull: Boolean, additionalContext: MutableMap<String, Any>): String {
+ suspend fun generateContentFromFiles(templatePath: String, templateType: String, jsonPath: String,
+ ignoreJsonNull: Boolean,
+ additionalContext: MutableMap<String, Any>): String {
val json = JacksonUtils.getClassPathFileContent(jsonPath)
val template = JacksonUtils.getClassPathFileContent(templatePath)
- return generateContent(template, json, ignoreJsonNull, additionalContext)
+ return generateContent(template, templateType, json, ignoreJsonNull, additionalContext)
}
} \ No newline at end of file
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt
index 7b5f181da..5e9fd6207 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JacksonUtils.kt
@@ -58,19 +58,6 @@ class JacksonUtils {
return objectMapper.treeToValue(node, valueType)
}
- fun removeJsonNullNode(node: JsonNode) {
- val it = node.iterator()
- while (it.hasNext()) {
- val child = it.next()
- if (child.isNull) {
- it.remove()
- } else {
- removeJsonNullNode(child)
- }
- }
- }
-
-
fun getContent(fileName: String): String = runBlocking {
try {
normalizedFile(fileName).readNBText()
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
index e4227180b..6f961c8ed 100644
--- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
+++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
@@ -21,10 +21,12 @@ package org.onap.ccsdk.cds.controllerblueprints.core.service
import kotlinx.coroutines.runBlocking
import org.junit.Test
import org.junit.runner.RunWith
+import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
import org.springframework.test.context.junit4.SpringRunner
import kotlin.test.BeforeTest
+import kotlin.test.assertEquals
import kotlin.test.assertNotNull
@RunWith(SpringRunner::class)
@@ -40,13 +42,13 @@ class BluePrintTemplateServiceTest {
@Test
fun testVelocityGeneratedContent() {
- runBlocking {
- val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl")
- val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json")
+ runBlocking {
+ val template = JacksonUtils.getClassPathFileContent("templates/base-config-velocity-template.vtl")
+ val json = JacksonUtils.getClassPathFileContent("templates/base-config-data-velocity.json")
- val content = BluePrintVelocityTemplateService.generateContent(template, json)
- assertNotNull(content, "failed to generate content for velocity template")
- }
+ val content = BluePrintVelocityTemplateService.generateContent(template, json)
+ assertNotNull(content, "failed to generate content for velocity template")
+ }
}
@@ -68,13 +70,12 @@ class BluePrintTemplateServiceTest {
@Test
fun testVelocityGeneratedContentFromFiles() {
runBlocking {
- val bluePrintTemplateService = BluePrintTemplateService(blueprintRuntime,
- "resource-assignment", "baseconfig-template")
+ val bluePrintTemplateService = BluePrintTemplateService()
val templateFile = "templates/base-config-velocity-template.vtl"
val jsonFile = "templates/base-config-data-velocity.json"
val content = bluePrintTemplateService.generateContentFromFiles(
- templateFile, jsonFile, false, mutableMapOf())
+ templateFile, BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf())
assertNotNull(content, "failed to generate content for velocity template")
}
@@ -86,17 +87,50 @@ class BluePrintTemplateServiceTest {
var element: MutableMap<String, Any> = mutableMapOf()
element["additional_array"] = arrayListOf(hashMapOf("name" to "Element1", "location" to "Region0"), hashMapOf("name" to "Element2", "location" to "Region1"))
- val bluePrintTemplateService = BluePrintTemplateService(blueprintRuntime,
- "resource-assignment", "another-template")
+ val bluePrintTemplateService = BluePrintTemplateService()
val templateFile = "templates/base-config-jinja-template.jinja"
val jsonFile = "templates/base-config-data-jinja.json"
val content = bluePrintTemplateService.generateContentFromFiles(
- templateFile,
+ templateFile, BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME,
jsonFile, false, element)
assertNotNull(content, "failed to generate content for velocity template")
}
}
+
+ @Test
+ fun `no value variable should evaluate to default value - standalone template mesh test`() {
+ runBlocking {
+ val template = JacksonUtils.getClassPathFileContent("templates/default-variable-value-velocity-template.vtl")
+ val json = JacksonUtils.getClassPathFileContent("templates/default-variable-value-data.json")
+
+ val content = BluePrintVelocityTemplateService.generateContent(template, json)
+ //first line represents a variable whose value was successfully retrieved, second line contains a variable
+ // whose value could not be evaluated
+ val expected = "sample-hostname\n\${node0_backup_router_address}"
+ assertEquals(expected, content, "No value variable should use default value")
+ }
+ }
+
+ @Test
+ fun `no value variable should evaluate to default value - blueprint processing test`() {
+ runBlocking {
+ val bluePrintTemplateService = BluePrintTemplateService()
+
+ val templateFile = "templates/default-variable-value-velocity-template.vtl"
+ val jsonFile = "templates/default-variable-value-data.json"
+
+ val content = bluePrintTemplateService.generateContentFromFiles(templateFile,
+ BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME, jsonFile, false, mutableMapOf())
+
+ //first line represents a variable whose value was successfully retrieved, second line contains a variable
+ // whose value could not be evaluated
+ val expected = "sample-hostname\n\${node0_backup_router_address}"
+ assertEquals(expected, content, "No value variable should use default value")
+ }
+
+ }
+
}
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-data.json b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-data.json
new file mode 100755
index 000000000..940ca8d73
--- /dev/null
+++ b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-data.json
@@ -0,0 +1,3 @@
+{
+ "node0_hostname": "sample-hostname"
+}
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-velocity-template.vtl b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-velocity-template.vtl
new file mode 100755
index 000000000..ce2458e2e
--- /dev/null
+++ b/ms/controllerblueprints/modules/blueprint-core/src/test/resources/templates/default-variable-value-velocity-template.vtl
@@ -0,0 +1,2 @@
+$node0_hostname
+${node0_backup_router_address} \ No newline at end of file