Merge "Remote Python executor unescapes script parameter values"
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BluePrintJinjaTemplateService.kt
1 /*
2  * Copyright © 2019 IBM, Bell Canada.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core.service
18
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
42
43 object BluePrintJinjaTemplateService {
44
45     /**
46      * To enable inheritance within CBA, we need Jinja runtime to know where to load the templates.
47      */
48     class BlueprintRelatedTemplateLocator(private val bluePrintPathConfiguration: BluePrintPathConfiguration,
49                                           private val artifactName: String,
50                                           private val artifactVersion: String) : ResourceLocator {
51
52         @Throws(IOException::class)
53         override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
54             try {
55                 val deployFile =
56                     normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath,
57                         artifactName,
58                         artifactVersion,
59                         fullName)
60
61                 return String(readAllBytes(Paths.get(deployFile)))
62             } catch (var5: IllegalArgumentException) {
63                 throw ResourceNotFoundException("Couldn't find resource: $fullName")
64             }
65
66         }
67     }
68
69     fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
70                         additionalContext: MutableMap<String, Any>,
71                         bluePrintPathConfiguration: BluePrintPathConfiguration, artifactName: String,
72                         artifactVersion: String): String {
73
74
75         return generateContent(template,
76             json,
77             ignoreJsonNull,
78             additionalContext,
79             BlueprintRelatedTemplateLocator(bluePrintPathConfiguration, artifactName, artifactVersion))
80     }
81
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
87         }
88
89         val mapper = ObjectMapper()
90         val nodeFactory = BluePrintJsonNodeFactory()
91         mapper.nodeFactory = nodeFactory
92
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")
97             if (ignoreJsonNull) {
98                 jsonNode.removeNullNode()
99             }
100
101             val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
102             additionalContext.putAll(jsonMap)
103         }
104
105         return jinJava.render(template, additionalContext)
106     }
107 }
108