Blueprint Processor Python Script Components
[ccsdk/apps.git] / ms / controllerblueprints / modules / blueprint-core / src / main / kotlin / org / onap / ccsdk / apps / controllerblueprints / core / service / BluePrintTemplateService.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
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.apps.controllerblueprints.core.service
18
19 import com.fasterxml.jackson.core.io.CharTypes
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.fasterxml.jackson.databind.ObjectMapper
22 import com.fasterxml.jackson.databind.node.JsonNodeFactory
23 import com.fasterxml.jackson.databind.node.TextNode
24 import org.apache.commons.lang3.BooleanUtils
25 import org.apache.commons.lang3.StringUtils
26 import org.apache.velocity.VelocityContext
27 import org.apache.velocity.app.Velocity
28 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
29 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
30 import java.io.StringWriter
31
32 open class BluePrintTemplateService {
33
34     companion object {
35
36         /**
37          * Generate Content from Velocity Template and JSON Content.
38          */
39         fun generateContent(template: String, json: String,
40                             ignoreJsonNull: Boolean = false,
41                             additionalContext: MutableMap<String, Any> = hashMapOf()): String {
42             Velocity.init()
43             val mapper = ObjectMapper()
44             val nodeFactory = BluePrintJsonNodeFactory()
45             mapper.setNodeFactory(nodeFactory)
46
47             val jsonNode = mapper.readValue<JsonNode>(json, JsonNode::class.java)
48                     ?: throw BluePrintProcessorException("couldn't get json node from json")
49
50             if (ignoreJsonNull)
51                 JacksonUtils.removeJsonNullNode(jsonNode)
52
53             val velocityContext = VelocityContext()
54             velocityContext.put("StringUtils", StringUtils::class.java)
55             velocityContext.put("BooleanUtils", BooleanUtils::class.java)
56             /**
57              * Add the Custom Velocity Context API
58              */
59             additionalContext.forEach { name, value -> velocityContext.put(name, value) }
60             /**
61              * Add the JSON Data to the context
62              */
63             jsonNode.fields().forEach { entry ->
64                 velocityContext.put(entry.key, entry.value)
65             }
66
67             val stringWriter = StringWriter()
68             Velocity.evaluate(velocityContext, stringWriter, "TemplateData", template)
69             stringWriter.flush()
70             return stringWriter.toString()
71         }
72     }
73 }
74
75 /**
76  * Customise JsonNodeFactory adn TextNode, Since it introduces quotes for string data.
77  */
78 open class BluePrintJsonNodeFactory : JsonNodeFactory() {
79     override fun textNode(text: String): TextNode {
80         return BluePrintTextNode(text)
81     }
82 }
83
84 open class BluePrintTextNode(v: String) : TextNode(v) {
85     override fun toString(): String {
86         var len = this._value.length
87         len = len + 2 + (len shr 4)
88         val sb = StringBuilder(len)
89         CharTypes.appendQuoted(sb, this._value)
90         return sb.toString()
91     }
92
93 }
94