Merge "Add dynamic-properties as python script arg"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / ComponentScriptExecutorDSL.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.services.execution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
22 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
26 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateOperationImplBuilder
27 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
29
30 /** Component Extensions **/
31
32 fun BluePrintTypes.nodeTypeComponentScriptExecutor(): NodeType {
33     return nodeType(id = "component-script-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
34             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
35             description = "Generic Script Component Executor") {
36         attribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false)
37         attribute(ComponentScriptExecutor.ATTRIBUTE_STATUS, BluePrintConstants.DATA_TYPE_STRING, true)
38
39         operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") {
40             inputs {
41                 property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING,
42                         true, "Script Type") {
43                     defaultValue(BluePrintConstants.SCRIPT_INTERNAL)
44                     constrain {
45                         validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
46                                 BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
47                                 BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive()))
48                     }
49                 }
50                 property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING,
51                         true, "Kotlin Script class name or jython script name.")
52                 property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
53                         false, "Dynamic Json Content or DSL Json reference.")
54             }
55             outputs {
56                 property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON,
57                         false, "Output Response")
58                 property(ComponentScriptExecutor.OUTPUT_STATUS, BluePrintConstants.DATA_TYPE_STRING,
59                         true, "Status of the Component Execution ( success or failure )")
60             }
61         }
62     }
63 }
64
65 /** Component Builder */
66 fun BluePrintTypes.nodeTemplateComponentScriptExecutor(id: String,
67                                                        description: String,
68                                                        block: ComponentScriptExecutorNodeTemplateBuilder.() -> Unit)
69         : NodeTemplate {
70     return ComponentScriptExecutorNodeTemplateBuilder(id, description).apply(block).build()
71 }
72
73 class ComponentScriptExecutorNodeTemplateBuilder(id: String, description: String) :
74         AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder,
75                 ComponentScriptExecutorNodeTemplateBuilder.InputsBuilder,
76                 ComponentScriptExecutorNodeTemplateBuilder.OutputsBuilder>(id, "component-script-executor",
77                 "ComponentScriptExecutor",
78                 description) {
79
80     class InputsBuilder : PropertiesAssignmentBuilder() {
81
82         fun type(type: String) = type(type.asJsonPrimitive())
83
84         fun type(type: JsonNode) {
85             property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
86         }
87
88         fun scriptClassReference(scriptClassReference: String) = scriptClassReference(scriptClassReference.asJsonPrimitive())
89
90         fun scriptClassReference(scriptClassReference: JsonNode) {
91             property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
92         }
93
94         fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
95
96         fun dynamicProperties(dynamicProperties: JsonNode) {
97             property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
98         }
99     }
100
101     class OutputsBuilder : PropertiesAssignmentBuilder() {
102
103         fun status(status: String) = status(status.asJsonPrimitive())
104
105         fun status(status: JsonNode) {
106             property(ComponentScriptExecutor.OUTPUT_STATUS, status)
107         }
108
109         fun responseData(responseData: String) = responseData(responseData.asJsonType())
110
111         fun responseData(responseData: JsonNode) {
112             property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, responseData)
113         }
114     }
115 }