From 3109998de219adb6488ff28c9ab4e9bf6b444e57 Mon Sep 17 00:00:00 2001 From: Jozsef Csongvai Date: Wed, 8 Apr 2020 18:49:00 -0400 Subject: Enable inline templating in mapping files Issue-ID: CCSDK-2306 Change-Id: I9657eb73b48659edfc1adfd842ed96f35a095197 Signed-off-by: Jozsef Csongvai --- .../resolution/ResourceAssignmentRuntimeService.kt | 4 ++++ .../resolution/ResourceResolutionConstants.kt | 1 + .../resolution/utils/ResourceAssignmentUtils.kt | 27 ++++++++++++++++++++++ .../utils/ResourceAssignmentUtilsTest.kt | 21 +++++++++++++++++ 4 files changed, 53 insertions(+) (limited to 'ms/blueprintsprocessor/functions/resource-resolution') diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentRuntimeService.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentRuntimeService.kt index a65ec585b..8087f7e3f 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentRuntimeService.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceAssignmentRuntimeService.kt @@ -23,6 +23,10 @@ class ResourceAssignmentRuntimeService(private var id: String, private var blueP resourceStore[key] = value } + fun getResolutionStore(): MutableMap { + 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)") diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionConstants.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionConstants.kt index 8f6069160..b934940b1 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionConstants.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/ResourceResolutionConstants.kt @@ -30,4 +30,5 @@ object ResourceResolutionConstants { const val RESOURCE_RESOLUTION_INPUT_RESOURCE_ID = "resource-id" const val RESOURCE_RESOLUTION_INPUT_RESOURCE_TYPE = "resource-type" const val RESOURCE_RESOLUTION_INPUT_RESOLUTION_SUMMARY = "resolution-summary" + const val METADATA_TRANSFORM_TEMPLATE = "transform-template" } diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt index bfa23ccc2..497b26803 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtils.kt @@ -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 + 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?) { diff --git a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt index 27adb51f4..7746b5c41 100644 --- a/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt +++ b/ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/utils/ResourceAssignmentUtilsTest.kt @@ -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() -- cgit 1.2.3-korg