Add remote python executor DSL properties
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / python-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / python / executor / ComponentRemotePythonExecutorDSL.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.functions.python.executor
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.node.ArrayNode
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintTypes
23 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
24 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
27 import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
28 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateImplBuilder
29 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
30 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.dataType
31 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33
34 /** Component Extensions **/
35 fun BluePrintTypes.nodeTypeComponentRemotePythonExecutor(): NodeType {
36     return nodeType(id = "component-remote-python-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
37             derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
38             description = "This is Remote Python Execution Component.") {
39
40         attribute(ComponentRemotePythonExecutor.ATTRIBUTE_PREPARE_ENV_LOG, BluePrintConstants.DATA_TYPE_STRING,
41                 false)
42         attribute(ComponentRemotePythonExecutor.ATTRIBUTE_EXEC_CMD_LOG, BluePrintConstants.DATA_TYPE_LIST,
43                 false) {
44             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
45         }
46         attribute(ComponentRemotePythonExecutor.ATTRIBUTE_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON,
47                 false)
48
49         operation("ComponentRemotePythonExecutor", "ComponentRemotePythonExecutor Operation") {
50             inputs {
51                 property(ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, BluePrintConstants.DATA_TYPE_STRING,
52                         false, "Remote Container or Server selector name.") {
53                     defaultValue(ComponentRemotePythonExecutor.DEFAULT_SELECTOR)
54                 }
55                 property(ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
56                         false, "Dynamic Json Content or DSL Json reference.")
57
58                 property(ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
59                         false, "Argument Json Content or DSL Json reference.")
60
61                 property(ComponentRemotePythonExecutor.INPUT_COMMAND, BluePrintConstants.DATA_TYPE_STRING,
62                         true, "Command to execute.")
63
64                 property(ComponentRemotePythonExecutor.INPUT_PACKAGES, BluePrintConstants.DATA_TYPE_LIST,
65                         false, "Packages to install based on type.") {
66                     entrySchema("dt-system-packages")
67                 }
68             }
69         }
70     }
71 }
72
73 fun BluePrintTypes.dataTypeDtSystemPackages(): DataType {
74     return dataType(id = "dt-system-packages", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
75             derivedFrom = BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT,
76             description = "This represent System Package Data Type") {
77         property("type", BluePrintConstants.DATA_TYPE_LIST, true, "") {
78             constrain {
79                 entrySchema(BluePrintConstants.DATA_TYPE_STRING)
80                 validValues(arrayListOf("ansible_galaxy".asJsonPrimitive(), "pip".asJsonPrimitive()))
81             }
82         }
83         property("package", BluePrintConstants.DATA_TYPE_LIST, true, "") {
84             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
85         }
86     }
87 }
88
89 /** Component Builder */
90 fun BluePrintTypes.nodeTemplateComponentRemotePythonExecutor(id: String,
91                                                              description: String,
92                                                              block: ComponentRemotePythonExecutorNodeTemplateImplBuilder.() -> Unit)
93         : NodeTemplate {
94     return ComponentRemotePythonExecutorNodeTemplateImplBuilder(id, description).apply(block).build()
95 }
96
97 class ComponentRemotePythonExecutorNodeTemplateImplBuilder(id: String, description: String) :
98         AbstractNodeTemplateImplBuilder<ComponentRemotePythonExecutorInputAssignmentBuilder,
99                 ComponentRemotePythonExecutorOutputAssignmentBuilder>(id, "component-remote-python-executor",
100                 "ComponentRemotePythonExecutor", description)
101
102 class DtSystemPackageDataTypeBuilder : PropertiesAssignmentBuilder() {
103
104     fun type(type: String) = type(type.asJsonPrimitive())
105
106     fun type(type: JsonNode) {
107         property("type", type)
108     }
109
110     fun packages(packages: String) = packages(packages.asJsonType())
111
112     fun packages(packages: List<String>) = packages(packages.asJsonType())
113
114     fun packages(packages: JsonNode) {
115         property("package", packages)
116     }
117 }
118
119 class ComponentRemotePythonExecutorInputAssignmentBuilder : PropertiesAssignmentBuilder() {
120
121     private var packageList: ArrayNode? = null
122
123     fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
124
125     fun endpointSelector(endpointSelector: JsonNode) {
126         property(ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, endpointSelector)
127     }
128
129     fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
130
131     fun dynamicProperties(dynamicProperties: JsonNode) {
132         property(ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
133     }
134
135     fun argumentProperties(argumentProperties: String) = argumentProperties(argumentProperties.asJsonType())
136
137     fun argumentProperties(argumentProperties: JsonNode) {
138         property(ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, argumentProperties)
139     }
140
141     fun command(command: String) = command(command.asJsonPrimitive())
142
143     fun command(command: JsonNode) {
144         property(ComponentRemotePythonExecutor.INPUT_COMMAND, command)
145     }
146
147     fun packages(block: DtSystemPackageDataTypeBuilder.() -> Unit) {
148         if (packageList == null)
149             packageList = JacksonUtils.objectMapper.createArrayNode()
150         val dtSysyemPackagePropertyAssignments = DtSystemPackageDataTypeBuilder().apply(block).build()
151         packageList!!.add(dtSysyemPackagePropertyAssignments.asJsonType())
152     }
153
154     override fun build(): MutableMap<String, JsonNode> {
155         val propertyAssignments = super.build()
156         if (packageList != null) {
157             propertyAssignments[ComponentRemotePythonExecutor.INPUT_PACKAGES] = packageList!!
158         }
159         return propertyAssignments
160     }
161 }
162
163 class ComponentRemotePythonExecutorOutputAssignmentBuilder : PropertiesAssignmentBuilder()