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.google.common.io.Resources
23 import com.hubspot.jinjava.Jinjava
24 import com.hubspot.jinjava.interpret.Context
25 import com.hubspot.jinjava.interpret.JinjavaInterpreter
26 import com.hubspot.jinjava.loader.ClasspathResourceLocator
27 import com.hubspot.jinjava.loader.ResourceLocator
28 import com.hubspot.jinjava.loader.ResourceNotFoundException
29 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
30 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintLoadConfiguration
31 import org.onap.ccsdk.cds.controllerblueprints.core.config.BluePrintPathConfiguration
32 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintJsonNodeFactory
33 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
34 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedPathName
35 import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode
36 import org.springframework.context.annotation.Scope
37 import org.springframework.stereotype.Service
38 import java.io.IOException
39 import java.nio.charset.Charset
40 import java.nio.file.Files.readAllBytes
41 import java.nio.file.Paths
43 object BluePrintJinjaTemplateService {
46 * To enable inheritance within CBA, we need Jinja runtime to know where to load the templates.
48 class BlueprintRelatedTemplateLocator(private val bluePrintPathConfiguration: BluePrintPathConfiguration,
49 private val artifactName: String,
50 private val artifactVersion: String) : ResourceLocator {
52 @Throws(IOException::class)
53 override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
56 normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath,
61 return String(readAllBytes(Paths.get(deployFile)))
62 } catch (var5: IllegalArgumentException) {
63 throw ResourceNotFoundException("Couldn't find resource: $fullName")
69 fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
70 additionalContext: MutableMap<String, Any>,
71 bluePrintPathConfiguration: BluePrintPathConfiguration, artifactName: String,
72 artifactVersion: String): String {
75 return generateContent(template,
79 BlueprintRelatedTemplateLocator(bluePrintPathConfiguration, artifactName, artifactVersion))
82 fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
83 additionalContext: MutableMap<String, Any>, resourceLocator: ResourceLocator? = null): String {
84 val jinJava = Jinjava()
85 if (resourceLocator != null) {
86 jinJava.resourceLocator = resourceLocator
89 val mapper = ObjectMapper()
90 val nodeFactory = BluePrintJsonNodeFactory()
91 mapper.nodeFactory = nodeFactory
93 // Add the JSON Data to the context
94 if (json.isNotEmpty()) {
95 val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
96 ?: throw BluePrintProcessorException("couldn't get json node from json")
98 jsonNode.removeNullNode()
101 val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
102 additionalContext.putAll(jsonMap)
105 return jinJava.render(template, additionalContext)