89338b781aa5dba269411c18b44eb90e7fab65cb
[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 import kotlin.reflect.KClass
30
31 /** Component Extensions **/
32
33 fun BluePrintTypes.nodeTypeComponentScriptExecutor(): NodeType {
34     return nodeType(id = "component-script-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
35             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
36             description = "Generic Script Component Executor") {
37         attribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false)
38         attribute(ComponentScriptExecutor.ATTRIBUTE_STATUS, BluePrintConstants.DATA_TYPE_STRING, true)
39
40         operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") {
41             inputs {
42                 property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING,
43                         true, "Script Type") {
44                     defaultValue(BluePrintConstants.SCRIPT_INTERNAL)
45                     constrain {
46                         validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
47                                 BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
48                                 BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive()))
49                     }
50                 }
51                 property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING,
52                         true, "Kotlin Script class name or jython script name.")
53                 property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
54                         false, "Dynamic Json Content or DSL Json reference.")
55             }
56             outputs {
57                 property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON,
58                         false, "Output Response")
59                 property(ComponentScriptExecutor.OUTPUT_STATUS, BluePrintConstants.DATA_TYPE_STRING,
60                         true, "Status of the Component Execution ( success or failure )")
61             }
62         }
63     }
64 }
65
66 /** Component Builder */
67 fun BluePrintTypes.nodeTemplateComponentScriptExecutor(id: String,
68                                                        description: String,
69                                                        block: ComponentScriptExecutorNodeTemplateBuilder.() -> Unit)
70         : NodeTemplate {
71     return ComponentScriptExecutorNodeTemplateBuilder(id, description).apply(block).build()
72 }
73
74 class ComponentScriptExecutorNodeTemplateBuilder(id: String, description: String) :
75         AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder,
76                 ComponentScriptExecutorNodeTemplateBuilder.InputsBuilder,
77                 ComponentScriptExecutorNodeTemplateBuilder.OutputsBuilder>(id, "component-script-executor",
78                 "ComponentScriptExecutor",
79                 description) {
80
81     class InputsBuilder : PropertiesAssignmentBuilder() {
82
83         fun type(type: String) = type(type.asJsonPrimitive())
84
85         fun type(type: JsonNode) {
86             property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
87         }
88
89         fun scriptClassReference(scriptClassReference: KClass<*>) {
90             scriptClassReference(scriptClassReference.qualifiedName!!)
91         }
92
93         fun scriptClassReference(scriptClassReference: String) = scriptClassReference(scriptClassReference.asJsonPrimitive())
94
95         fun scriptClassReference(scriptClassReference: JsonNode) {
96             property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
97         }
98
99         fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
100
101         fun dynamicProperties(dynamicProperties: JsonNode) {
102             property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
103         }
104     }
105
106     class OutputsBuilder : PropertiesAssignmentBuilder() {
107
108         fun status(status: String) = status(status.asJsonPrimitive())
109
110         fun status(status: JsonNode) {
111             property(ComponentScriptExecutor.OUTPUT_STATUS, status)
112         }
113
114         fun responseData(responseData: String) = responseData(responseData.asJsonType())
115
116         fun responseData(responseData: JsonNode) {
117             property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, responseData)
118         }
119     }
120 }