a06fb99ffff63fa92facd53d6b32b0e6cba60851
[ccsdk/cds.git] /
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.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
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(private val bluePrintPathConfiguration: BluePrintPathConfiguration,
43                                           private val artifactName: String,
44                                           private val artifactVersion: String) : ResourceLocator {
45
46         @Throws(IOException::class)
47         override fun getString(fullName: String, encoding: Charset, interpreter: JinjavaInterpreter): String {
48             try {
49                 val deployFile =
50                     normalizedPathName(bluePrintPathConfiguration.blueprintDeployPath,
51                         artifactName,
52                         artifactVersion,
53                         fullName)
54
55                 return String(readAllBytes(Paths.get(deployFile)))
56             } catch (var5: IllegalArgumentException) {
57                 throw ResourceNotFoundException("Couldn't find resource: $fullName")
58             }
59
60         }
61     }
62
63     fun generateContent(template: String, json: String, ignoreJsonNull: Boolean,
64                         additionalContext: MutableMap<String, Any>,
65                         bluePrintPathConfiguration: BluePrintPathConfiguration, artifactName: String,
66                         artifactVersion: String): String {
67         
68         return generateContent(template,
69             json,
70             ignoreJsonNull,
71             additionalContext,
72             BlueprintRelatedTemplateLocator(bluePrintPathConfiguration, artifactName, artifactVersion))
73     }
74
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
80         }
81
82         val mapper = ObjectMapper()
83         val nodeFactory = BluePrintJsonNodeFactory()
84         mapper.nodeFactory = nodeFactory
85
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")
90             if (ignoreJsonNull) {
91                 jsonNode.removeNullNode()
92             }
93
94             val jsonMap: Map<String, Any> = mapper.readValue(json, object : TypeReference<Map<String, Any>>() {})
95             additionalContext.putAll(jsonMap)
96         }
97
98         return jinJava.render(template, additionalContext)
99     }
100 }
101