2d3c35de78bea19adfda181c21bb302f63449731
[ccsdk/cds.git] /
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(template: String, json: String, ignoreJsonNull: Boolean = false,
38                         additionalContext: MutableMap<String, Any> = mutableMapOf()): String {
39
40         // Customized Object Mapper to remove String double quotes
41         val mapper = ObjectMapper()
42         val nodeFactory = BluePrintJsonNodeFactory()
43         mapper.nodeFactory = nodeFactory
44
45         val jsonNode: JsonNode? = if (json.isNotEmpty()) {
46             mapper.readValue(json, JsonNode::class.java)
47                     ?: throw BluePrintProcessorException("couldn't get json node from json")
48         } else {
49             null
50         }
51         return generateContent(template, jsonNode, ignoreJsonNull, additionalContext)
52     }
53
54     /**
55      * Generate Content from Velocity Template and JSON Node with injected API
56      */
57     fun generateContent(template: String, jsonNode: JsonNode?, ignoreJsonNull: Boolean = false,
58                         additionalContext: MutableMap<String, Any> = mutableMapOf()): String {
59
60         /*
61          *  create a new instance of the velocity engine
62          */
63         val velocity = VelocityEngine()
64
65         /*
66          *  initialize the engine
67          */
68         velocity.init()
69
70         val velocityContext = VelocityContext()
71         velocityContext.put("StringUtils", StringUtils::class.java)
72         velocityContext.put("BooleanUtils", BooleanUtils::class.java)
73
74         // Add the Custom Velocity Context API
75         additionalContext.forEach { (name, value) -> velocityContext.put(name, value) }
76
77         // Add the JSON Data to the context
78         if (jsonNode != null) {
79             if (ignoreJsonNull)
80                 jsonNode.removeNullNode()
81             jsonNode.fields().forEach { entry ->
82                 velocityContext.put(entry.key, entry.value)
83             }
84         }
85
86         val stringWriter = StringWriter()
87         velocity.evaluate(velocityContext, stringWriter, "TemplateData", template)
88         stringWriter.flush()
89         return stringWriter.toString()
90     }
91 }