2 * Copyright © 2019 IBM, Bell Canada.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core.service
19 import com.fasterxml.jackson.core.type.TypeReference
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import com.hubspot.jinjava.Jinjava
23 import com.hubspot.jinjava.JinjavaConfig
24 import com.hubspot.jinjava.interpret.JinjavaInterpreter
25 import com.hubspot.jinjava.loader.ResourceLocator
26 import com.hubspot.jinjava.loader.ResourceNotFoundException
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
29 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
31 import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode
32 import java.io.IOException
33 import java.nio.charset.Charset
34 import java.nio.file.Files.readAllBytes
35 import java.nio.file.Paths
37 object BluePrintJinjaTemplateService {
40 * To enable inheritance within CBA, we need Jinja runtime to know where to load the templates.
42 class BlueprintRelatedTemplateLocator(private val bluePrintPathConfiguration: BluePrintPathConfiguration,
43 private val artifactName: String,
44 private val artifactVersion: String) : ResourceLocator {
46 @Throws(IOException::class)
47 override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
50 normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath,
55 return String(readAllBytes(Paths.get(deployFile)))
56 } catch (var5: IllegalArgumentException) {
57 throw ResourceNotFoundException("Couldn't find resource: $fullName")
63 fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
64 additionalContext: MutableMap<String, Any>,
65 bluePrintPathConfiguration: BluePrintPathConfiguration, artifactName: String,
66 artifactVersion: String): String {
68 return generateContent(template,
72 BlueprintRelatedTemplateLocator(bluePrintPathConfiguration, artifactName, artifactVersion))
75 fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
76 additionalContext: MutableMap<String, Any>, resourceLocator: ResourceLocator? = null): String {
77 val jinJava = Jinjava(JinjavaConfig())
78 if (resourceLocator != null) {
79 jinJava.resourceLocator = resourceLocator
82 val mapper = ObjectMapper()
83 val nodeFactory = BluePrintJsonNodeFactory()
84 mapper.nodeFactory = nodeFactory
86 // Add the JSON Data to the context
87 if (json.isNotEmpty()) {
88 val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
89 ?: throw BluePrintProcessorException("couldn't get json node from json")
91 jsonNode.removeNullNode()
94 val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
95 additionalContext.putAll(jsonMap)
98 return jinJava.render(template, additionalContext)