Revert "Renaming Files having BluePrint to have Blueprint"
[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.AbstractNodeTemplateOperationImplBuilder
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(
37         id = "component-remote-python-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
38         derivedFrom = BluePrintConstants.MODEL_TYPE_NODE_COMPONENT,
39         description = "This is Remote Python Execution Component."
40     ) {
41
42         attribute(
43             ComponentRemotePythonExecutor.ATTRIBUTE_PREPARE_ENV_LOG, BluePrintConstants.DATA_TYPE_STRING,
44             false
45         )
46         attribute(
47             ComponentRemotePythonExecutor.ATTRIBUTE_EXEC_CMD_LOG, BluePrintConstants.DATA_TYPE_LIST,
48             false
49         ) {
50             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
51         }
52         attribute(
53             ComponentRemotePythonExecutor.ATTRIBUTE_RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON,
54             false
55         )
56
57         operation("ComponentRemotePythonExecutor", "ComponentRemotePythonExecutor Operation") {
58             inputs {
59                 property(
60                     ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, BluePrintConstants.DATA_TYPE_STRING,
61                     false, "Remote Container or Server selector name."
62                 ) {
63                     defaultValue(ComponentRemotePythonExecutor.DEFAULT_SELECTOR)
64                 }
65                 property(
66                     ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
67                     false, "Dynamic Json Content or DSL Json reference."
68                 )
69
70                 property(
71                     ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON,
72                     false, "Argument Json Content or DSL Json reference."
73                 )
74
75                 property(
76                     ComponentRemotePythonExecutor.INPUT_COMMAND, BluePrintConstants.DATA_TYPE_STRING,
77                     true, "Command to execute."
78                 )
79
80                 property(
81                     ComponentRemotePythonExecutor.INPUT_PACKAGES, BluePrintConstants.DATA_TYPE_LIST,
82                     false, "Packages to install based on type."
83                 ) {
84                     entrySchema("dt-system-packages")
85                 }
86             }
87         }
88     }
89 }
90
91 fun BluePrintTypes.dataTypeDtSystemPackages(): DataType {
92     return dataType(
93         id = "dt-system-packages", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
94         derivedFrom = BluePrintConstants.MODEL_TYPE_DATATYPES_ROOT,
95         description = "This represent System Package Data Type"
96     ) {
97         property("type", BluePrintConstants.DATA_TYPE_LIST, true, "") {
98             constrain {
99                 entrySchema(BluePrintConstants.DATA_TYPE_STRING)
100                 validValues(arrayListOf("ansible_galaxy".asJsonPrimitive(), "pip".asJsonPrimitive()))
101             }
102         }
103         property("package", BluePrintConstants.DATA_TYPE_LIST, true, "") {
104             entrySchema(BluePrintConstants.DATA_TYPE_STRING)
105         }
106     }
107 }
108
109 /** Component Builder */
110 fun BluePrintTypes.nodeTemplateComponentRemotePythonExecutor(
111     id: String,
112     description: String,
113     block: ComponentRemotePythonExecutorNodeTemplateBuilder.() -> Unit
114 ):
115     NodeTemplate {
116         return ComponentRemotePythonExecutorNodeTemplateBuilder(id, description).apply(block).build()
117     }
118
119 class DtSystemPackageDataTypeBuilder : PropertiesAssignmentBuilder() {
120
121     fun type(type: String) = type(type.asJsonPrimitive())
122
123     fun type(type: JsonNode) {
124         property("type", type)
125     }
126
127     fun packages(packages: String) = packages(packages.asJsonType())
128
129     fun packages(packages: List<String>) = packages(packages.asJsonType())
130
131     fun packages(packages: JsonNode) {
132         property("package", packages)
133     }
134 }
135
136 class ComponentRemotePythonExecutorNodeTemplateBuilder(id: String, description: String) :
137     AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder, ComponentRemotePythonExecutorNodeTemplateBuilder.InputsBuilder,
138         ComponentRemotePythonExecutorNodeTemplateBuilder.OutputsBuilder>(
139         id, "component-remote-python-executor",
140         "ComponentRemotePythonExecutor", description
141     ) {
142
143     class InputsBuilder : PropertiesAssignmentBuilder() {
144
145         private var packageList: ArrayNode? = null
146
147         fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())
148
149         fun endpointSelector(endpointSelector: JsonNode) {
150             property(ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, endpointSelector)
151         }
152
153         fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())
154
155         fun dynamicProperties(dynamicProperties: JsonNode) {
156             property(ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
157         }
158
159         fun argumentProperties(argumentProperties: String) = argumentProperties(argumentProperties.asJsonType())
160
161         fun argumentProperties(argumentProperties: JsonNode) {
162             property(ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, argumentProperties)
163         }
164
165         fun command(command: String) = command(command.asJsonPrimitive())
166
167         fun command(command: JsonNode) {
168             property(ComponentRemotePythonExecutor.INPUT_COMMAND, command)
169         }
170
171         fun packages(block: DtSystemPackageDataTypeBuilder.() -> Unit) {
172             if (packageList == null)
173                 packageList = JacksonUtils.objectMapper.createArrayNode()
174             val dtSysyemPackagePropertyAssignments = DtSystemPackageDataTypeBuilder().apply(block).build()
175             packageList!!.add(dtSysyemPackagePropertyAssignments.asJsonType())
176         }
177
178         override fun build(): MutableMap<String, JsonNode> {
179             val propertyAssignments = super.build()
180             if (packageList != null) {
181                 propertyAssignments[ComponentRemotePythonExecutor.INPUT_PACKAGES] = packageList!!
182             }
183             return propertyAssignments
184         }
185     }
186
187     class OutputsBuilder : PropertiesAssignmentBuilder()
188 }