Enable inline templating in mapping files 95/105695/1
authorJozsef Csongvai <jozsef.csongvai@bell.ca>
Wed, 8 Apr 2020 22:49:00 +0000 (18:49 -0400)
committerJozsef Csongvai <jozsef.csongvai@bell.ca>
Thu, 9 Apr 2020 16:56:17 +0000 (12:56 -0400)
Issue-ID: CCSDK-2306
Change-Id: I9657eb73b48659edfc1adfd842ed96f35a095197
Signed-off-by: Jozsef Csongvai <jozsef.csongvai@bell.ca>
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentRuntimeService.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionConstants.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt

index a65ec58..8087f7e 100644 (file)
@@ -23,6 +23,10 @@ class ResourceAssignmentRuntimeService(private var id: String, private var blueP
         resourceStore[key] = value
     }
 
+    fun getResolutionStore(): MutableMap<String, JsonNode> {
+        return resourceStore.mapValues { e -> e.value.deepCopy() as JsonNode }.toMutableMap()
+    }
+
     fun getResolutionStore(key: String): JsonNode {
         return resourceStore[key]
             ?: throw BluePrintProcessorException("failed to get execution property ($key)")
index bfa23cc..497b268 100644 (file)
@@ -39,6 +39,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
 import org.onap.ccsdk.cds.controllerblueprints.core.nullToEmpty
 import org.onap.ccsdk.cds.controllerblueprints.core.rootFieldsToMap
 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
+import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonReactorUtils
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.PropertyDefinitionUtils.Companion.hasLogProtect
@@ -127,6 +128,32 @@ class ResourceAssignmentUtils {
             raRuntimeService.putResolutionStore(resourceAssignment.name, value)
             raRuntimeService.putDictionaryStore(resourceAssignment.dictionaryName!!, value)
             resourceAssignment.property!!.value = value
+
+            val metadata = resourceAssignment.property?.metadata
+            metadata?.get(ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE)
+                    ?.let { if (it.contains("$")) it else null }
+                    ?.let { template ->
+                        val resolutionStore = raRuntimeService.getResolutionStore()
+                                .mapValues { e -> e.value.asText() } as MutableMap<String, Any>
+                        val newValue: JsonNode
+                        try {
+                            newValue = BluePrintVelocityTemplateService
+                                    .generateContent(template, null, true, resolutionStore)
+                                    .also { if (hasLogProtect(metadata))
+                                        logger.info("Transformed value: $resourceAssignment.name")
+                                    else
+                                        logger.info("Transformed value: $value -> $it") }
+                                    .let { v -> v.asJsonType() }
+                        } catch (e: Exception) {
+                            throw BluePrintProcessorException(
+                                    "transform-template failed: $template", e)
+                        }
+                        with(resourceAssignment) {
+                            raRuntimeService.putResolutionStore(this.name, newValue)
+                            raRuntimeService.putDictionaryStore(this.dictionaryName!!, newValue)
+                            this.property!!.value = newValue
+                        }
+                    }
         }
 
         fun setFailedResourceDataValue(resourceAssignment: ResourceAssignment, message: String?) {
index 27adb51..7746b5c 100644 (file)
@@ -29,6 +29,7 @@ import kotlinx.coroutines.runBlocking
 import org.junit.Before
 import org.junit.Test
 import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceAssignmentRuntimeService
+import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.ResourceResolutionConstants.METADATA_TRANSFORM_TEMPLATE
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
@@ -333,6 +334,26 @@ class ResourceAssignmentUtilsTest {
         )
     }
 
+    @Test
+    fun `transform resolved value with inline template`() {
+        resourceAssignmentRuntimeService.putResolutionStore("vnf_name", "abc-vnf".asJsonType())
+        resourceAssignment = ResourceAssignment()
+        resourceAssignment.name = "int_pktgen_private_net_id"
+        resourceAssignment.property = PropertyDefinition()
+        resourceAssignment.property!!.type = "string"
+        val value = "".asJsonType()
+
+        // Enable transform template
+        resourceAssignment.property!!.metadata =
+                mutableMapOf(METADATA_TRANSFORM_TEMPLATE to "\${vnf_name}_private2")
+
+        ResourceAssignmentUtils
+                .setResourceDataValue(resourceAssignment, resourceAssignmentRuntimeService, value)
+
+        assertEquals("abc-vnf_private2",
+                resourceAssignment.property!!.value!!.asText())
+    }
+
     private fun initInputMapAndExpectedValuesForPrimitiveType() {
         inputMapToTestPrimitiveTypeWithValue = "1.2.3.1".asJsonType()
         val keyValue = mutableMapOf<String, String>()