From 7ad72c86fbd10888a849eed2b00dc9fddadef5aa Mon Sep 17 00:00:00 2001 From: Alexis de Talhouët Date: Tue, 18 Jun 2019 19:43:50 -0400 Subject: Add Jinja2 custom ResourceLocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will allow to include template within template to create template hierarchy Change-Id: I21c5deaf51d391e1a51b9863a905c26b1891db16 Issue-ID: CCSDK-1417 Signed-off-by: Alexis de Talhouët --- .../core/interfaces/BlueprintTemplateService.kt | 15 ------ .../core/service/BluePrintJinjaTemplateService.kt | 59 +++++++++++++++++++++- .../core/service/BlueprintTemplateService.kt | 29 +++++------ 3 files changed, 71 insertions(+), 32 deletions(-) (limited to 'ms/controllerblueprints/modules/blueprint-core/src/main') 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 86bf3ff56..98abf8987 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 @@ -40,21 +40,6 @@ interface BlueprintTemplateService { jsonData: String = "", ignoreJsonNull: Boolean = false, additionalContext: MutableMap = mutableMapOf()): String - - - /** - * Generate dynamique content using Velocity Template or Jinja template - * - * @param template template 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, templateType: String, jsonData: String = "", ignoreJsonNull: Boolean = false, - additionalContext: MutableMap = 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 1dbbd9977..baddd6a12 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 @@ -19,18 +19,73 @@ package org.onap.ccsdk.cds.controllerblueprints.core.service import com.fasterxml.jackson.core.type.TypeReference import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.ObjectMapper +import com.google.common.io.Resources import com.hubspot.jinjava.Jinjava +import com.hubspot.jinjava.interpret.Context +import com.hubspot.jinjava.interpret.JinjavaInterpreter +import com.hubspot.jinjava.loader.ClasspathResourceLocator +import com.hubspot.jinjava.loader.ResourceLocator +import com.hubspot.jinjava.loader.ResourceNotFoundException import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration +import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory +import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile +import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode +import org.springframework.context.annotation.Scope +import org.springframework.stereotype.Service +import java.io.IOException +import java.nio.charset.Charset +import java.nio.file.Files.readAllBytes +import java.nio.file.Paths object BluePrintJinjaTemplateService { + /** + * To enable inheritance within CBA, we need Jinja runtime to know where to load the templates. + */ + class BlueprintRelatedTemplateLocator(private val bluePrintPathConfiguration: BluePrintPathConfiguration, + private val artifactName: String, + private val artifactVersion: String) : ResourceLocator { + + @Throws(IOException::class) + override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String { + try { + val deployFile = + normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath, + artifactName, + artifactVersion, + fullName) + + return String(readAllBytes(Paths.get(deployFile))) + } catch (var5: IllegalArgumentException) { + throw ResourceNotFoundException("Couldn't find resource: $fullName") + } + + } + } + fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, - additionalContext: MutableMap): String { + additionalContext: MutableMap, + bluePrintPathConfiguration: BluePrintPathConfiguration, artifactName: String, + artifactVersion: String): String { - // Load template + + return generateContent(template, + json, + ignoreJsonNull, + additionalContext, + BlueprintRelatedTemplateLocator(bluePrintPathConfiguration, artifactName, artifactVersion)) + } + + fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, + additionalContext: MutableMap, resourceLocator: ResourceLocator? = null): String { val jinJava = Jinjava() + if (resourceLocator != null) { + jinJava.resourceLocator = resourceLocator + } + val mapper = ObjectMapper() val nodeFactory = BluePrintJsonNodeFactory() mapper.nodeFactory = nodeFactory 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 45e2678ed..af97d6691 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 @@ -17,26 +17,33 @@ package org.onap.ccsdk.cds.controllerblueprints.core.service import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException +import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintTemplateService import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils +import org.springframework.stereotype.Service -class BluePrintTemplateService : BlueprintTemplateService { +@Service +class BluePrintTemplateService(private val bluePrintPathConfiguration: BluePrintPathConfiguration) : + BlueprintTemplateService { override suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>, nodeTemplateName: String, artifactName: String, jsonData: String, ignoreJsonNull: Boolean, additionalContext: MutableMap): String { - val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName) + 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 { return when (templateType) { BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME -> { - BluePrintJinjaTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext) + BluePrintJinjaTemplateService.generateContent(template, + jsonData, + ignoreJsonNull, + additionalContext, + bluePrintPathConfiguration, + bluePrintRuntimeService.bluePrintContext().name(), + bluePrintRuntimeService.bluePrintContext().version()) } BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME -> { BluePrintVelocityTemplateService.generateContent(template, jsonData, ignoreJsonNull, additionalContext) @@ -47,12 +54,4 @@ class BluePrintTemplateService : BlueprintTemplateService { } } } - - suspend fun generateContentFromFiles(templatePath: String, templateType: String, jsonPath: String, - ignoreJsonNull: Boolean, - additionalContext: MutableMap): String { - val json = JacksonUtils.getClassPathFileContent(jsonPath) - val template = JacksonUtils.getClassPathFileContent(templatePath) - return generateContent(template, templateType, json, ignoreJsonNull, additionalContext) - } } \ No newline at end of file -- cgit 1.2.3-korg