diff options
author | Steve Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-04-03 15:23:27 -0400 |
---|---|---|
committer | Steve Siani <alphonse.steve.siani.djissitchi@ibm.com> | 2019-04-09 13:08:39 -0400 |
commit | f7d891db891f4fb8db103236d4010de1b7739378 (patch) | |
tree | c7a7fff4bca0ec2ac3536442463ad8494fd07196 /ms/controllerblueprints/modules/blueprint-core/src/main | |
parent | f2b17fe1579222ffc251d48bf9475dc3fcfc1206 (diff) |
Jinja template for Blueprint template service
Change-Id: Iec777e4500c2a040faccc8375b1d2dd24c27cb7f
Issue-ID: CCSDK-1193
Signed-off-by: Steve Siani <alphonse.steve.siani.djissitchi@ibm.com>
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core/src/main')
6 files changed, 229 insertions, 94 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt index 23c52b4b3..9a652fb92 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/BluePrintConstants.kt @@ -1,6 +1,6 @@ /* * Copyright © 2017-2018 AT&T Intellectual Property. - * Modifications Copyright © 2018 IBM. + * Modifications Copyright © 2018 - 2019 IBM, Bell Canada * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -180,5 +180,6 @@ object BluePrintConstants { const val PROPERTY_CURRENT_IMPLEMENTATION = "current-implementation" const val PROPERTY_EXECUTION_REQUEST = "execution-request" - + const val ARTIFACT_VELOCITY_TYPE_NAME = "artifact-template-velocity" + const val ARTIFACT_JINJA_TYPE_NAME = "artifact-template-jinja" }
\ No newline at end of file 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 new file mode 100644 index 000000000..ec1542cd3 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/interfaces/BlueprintTemplateService.kt @@ -0,0 +1,56 @@ +/* + * Copyright © 2019 IBM, Bell Canada. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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 + +interface BlueprintTemplateService { + + /** + * Generate dynamique content using Velocity Template or Jinja template + * + * @param template template string content + * @param json json string content + * @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 +} + +/** + * Customise JsonNodeFactory and TextNode, Since it introduces quotes for string data. + */ +open class BluePrintJsonNodeFactory : JsonNodeFactory() { + override fun textNode(text: String): TextNode { + return BluePrintTextNode(text) + } +} + +open class BluePrintTextNode(v: String) : TextNode(v) { + override fun toString(): String { + var len = this._value.length + len = len + 2 + (len shr 4) + val sb = StringBuilder(len) + CharTypes.appendQuoted(sb, this._value) + return sb.toString() + } + +}
\ No newline at end of file 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 new file mode 100644 index 000000000..f835cbe52 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintJinjaTemplateService.kt @@ -0,0 +1,52 @@ +/* + * Copyright © 2019 IBM, Bell Canada. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.controllerblueprints.core.service + +import com.fasterxml.jackson.databind.JsonNode +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 + + +object BluePrintJinjaTemplateService: BlueprintTemplateService { + + override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, + additionalContext: MutableMap<String, Any>): String { + // Load template + val jinJava = Jinjava() + 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) + ?: throw BluePrintProcessorException("couldn't get json node from json") + if (ignoreJsonNull) + JacksonUtils.removeJsonNullNode(jsonNode) + jsonNode.fields().forEach { entry -> + additionalContext[entry.key] = entry.value + } + } + + return jinJava.render(template, additionalContext.toMap()) + } +} + 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 deleted file mode 100644 index 4e2184d3f..000000000 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintTemplateService.kt +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Copyright © 2017-2018 AT&T Intellectual Property. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.onap.ccsdk.cds.controllerblueprints.core.service - -import com.fasterxml.jackson.core.io.CharTypes -import com.fasterxml.jackson.databind.JsonNode -import com.fasterxml.jackson.databind.ObjectMapper -import com.fasterxml.jackson.databind.node.JsonNodeFactory -import com.fasterxml.jackson.databind.node.TextNode -import org.apache.commons.lang3.BooleanUtils -import org.apache.commons.lang3.StringUtils -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.utils.JacksonUtils -import java.io.StringWriter - -open class BluePrintTemplateService { - - companion object { - - /** - * Generate Content from Velocity Template and JSON Content or property map. - */ - fun generateContent(template: String, json: String = "", - ignoreJsonNull: Boolean = false, - additionalContext: Map<String, Any> = hashMapOf()): String { - Velocity.init() - val mapper = ObjectMapper() - val nodeFactory = BluePrintJsonNodeFactory() - mapper.nodeFactory = nodeFactory - - 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) } - - // 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 (ignoreJsonNull) - JacksonUtils.removeJsonNullNode(jsonNode) - jsonNode.fields().forEach { entry -> - velocityContext.put(entry.key, entry.value) - } - } - - val stringWriter = StringWriter() - Velocity.evaluate(velocityContext, stringWriter, "TemplateData", template) - stringWriter.flush() - return stringWriter.toString() - } - } -} - -/** - * Customise JsonNodeFactory adn TextNode, Since it introduces quotes for string data. - */ -open class BluePrintJsonNodeFactory : JsonNodeFactory() { - override fun textNode(text: String): TextNode { - return BluePrintTextNode(text) - } -} - -open class BluePrintTextNode(v: String) : TextNode(v) { - override fun toString(): String { - var len = this._value.length - len = len + 2 + (len shr 4) - val sb = StringBuilder(len) - CharTypes.appendQuoted(sb, this._value) - return sb.toString() - } - -} - 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 new file mode 100644 index 000000000..4b6905bff --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BluePrintVelocityTemplateService.kt @@ -0,0 +1,69 @@ +/* + * Copyright © 2017-2018 AT&T Intellectual Property. + * + * Modifications Copyright © 2019 IBM, Bell Canada. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.onap.ccsdk.cds.controllerblueprints.core.service + +import com.fasterxml.jackson.databind.JsonNode +import com.fasterxml.jackson.databind.ObjectMapper +import org.apache.commons.lang3.BooleanUtils +import org.apache.commons.lang3.StringUtils +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 java.io.StringWriter + +object BluePrintVelocityTemplateService: BlueprintTemplateService { + + /** + * Generate Content from Velocity Template and JSON Content or property map. + */ + override suspend fun generateContent(template: String, json: String, ignoreJsonNull: Boolean, additionalContext: + MutableMap<String, Any>): String { + Velocity.init() + val mapper = ObjectMapper() + val nodeFactory = BluePrintJsonNodeFactory() + mapper.nodeFactory = nodeFactory + + 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) } + + // 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 (ignoreJsonNull) + JacksonUtils.removeJsonNullNode(jsonNode) + jsonNode.fields().forEach { entry -> + velocityContext.put(entry.key, entry.value) + } + } + + val stringWriter = StringWriter() + 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 new file mode 100644 index 000000000..73457b986 --- /dev/null +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/BlueprintTemplateService.kt @@ -0,0 +1,49 @@ +/* + * Copyright © 2019 IBM, Bell Canada + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +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.interfaces.BlueprintTemplateService +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils + +class BluePrintTemplateService(val bluePrintRuntimeService: BluePrintRuntimeService<*>, + val nodeTemplateName: String, val artifactName: String): + BlueprintTemplateService { + + 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 + return when (templateType) { + BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME -> { + BluePrintJinjaTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext) + } + BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME -> { + BluePrintVelocityTemplateService.generateContent(template, json, ignoreJsonNull, additionalContext) + } + else -> { + throw BluePrintProcessorException("Unknown Artifact type, expecting ${BluePrintConstants.ARTIFACT_JINJA_TYPE_NAME}" + + "or ${BluePrintConstants.ARTIFACT_VELOCITY_TYPE_NAME}") + } + } + } + + suspend fun generateContentFromFiles(templatePath: 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) + } +}
\ No newline at end of file |