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