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