1b905fa600f09786b846aef14cc0657d5d26c2f5
[ccsdk/cds.git] /
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.AbstractNodeTemplateImplBuilder
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: ComponentScriptExecutorNodeTemplateImplBuilder.() -> Unit)
69         : NodeTemplate {
70     return ComponentScriptExecutorNodeTemplateImplBuilder(id, description).apply(block).build()
71 }
72
73 class ComponentScriptExecutorNodeTemplateImplBuilder(id: String, description: String) :
74         AbstractNodeTemplateImplBuilder<ComponentScriptExecutorInputAssignmentBuilder,
75                 ComponentScriptExecutorOutputAssignmentBuilder>(id, "component-script-executor",
76                 "ComponentScriptExecutor",
77                 description)
78
79 class ComponentScriptExecutorInputAssignmentBuilder : PropertiesAssignmentBuilder() {
80
81     fun type(type: String) {
82         type(type.asJsonPrimitive())
83     }
84
85     fun type(type: JsonNode) {
86         property(ComponentScriptExecutor.INPUT_SCRIPT_TYPE, type)
87     }
88
89     fun scriptClassReference(scriptClassReference: String) {
90         scriptClassReference(scriptClassReference.asJsonPrimitive())
91     }
92
93     fun scriptClassReference(scriptClassReference: JsonNode) {
94         property(ComponentScriptExecutor.INPUT_SCRIPT_CLASS_REFERENCE, scriptClassReference)
95     }
96
97     fun dynamicProperty(dynamicProperty: String) {
98         dynamicProperty(dynamicProperty.asJsonType())
99     }
100
101     fun dynamicProperty(dynamicProperty: JsonNode) {
102         property(ComponentScriptExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperty)
103     }
104 }
105
106 class ComponentScriptExecutorOutputAssignmentBuilder : PropertiesAssignmentBuilder() {
107
108     fun status(status: String) {
109         status(status.asJsonPrimitive())
110     }
111
112     fun status(status: JsonNode) {
113         property(ComponentScriptExecutor.OUTPUT_STATUS, status)
114     }
115
116     fun responseData(responseData: String) {
117         responseData(responseData.asJsonType())
118     }
119
120     fun responseData(responseData: JsonNode) {
121         property(ComponentScriptExecutor.OUTPUT_RESPONSE_DATA, responseData)
122     }
123 }