Fixing Blueprint Typo's and docs
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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.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
36
37 object BlueprintJinjaTemplateService {
38
39     /**
40      * To enable inheritance within CBA, we need Jinja runtime to know where to load the templates.
41      */
42     class BlueprintRelatedTemplateLocator(
43         private val bluePrintLoadConfiguration: BlueprintLoadConfiguration,
44         private val artifactName: String,
45         private val artifactVersion: String
46     ) : ResourceLocator {
47
48         @Throws(IOException::class)
49         override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
50             try {
51                 val deployFile =
52                     normalizedPathName(
53                         bluePrintLoadConfiguration.blueprintDeployPath,
54                         artifactName,
55                         artifactVersion,
56                         fullName
57                     )
58
59                 return String(readAllBytes(Paths.get(deployFile)))
60             } catch (var5: IllegalArgumentException) {
61                 throw ResourceNotFoundException("Couldn't find resource: $fullName")
62             }
63         }
64     }
65
66     fun generateContent(
67         template: String,
68         json: String,
69         ignoreJsonNull: Boolean,
70         additionalContext: MutableMap<String, Any>,
71         bluePrintLoadConfiguration: BlueprintLoadConfiguration,
72         artifactName: String,
73         artifactVersion: String
74     ): String {
75
76         return generateContent(
77             template,
78             json,
79             ignoreJsonNull,
80             additionalContext,
81             BlueprintRelatedTemplateLocator(bluePrintLoadConfiguration, artifactName, artifactVersion)
82         )
83     }
84
85     fun generateContent(
86         template: String,
87         json: String,
88         ignoreJsonNull: Boolean,
89         additionalContext: MutableMap<String, Any>,
90         resourceLocator: ResourceLocator? = null
91     ): String {
92         val jinJava = Jinjava(JinjavaConfig())
93         if (resourceLocator != null) {
94             jinJava.resourceLocator = resourceLocator
95         }
96
97         val mapper = ObjectMapper()
98         val nodeFactory = BlueprintJsonNodeFactory()
99         mapper.nodeFactory = nodeFactory
100
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()
107             }
108
109             val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
110             additionalContext.putAll(jsonMap)
111         }
112
113         return jinJava.render(template, additionalContext)
114     }
115 }