summaryrefslogtreecommitdiffstats
path: root/ms
diff options
context:
space:
mode:
authorSingal, Kapil (ks220y) <ks220y@att.com>2023-05-08 09:10:58 -0400
committerKAPIL SINGAL <ks220y@att.com>2023-05-08 13:46:41 +0000
commit26628bf766b14d66f683a4608b97b7538ca582d6 (patch)
tree980a42b431ce7a193b757a9f0d744b15b2f470c2 /ms
parent2c31f21221231028fad943a231ee2d4c4fe256e1 (diff)
Enhancing BluePrintJinjaTemplateService
* Using CustomJinjavaInterpreter to override getAsString API, and returning context.currentNode.master.image instead of empty string * Earlier rendered result was not keeping unresolved variable rather replacing with empty string, and above helps keeping those. * Upgrading jinja.version to 2.7.0 from 2.5.1 and adding related Junit Test Issue-ID: CCSDK-3895 Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com> Change-Id: I00da64811883957f44f819a44405e073ed7a7755
Diffstat (limited to 'ms')
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt49
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt24
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template-resolved.jinja6
-rw-r--r--ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template.jinja6
-rwxr-xr-xms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-value-data.json3
-rwxr-xr-xms/blueprintsprocessor/parent/pom.xml4
6 files changed, 70 insertions, 22 deletions
diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
index fdc348bf7..d3a7f0fe0 100644
--- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
+++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt
@@ -21,9 +21,12 @@ import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.hubspot.jinjava.Jinjava
import com.hubspot.jinjava.JinjavaConfig
+import com.hubspot.jinjava.interpret.Context
+import com.hubspot.jinjava.interpret.InterpreterFactory
import com.hubspot.jinjava.interpret.JinjavaInterpreter
import com.hubspot.jinjava.loader.ResourceLocator
import com.hubspot.jinjava.loader.ResourceNotFoundException
+import com.hubspot.jinjava.objects.serialization.PyishObjectMapper
import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory
@@ -33,6 +36,7 @@ import java.io.IOException
import java.nio.charset.Charset
import java.nio.file.Files.readAllBytes
import java.nio.file.Paths
+import java.util.Objects
object BluePrintJinjaTemplateService {
@@ -89,27 +93,46 @@ object BluePrintJinjaTemplateService {
additionalContext: MutableMap<String, Any>,
resourceLocator: ResourceLocator? = null
): String {
- val jinJava = Jinjava(JinjavaConfig())
- if (resourceLocator != null) {
- jinJava.resourceLocator = resourceLocator
- }
-
- val mapper = ObjectMapper()
- val nodeFactory = BluePrintJsonNodeFactory()
- mapper.nodeFactory = nodeFactory
-
// Add the JSON Data to the context
if (json.isNotEmpty()) {
- val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
+ val mapper = ObjectMapper().setNodeFactory(BluePrintJsonNodeFactory())
+ val jsonNode = mapper.readValue(json, JsonNode::class.java)
?: throw BluePrintProcessorException("couldn't get json node from json")
if (ignoreJsonNull) {
jsonNode.removeNullNode()
}
+ additionalContext.putAll(mapper.readValue(json, object : TypeReference<Map<String, Any>>() {}))
+ }
+
+ val jinjava = Jinjava(
+ JinjavaConfig(object : InterpreterFactory {
+ override fun newInstance(interpreter: JinjavaInterpreter): JinjavaInterpreter {
+ return CustomJinjavaInterpreter(interpreter)
+ }
- val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
- additionalContext.putAll(jsonMap)
+ override fun newInstance(jinjava: Jinjava, context: Context, config: JinjavaConfig): JinjavaInterpreter {
+ return CustomJinjavaInterpreter(jinjava, context, config)
+ }
+ })
+ )
+
+ if (resourceLocator != null) {
+ jinjava.resourceLocator = resourceLocator
}
- return jinJava.render(template, additionalContext)
+ return jinjava.render(template, additionalContext)
+ }
+
+ class CustomJinjavaInterpreter : JinjavaInterpreter {
+ constructor(interpreter: JinjavaInterpreter) : super(interpreter)
+ constructor(jinjava: Jinjava, context: Context, config: JinjavaConfig) : super(jinjava, context, config)
+
+ // Overriding actual getAsString method to return `context.currentNode.master.image` instead of empty string
+ override fun getAsString(`object`: Any?): String {
+ return if (config.legacyOverrides.isUsePyishObjectMapper)
+ PyishObjectMapper.getAsUnquotedPyishString(`object`)
+ else
+ Objects.toString(`object`, context.currentNode.master.image)
+ }
}
}
diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
index 0e93ccf6b..87591e1c9 100644
--- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
+++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateServiceTest.kt
@@ -61,22 +61,36 @@ class BluePrintTemplateServiceTest {
)
val content = BluePrintJinjaTemplateService.generateContent(template, json, false, element)
- assertNotNull(content, "failed to generate content for velocity template")
+ assertNotNull(content, "failed to generate content for jinja template")
}
}
@Test
- fun `no value variable should evaluate to default value - standalone template mesh test`() {
+ fun `Unresolved variable should be kept as-is - standalone velocity 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}"
+ // first line represents a variable whose value was successfully retrieved,
+ // second line contains a variable whose value could not be evaluated
+ val expected = "sample-node0_hostname\n\${node0_backup_router_address}"
assertEquals(expected, content, "No value variable should use default value")
}
}
+
+ @Test
+ fun `Unresolved variable should be kept as-is - standalone jinja template mesh test`() {
+ runBlocking {
+ val context = JacksonUtils.getClassPathFileContent("templates/default-variable-value-data.json")
+ val jinjaTemplate =
+ JacksonUtils.getClassPathFileContent("templates/default-variable-jinja-template.jinja")
+ val renderedContent =
+ BluePrintJinjaTemplateService.generateContent(jinjaTemplate, context, false, mutableMapOf())
+ val expectedContent =
+ JacksonUtils.getClassPathFileContent("templates/default-variable-jinja-template-resolved.jinja")
+ assertEquals(expectedContent, renderedContent, "No value variable should use default value")
+ }
+ }
}
diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template-resolved.jinja b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template-resolved.jinja
new file mode 100644
index 000000000..b9617d677
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template-resolved.jinja
@@ -0,0 +1,6 @@
+sample-node0_hostname
+{{ node1_hostname }}
+This will be kept as-is {{ node0_backup_router_address }}
+sample-node0_hostname
+Even this one {{ node1_backup_router_address }} as it doesn't matter if variable is surrounded by text
+sample-node2_backup_router_address {{ node1_backup_router_address }} Even this One if multiple variable in same line
diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template.jinja b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template.jinja
new file mode 100644
index 000000000..5d483e383
--- /dev/null
+++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-jinja-template.jinja
@@ -0,0 +1,6 @@
+{{ node0_hostname }}
+{{ node1_hostname }}
+This will be kept as-is {{ node0_backup_router_address }}
+{{ node0_hostname }}
+Even this one {{ node1_backup_router_address }} as it doesn't matter if variable is surrounded by text
+{{ node2_backup_router_address }} {{ node1_backup_router_address }} Even this One if multiple variable in same line
diff --git a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-value-data.json b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-value-data.json
index 940ca8d73..e3bc35525 100755
--- a/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-value-data.json
+++ b/ms/blueprintsprocessor/modules/blueprints/blueprint-core/src/test/resources/templates/default-variable-value-data.json
@@ -1,3 +1,4 @@
{
- "node0_hostname": "sample-hostname"
+ "node0_hostname": "sample-node0_hostname",
+ "node2_backup_router_address": "sample-node2_backup_router_address"
}
diff --git a/ms/blueprintsprocessor/parent/pom.xml b/ms/blueprintsprocessor/parent/pom.xml
index 3de8e9be5..7db6003bd 100755
--- a/ms/blueprintsprocessor/parent/pom.xml
+++ b/ms/blueprintsprocessor/parent/pom.xml
@@ -44,13 +44,11 @@
<json.unit.version>2.8.0</json.unit.version>
<xmlunit.version>2.6.3</xmlunit.version>
-
-
<sshd.version>2.2.0</sshd.version>
<jsch.version>0.1.55</jsch.version>
<jslt.version>0.1.8</jslt.version>
<jython.version>2.7.2</jython.version>
- <jinja.version>2.5.1</jinja.version>
+ <jinja.version>2.7.0</jinja.version>
<guava.version>27.0.1-jre</guava.version>
<json-patch.version>1.9</json-patch.version>
<json-smart.version>2.4.6</json-smart.version>