Renaming Files having BluePrint to have Blueprint
[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.ServiceTemplateBuilder
29 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.TopologyTemplateBuilder
30 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
31 import kotlin.reflect.KClass
32
33 /** Component Extensions **/
34 fun ServiceTemplateBuilder.nodeTypeComponentScriptExecutor() {
35     val nodeType = BlueprintTypes.nodeTypeComponentScriptExecutor()
36     if (this.nodeTypes == null) this.nodeTypes = hashMapOf()
37     this.nodeTypes!![nodeType.id!!] = nodeType
38 }
39
40 fun BlueprintTypes.nodeTypeComponentScriptExecutor(): NodeType {
41     return nodeType(
42         id = "component-script-executor", version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
43         derivedFrom = BlueprintConstants.MODEL_TYPE_NODE_COMPONENT,
44         description = "Generic Script Component Executor"
45     ) {
46         attribute(ComponentScriptExecutor.ATTRIBUTE_RESPONSE_DATA, BlueprintConstants.DATA_TYPE_JSON, false)
47         attribute(ComponentScriptExecutor.ATTRIBUTE_STATUS, BlueprintConstants.DATA_TYPE_STRING, true)
48
49         operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") {
50             inputs {
51                 property(
52                     ComponentScriptExecutor.INPUT_SCRIPT_TYPE, BlueprintConstants.DATA_TYPE_STRING,
53                     true, "Script Type"
54                 ) {
55                     defaultValue(BlueprintConstants.SCRIPT_INTERNAL)
56                     constrain {
57                         validValues(
58                             listOf(
59                                 BlueprintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
60                                 BlueprintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
61                                 BlueprintConstants.SCRIPT_KOTLIN.asJsonPrimitive()
62                             )
63                         )
64                     }
65                 }
66                 property(
67                     ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, BlueprintConstants.DATA_TYPE_STRING,
68                     true, "Kotlin Script class name or jython script name."
69                 )
70                 property(
71                     ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, BlueprintConstants.DATA_TYPE_JSON,
72                     false, "Dynamic Json Content or DSL Json reference."
73                 )
74             }
75             outputs {
76                 property(
77                     ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, BlueprintConstants.DATA_TYPE_JSON,
78                     false, "Output Response"
79                 )
80                 property(
81                     ComponentScriptExecutor.OUTPUT_STATUS, BlueprintConstants.DATA_TYPE_STRING,
82                     true, "Status of the Component Execution ( success or failure )"
83                 )
84             }
85         }
86     }
87 }
88
89 /** Component Builder */
90 fun TopologyTemplateBuilder.nodeTemplateComponentScriptExecutor(
91     id: String,
92     description: String,
93     block: ComponentScriptExecutorNodeTemplateBuilder.() -> Unit
94 ) {
95     val nodeTemplate = BlueprintTypes.nodeTemplateComponentScriptExecutor(
96         id, description,
97         block
98     )
99     if (nodeTemplates == null) nodeTemplates = hashMapOf()
100     nodeTemplates!![nodeTemplate.id!!] = nodeTemplate
101 }
102
103 fun BlueprintTypes.nodeTemplateComponentScriptExecutor(
104     id: String,
105     description: String,
106     block: ComponentScriptExecutorNodeTemplateBuilder.() -> Unit
107 ): NodeTemplate {
108     return ComponentScriptExecutorNodeTemplateBuilder(id, description).apply(block).build()
109 }
110
111 class ComponentScriptExecutorNodeTemplateBuilder(id: String, description: String) :
112     AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder,
113         ComponentScriptExecutorNodeTemplateBuilder.InputsBuilder,
114         ComponentScriptExecutorNodeTemplateBuilder.OutputsBuilder>(
115         id, "component-script-executor",
116         "ComponentScriptExecutor",
117         description
118     ) {
119
120     class InputsBuilder : PropertiesAssignmentBuilder() {
121
122         fun type(type: String) = type(type.asJsonPrimitive())
123
124         fun type(type: JsonNode) {
125             property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
126         }
127
128         fun scriptClassReference(scriptClassReference: KClass<*>) {
129             scriptClassReference(scriptClassReference.qualifiedName!!)
130         }
131
132         fun scriptClassReference(scriptClassReference: String) =
133             scriptClassReference(scriptClassReference.asJsonPrimitive())
134
135         fun scriptClassReference(scriptClassReference: JsonNode) {
136             property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
137         }
138
139         fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
140
141         fun dynamicProperties(dynamicProperties: JsonNode) {
142             property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
143         }
144     }
145
146     class OutputsBuilder : PropertiesAssignmentBuilder() {
147
148         fun status(status: String) = status(status.asJsonPrimitive())
149
150         fun status(status: JsonNode) {
151             property(ComponentScriptExecutor.OUTPUT_STATUS, status)
152         }
153
154         fun responseData(responseData: String) = responseData(responseData.asJsonType())
155
156         fun responseData(responseData: JsonNode) {
157             property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, responseData)
158         }
159     }
160 }