Merge "Resource resolution should return a string"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / resource-resolution / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / resource / resolution / ResourceSourceDSL.kt
1 /*
2  *  Copyright © 2019 IBM.
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.blueprintsprocessor.functions.resource.resolution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentScriptExecutor
21 import org.onap.ccsdk.cds.controllerblueprints.core.*
22 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
23 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
24 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplatePropertyImplBuilder
25 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
26 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
27 import kotlin.reflect.KClass
28
29 fun BluePrintTypes.nodeTypeSourceInput(): NodeType {
30     return nodeType(id = "source-input", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
31             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
32             description = "This is Input Resource Source Node Type") {}
33 }
34
35 fun BluePrintTypes.nodeTypeSourceDefault(): NodeType {
36     return nodeType(id = "source-default", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
37             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
38             description = "This is Default Resource Source Node Type") {}
39 }
40
41 fun BluePrintTypes.nodeTypeSourceDb(): NodeType {
42     return nodeType(id = "source-db", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
43             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
44             description = "This is Database Resource Source Node Type") {
45         property("type", BluePrintConstants.DATA_TYPE_STRING,
46                 true, "") {
47             defaultValue("SQL".asJsonPrimitive())
48             constrain {
49                 validValues(arrayListOf("SQL".asJsonPrimitive(), "PLSQL".asJsonPrimitive()))
50             }
51         }
52         property("endpoint-selector", BluePrintConstants.DATA_TYPE_STRING,
53                 false, "")
54         property("query", BluePrintConstants.DATA_TYPE_STRING,
55                 true, "")
56         property("input-key-mapping", BluePrintConstants.DATA_TYPE_MAP,
57                 true, "") {
58             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
59         }
60         property("output-key-mapping", BluePrintConstants.DATA_TYPE_MAP,
61                 false, "") {
62             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
63         }
64         property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST,
65                 true, "Resource Resolution dependency dictionary names.") {
66             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
67         }
68     }
69 }
70
71 fun BluePrintTypes.nodeTypeSourceRest(): NodeType {
72     return nodeType(id = "source-rest", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
73             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
74             description = "This is Rest Resource Source Node Type") {
75         property("type", BluePrintConstants.DATA_TYPE_STRING,
76                 true, "") {
77             defaultValue("JSON".asJsonPrimitive())
78             constrain {
79                 validValues(arrayListOf("JSON".asJsonPrimitive(), "XML".asJsonPrimitive()))
80             }
81         }
82         property("verb", BluePrintConstants.DATA_TYPE_STRING,
83                 true, "") {
84             defaultValue("GET".asJsonPrimitive())
85             constrain {
86                 validValues(arrayListOf("GET".asJsonPrimitive(), "POST".asJsonPrimitive(),
87                         "DELETE".asJsonPrimitive(), "PUT".asJsonPrimitive()))
88             }
89         }
90         property("payload", BluePrintConstants.DATA_TYPE_STRING,
91                 false, "") {
92             defaultValue("".asJsonPrimitive())
93         }
94         property("endpoint-selector", BluePrintConstants.DATA_TYPE_STRING,
95                 false, "")
96         property("url-path", BluePrintConstants.DATA_TYPE_STRING,
97                 true, "")
98         property("path", BluePrintConstants.DATA_TYPE_STRING,
99                 true, "")
100         property("expression-type", BluePrintConstants.DATA_TYPE_STRING,
101                 false, "") {
102             defaultValue("JSON_PATH".asJsonPrimitive())
103             constrain {
104                 validValues(arrayListOf("JSON_PATH".asJsonPrimitive(), "JSON_POINTER".asJsonPrimitive()))
105             }
106         }
107         property("input-key-mapping", BluePrintConstants.DATA_TYPE_MAP,
108                 true, "") {
109             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
110         }
111         property("output-key-mapping", BluePrintConstants.DATA_TYPE_MAP,
112                 false, "") {
113             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
114         }
115         property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST,
116                 true, "Resource Resolution dependency dictionary names.") {
117             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
118         }
119     }
120 }
121
122 fun BluePrintTypes.nodeTypeSourceCapability(): NodeType {
123     return nodeType(id = "source-capability", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
124             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_RESOURCE_SOURCE,
125             description = "This is Component Resource Source Node Type") {
126         property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING,
127                 true, "Request Id, Unique Id for the request.") {
128             defaultValue(BluePrintConstants.SCRIPT_KOTLIN)
129             constrain {
130                 validValues(arrayListOf(BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive(),
131                         BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
132                         BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive()))
133             }
134         }
135         property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING,
136                 true, "Kotlin Script class name or jython script name.")
137         property("key-dependencies", BluePrintConstants.DATA_TYPE_LIST,
138                 true, "Resource Resolution dependency dictionary names.") {
139             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
140         }
141     }
142 }
143
144 /** Node Template Source Input **/
145 fun BluePrintTypes.nodeTemplateSourceInput(id: String, description: String,
146                                            block: SourceInputNodeTemplateBuilder.() -> Unit): NodeTemplate {
147     return SourceInputNodeTemplateBuilder(id, description).apply(block).build()
148 }
149
150 class SourceInputNodeTemplateBuilder(id: String, description: String) :
151         AbstractNodeTemplatePropertyImplBuilder<PropertiesAssignmentBuilder>(id,
152                 "source-input", description)
153
154 /** Node Template Source Default **/
155 fun BluePrintTypes.nodeTemplateSourceDefault(id: String, description: String,
156                                              block: SourceDefaultNodeTemplateBuilder.() -> Unit): NodeTemplate {
157     return SourceDefaultNodeTemplateBuilder(id, description).apply(block).build()
158 }
159
160 class SourceDefaultNodeTemplateBuilder(id: String, description: String) :
161         AbstractNodeTemplatePropertyImplBuilder<PropertiesAssignmentBuilder>(id,
162                 "source-default", description)
163
164 /** Node Template Source DB **/
165 fun BluePrintTypes.nodeTemplateSourceDb(id: String, description: String,
166                                         block: SourceDbNodeTemplateBuilder.() -> Unit): NodeTemplate {
167     return SourceDbNodeTemplateBuilder(id, description).apply(block).build()
168 }
169
170 class SourceDbNodeTemplateBuilder(id: String, description: String) :
171         AbstractNodeTemplatePropertyImplBuilder<SourceDbNodeTemplateBuilder.PropertiesBuilder>(id,
172                 "source-db", description) {
173
174     class PropertiesBuilder : PropertiesAssignmentBuilder() {
175         fun type(type: String) = type(type.asJsonPrimitive())
176
177         fun type(type: JsonNode) {
178             property("type", type)
179         }
180
181         fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
182
183         fun endpointSelector(endpointSelector: JsonNode) {
184             property("endpoint-selector", endpointSelector)
185         }
186
187         fun query(query: String) = query(query.asJsonPrimitive())
188
189         fun query(query: JsonNode) {
190             property("query", query)
191         }
192
193         fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
194             val map = KeyMappingBuilder().apply(block).build()
195             property("input-key-mapping", map.asJsonType())
196         }
197
198         fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
199             val map = KeyMappingBuilder().apply(block).build()
200             property("output-key-mapping", map.asJsonType())
201         }
202
203         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
204
205         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
206
207         fun keyDependencies(keyDependencies: JsonNode) {
208             property("key-dependencies", keyDependencies)
209         }
210     }
211 }
212
213 class KeyMappingBuilder() {
214     val map: MutableMap<String, String> = hashMapOf()
215     fun map(key: String, value: String) {
216         map[key] = value
217     }
218
219     fun build(): MutableMap<String, String> {
220         return map
221     }
222 }
223
224
225 /** Node Template Source Rest **/
226 fun BluePrintTypes.nodeTemplateSourceRest(id: String, description: String,
227                                           block: SourceRestNodeTemplateBuilder.() -> Unit): NodeTemplate {
228     return SourceRestNodeTemplateBuilder(id, description).apply(block).build()
229 }
230
231 class SourceRestNodeTemplateBuilder(id: String, description: String) :
232         AbstractNodeTemplatePropertyImplBuilder<SourceRestNodeTemplateBuilder.PropertiesBuilder>(id,
233                 "source-rest", description) {
234
235     class PropertiesBuilder : PropertiesAssignmentBuilder() {
236         fun type(type: String) = type(type.asJsonPrimitive())
237
238         fun type(type: JsonNode) {
239             property("type", type)
240         }
241
242         fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
243
244         fun endpointSelector(endpointSelector: JsonNode) {
245             property("endpoint-selector", endpointSelector)
246         }
247
248         fun verb(verb: String) = verb(verb.asJsonPrimitive())
249
250         fun verb(verb: JsonNode) {
251             property("verb", verb)
252         }
253
254         fun payload(payload: String) = payload(payload.asJsonPrimitive())
255
256         fun payload(payload: JsonNode) {
257             property("payload", payload)
258         }
259
260         fun urlPath(urlPath: String) = urlPath(urlPath.asJsonPrimitive())
261
262         fun urlPath(urlPath: JsonNode) {
263             property("url-path", urlPath)
264         }
265
266         fun path(path: String) = path(path.asJsonPrimitive())
267
268         fun path(path: JsonNode) {
269             property("path", path)
270         }
271
272         fun expressionType(expressionType: String) = expressionType(expressionType.asJsonPrimitive())
273
274         fun expressionType(expressionType: JsonNode) {
275             property("expression-type", expressionType)
276         }
277
278         fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
279             val map = KeyMappingBuilder().apply(block).build()
280             property("input-key-mapping", map.asJsonType())
281         }
282
283         fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
284             val map = KeyMappingBuilder().apply(block).build()
285             property("output-key-mapping", map.asJsonType())
286         }
287
288         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
289
290         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
291
292         fun keyDependencies(keyDependencies: JsonNode) {
293             property("key-dependencies", keyDependencies)
294         }
295     }
296 }
297
298 /** Node Template Source Rest **/
299 fun BluePrintTypes.nodeTemplateSourceCapability(id: String, description: String,
300                                                 block: SourceCapabilityNodeTemplateBuilder.() -> Unit): NodeTemplate {
301     return SourceCapabilityNodeTemplateBuilder(id, description).apply(block).build()
302 }
303
304 class SourceCapabilityNodeTemplateBuilder(id: String, description: String) :
305         AbstractNodeTemplatePropertyImplBuilder<SourceCapabilityNodeTemplateBuilder.PropertiesBuilder>(id,
306                 "source-capability", description) {
307
308     class PropertiesBuilder : PropertiesAssignmentBuilder() {
309         fun type(type: String) = type(type.asJsonPrimitive())
310
311         fun type(type: JsonNode) {
312             property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
313         }
314
315         fun scriptClassReference(scriptClassReference: KClass<*>) {
316             scriptClassReference(scriptClassReference.qualifiedName!!)
317         }
318
319         fun scriptClassReference(scriptClassReference: String) = scriptClassReference(scriptClassReference.asJsonPrimitive())
320
321         fun scriptClassReference(scriptClassReference: JsonNode) {
322             property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
323         }
324
325         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
326
327         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
328
329         fun keyDependencies(keyDependencies: JsonNode) {
330             property("key-dependencies", keyDependencies)
331         }
332     }
333 }