Renaming Files having BluePrint to have Blueprint
[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
251         fun type(type: String) = type(type.asJsonPrimitive())
252
253         fun type(type: JsonNode) {
254             property("type", type)
255         }
256
257         fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
258
259         fun endpointSelector(endpointSelector: JsonNode) {
260             property("endpoint-selector", endpointSelector)
261         }
262
263         fun query(query: String) = query(query.asJsonPrimitive())
264
265         fun query(query: JsonNode) {
266             property("query", query)
267         }
268
269         fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
270             val map = KeyMappingBuilder().apply(block).build()
271             property("input-key-mapping", map.asJsonType())
272         }
273
274         fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
275             val map = KeyMappingBuilder().apply(block).build()
276             property("output-key-mapping", map.asJsonType())
277         }
278
279         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
280
281         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
282
283         fun keyDependencies(keyDependencies: JsonNode) {
284             property("key-dependencies", keyDependencies)
285         }
286     }
287 }
288
289 class KeyMappingBuilder() {
290
291     val map: MutableMap<String, String> = hashMapOf()
292     fun map(key: String, value: String) {
293         map[key] = value
294     }
295
296     fun build(): MutableMap<String, String> {
297         return map
298     }
299 }
300
301 /** Node Template Source Rest **/
302 fun BlueprintTypes.nodeTemplateSourceRest(
303     id: String,
304     description: String,
305     block: SourceRestNodeTemplateBuilder.() -> Unit
306 ): NodeTemplate {
307     return SourceRestNodeTemplateBuilder(id, description).apply(block).build()
308 }
309
310 class SourceRestNodeTemplateBuilder(id: String, description: String) :
311     AbstractNodeTemplatePropertyImplBuilder<SourceRestNodeTemplateBuilder.PropertiesBuilder>(
312         id,
313         "source-rest", description
314     ) {
315
316     class PropertiesBuilder : PropertiesAssignmentBuilder() {
317
318         fun type(type: String) = type(type.asJsonPrimitive())
319
320         fun type(type: JsonNode) {
321             property("type", type)
322         }
323
324         fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
325
326         fun endpointSelector(endpointSelector: JsonNode) {
327             property("endpoint-selector", endpointSelector)
328         }
329
330         fun verb(verb: String) = verb(verb.asJsonPrimitive())
331
332         fun verb(verb: JsonNode) {
333             property("verb", verb)
334         }
335
336         fun payload(payload: String) = payload(payload.asJsonPrimitive())
337
338         fun payload(payload: JsonNode) {
339             property("payload", payload)
340         }
341
342         fun urlPath(urlPath: String) = urlPath(urlPath.asJsonPrimitive())
343
344         fun urlPath(urlPath: JsonNode) {
345             property("url-path", urlPath)
346         }
347
348         fun path(path: String) = path(path.asJsonPrimitive())
349
350         fun path(path: JsonNode) {
351             property("path", path)
352         }
353
354         fun expressionType(expressionType: String) = expressionType(expressionType.asJsonPrimitive())
355
356         fun expressionType(expressionType: JsonNode) {
357             property("expression-type", expressionType)
358         }
359
360         fun inputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
361             val map = KeyMappingBuilder().apply(block).build()
362             property("input-key-mapping", map.asJsonType())
363         }
364
365         fun outputKeyMapping(block: KeyMappingBuilder.() -> Unit) {
366             val map = KeyMappingBuilder().apply(block).build()
367             property("output-key-mapping", map.asJsonType())
368         }
369
370         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
371
372         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
373
374         fun keyDependencies(keyDependencies: JsonNode) {
375             property("key-dependencies", keyDependencies)
376         }
377     }
378 }
379
380 /** Node Template Source Rest **/
381 fun BlueprintTypes.nodeTemplateSourceCapability(
382     id: String,
383     description: String,
384     block: SourceCapabilityNodeTemplateBuilder.() -> Unit
385 ): NodeTemplate {
386     return SourceCapabilityNodeTemplateBuilder(id, description).apply(block).build()
387 }
388
389 class SourceCapabilityNodeTemplateBuilder(id: String, description: String) :
390     AbstractNodeTemplatePropertyImplBuilder<SourceCapabilityNodeTemplateBuilder.PropertiesBuilder>(
391         id,
392         "source-capability", description
393     ) {
394
395     class PropertiesBuilder : PropertiesAssignmentBuilder() {
396
397         fun type(type: String) = type(type.asJsonPrimitive())
398
399         fun type(type: JsonNode) {
400             property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
401         }
402
403         fun scriptClassReference(scriptClassReference: KClass<*>) {
404             scriptClassReference(scriptClassReference.qualifiedName!!)
405         }
406
407         fun scriptClassReference(scriptClassReference: String) = scriptClassReference(scriptClassReference.asJsonPrimitive())
408
409         fun scriptClassReference(scriptClassReference: JsonNode) {
410             property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
411         }
412
413         fun keyDependencies(keyDependencies: String) = keyDependencies(keyDependencies.asJsonType())
414
415         fun keyDependencies(keyDependencies: List<String>) = keyDependencies(keyDependencies.asJsonString())
416
417         fun keyDependencies(keyDependencies: JsonNode) {
418             property("key-dependencies", keyDependencies)
419         }
420     }
421 }