Renaming Files having BluePrint to have Blueprint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / blueprint-core / src / main / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / service / BlueprintVelocityTemplateService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Modifications Copyright © 2019 IBM, Bell Canada.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.controllerblueprints.core.service
20
21 import com.fasterxml.jackson.databind.JsonNode
22 import com.fasterxml.jackson.databind.ObjectMapper
23 import org.apache.commons.lang3.BooleanUtils
24 import org.apache.commons.lang3.StringUtils
25 import org.apache.velocity.VelocityContext
26 import org.apache.velocity.app.VelocityEngine
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintJsonNodeFactory
29 import org.onap.ccsdk.cds.controllerblueprints.core.removeNullNode
30 import java.io.StringWriter
31
32 object BlueprintVelocityTemplateService {
33
34     /**
35      * Generate Content from Velocity Template and JSON Content with injected API
36      */
37     fun generateContent(
38         template: String,
39         json: String,
40         ignoreJsonNull: Boolean = false,
41         additionalContext: MutableMap<String, Any> = mutableMapOf()
42     ): String {
43
44         // Customized Object Mapper to remove String double quotes
45         val mapper = ObjectMapper()
46         val nodeFactory = BlueprintJsonNodeFactory()
47         mapper.nodeFactory = nodeFactory
48
49         val jsonNode: JsonNode? = if (json.isNotEmpty()) {
50             mapper.readValue(json, JsonNode::class.java)
51                 ?: throw BlueprintProcessorException("couldn't get json node from json")
52         } else {
53             null
54         }
55         return generateContent(template, jsonNode, ignoreJsonNull, additionalContext)
56     }
57
58     /**
59      * Generate Content from Velocity Template and JSON Node with injected API
60      */
61     fun generateContent(
62         template: String,
63         jsonNode: JsonNode?,
64         ignoreJsonNull: Boolean = false,
65         additionalContext: MutableMap<String, Any> = mutableMapOf()
66     ): String {
67
68         /*
69          *  create a new instance of the velocity engine
70          */
71         val velocity = VelocityEngine()
72
73         /*
74          *  initialize the engine
75          */
76         velocity.init()
77
78         val velocityContext = VelocityContext()
79         velocityContext.put("StringUtils", StringUtils::class.java)
80         velocityContext.put("BooleanUtils", BooleanUtils::class.java)
81
82         // Add the Custom Velocity Context API
83         additionalContext.forEach { (name, value) -> velocityContext.put(name, value) }
84
85         // Add the JSON Data to the context
86         if (jsonNode != null) {
87             if (ignoreJsonNull)
88                 jsonNode.removeNullNode()
89             jsonNode.fields().forEach { entry ->
90                 velocityContext.put(entry.key, entry.value)
91             }
92         }
93
94         val stringWriter = StringWriter()
95         velocity.evaluate(velocityContext, stringWriter, "TemplateData", template)
96         stringWriter.flush()
97         return stringWriter.toString()
98     }
99 }