86bf3ff563816f3e70614108645dfd30773917ea
[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 package org.onap.ccsdk.cds.controllerblueprints.core.interfaces
17
18 import com.fasterxml.jackson.core.io.CharTypes
19 import com.fasterxml.jackson.databind.node.JsonNodeFactory
20 import com.fasterxml.jackson.databind.node.TextNode
21 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
22
23 interface BlueprintTemplateService {
24
25     /**
26      * Generate dynamique content using Velocity Template or Jinja template
27      *
28      * @param bluePrintRuntimeService blueprint runtime
29      * @param nodeTemplateName node template
30      * @param artifactName Artifact Name
31      * @param jsonData json string data content to mash
32      * @param ignoreJsonNull Ignore Null value in the JSON content
33      * @param additionalContext (Key, value) mutable map for additional variables
34      * @return Content result
35      *
36      **/
37     suspend fun generateContent(bluePrintRuntimeService: BluePrintRuntimeService<*>,
38                                 nodeTemplateName: String,
39                                 artifactName: String,
40                                 jsonData: String = "",
41                                 ignoreJsonNull: Boolean = false,
42                                 additionalContext: MutableMap<String, Any> = mutableMapOf()): String
43
44
45     /**
46      * Generate dynamique content using Velocity Template or Jinja template
47      *
48      * @param template template string content
49      * @param templateType template type
50      * @param jsonData json string data content to mash
51      * @param ignoreJsonNull Ignore Null value in the JSON content
52      * @param additionalContext (Key, value) mutable map for additional variables
53      * @return Content result
54      *
55      **/
56     suspend fun generateContent(template: String, templateType: String, jsonData: String = "", ignoreJsonNull: Boolean = false,
57                                 additionalContext: MutableMap<String, Any> = mutableMapOf()): String
58 }
59
60 /**
61  * Customise JsonNodeFactory and TextNode, Since it introduces quotes for string data.
62  */
63 open class BluePrintJsonNodeFactory : JsonNodeFactory() {
64     override fun textNode(text: String): TextNode {
65         return BluePrintTextNode(text)
66     }
67 }
68
69 open class BluePrintTextNode(v: String) : TextNode(v) {
70     override fun toString(): String {
71         var len = this._value.length
72         len = len + 2 + (len shr 4)
73         val sb = StringBuilder(len)
74         CharTypes.appendQuoted(sb, this._value)
75         return sb.toString()
76     }
77
78 }