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.BluePrintLoadConfiguration
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(
43 private val bluePrintLoadConfiguration: BluePrintLoadConfiguration,
44 private val artifactName: String,
45 private val artifactVersion: String
48 @Throws(IOException::class)
49 override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
53 bluePrintLoadConfiguration.blueprintDeployPath,
59 return String(readAllBytes(Paths.get(deployFile)))
60 } catch (var5: IllegalArgumentException) {
61 throw ResourceNotFoundException("Couldn't find resource: $fullName")
69 ignoreJsonNull: Boolean,
70 additionalContext: MutableMap<String, Any>,
71 bluePrintLoadConfiguration: BluePrintLoadConfiguration,
73 artifactVersion: String
76 return generateContent(
81 BlueprintRelatedTemplateLocator(bluePrintLoadConfiguration, artifactName, artifactVersion)
88 ignoreJsonNull: Boolean,
89 additionalContext: MutableMap<String, Any>,
90 resourceLocator: ResourceLocator? = null
92 val jinJava = Jinjava(JinjavaConfig())
93 if (resourceLocator != null) {
94 jinJava.resourceLocator = resourceLocator
97 val mapper = ObjectMapper()
98 val nodeFactory = BluePrintJsonNodeFactory()
99 mapper.nodeFactory = nodeFactory
101 // Add the JSON Data to the context
102 if (json.isNotEmpty()) {
103 val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
104 ?: throw BluePrintProcessorException("couldn't get json node from json")
105 if (ignoreJsonNull) {
106 jsonNode.removeNullNode()
109 val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
110 additionalContext.putAll(jsonMap)
113 return jinJava.render(template, additionalContext)