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