aboutsummaryrefslogtreecommitdiffstats
path: root/ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/python/executor/ComponentRemotePythonExecutorDSL.kt
blob: 6788310aa2b396155a26c12031fbe94e9ed1be2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
 *  Copyright © 2019 IBM.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.node.ArrayNode
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintTypes
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
import org.onap.ccsdk.cds.controllerblueprints.core.asJsonType
import org.onap.ccsdk.cds.controllerblueprints.core.data.DataType
import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeTemplate
import org.onap.ccsdk.cds.controllerblueprints.core.data.NodeType
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.AbstractNodeTemplateOperationImplBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.PropertiesAssignmentBuilder
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.dataType
import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils

/** Component Extensions **/
fun BlueprintTypes.nodeTypeComponentRemotePythonExecutor(): NodeType {
    return nodeType(
        id = "component-remote-python-executor", version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
        derivedFrom = BlueprintConstants.MODEL_TYPE_NODE_COMPONENT,
        description = "This is Remote Python Execution Component."
    ) {

        attribute(
            ComponentRemotePythonExecutor.ATTRIBUTE_PREPARE_ENV_LOG, BlueprintConstants.DATA_TYPE_STRING,
            false
        )
        attribute(
            ComponentRemotePythonExecutor.ATTRIBUTE_EXEC_CMD_LOG, BlueprintConstants.DATA_TYPE_LIST,
            false
        ) {
            entrySchema(BlueprintConstants.DATA_TYPE_STRING)
        }
        attribute(
            ComponentRemotePythonExecutor.ATTRIBUTE_RESPONSE_DATA, BlueprintConstants.DATA_TYPE_JSON,
            false
        )

        operation("ComponentRemotePythonExecutor", "ComponentRemotePythonExecutor Operation") {
            inputs {
                property(
                    ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, BlueprintConstants.DATA_TYPE_STRING,
                    false, "Remote Container or Server selector name."
                ) {
                    defaultValue(ComponentRemotePythonExecutor.DEFAULT_SELECTOR)
                }
                property(
                    ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, BlueprintConstants.DATA_TYPE_JSON,
                    false, "Dynamic Json Content or DSL Json reference."
                )

                property(
                    ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, BlueprintConstants.DATA_TYPE_JSON,
                    false, "Argument Json Content or DSL Json reference."
                )

                property(
                    ComponentRemotePythonExecutor.INPUT_COMMAND, BlueprintConstants.DATA_TYPE_STRING,
                    true, "Command to execute."
                )

                property(
                    ComponentRemotePythonExecutor.INPUT_PACKAGES, BlueprintConstants.DATA_TYPE_LIST,
                    false, "Packages to install based on type."
                ) {
                    entrySchema("dt-system-packages")
                }
            }
        }
    }
}

fun BlueprintTypes.dataTypeDtSystemPackages(): DataType {
    return dataType(
        id = "dt-system-packages", version = BlueprintConstants.DEFAULT_VERSION_NUMBER,
        derivedFrom = BlueprintConstants.MODEL_TYPE_DATATYPES_ROOT,
        description = "This represent System Package Data Type"
    ) {
        property("type", BlueprintConstants.DATA_TYPE_LIST, true, "") {
            constrain {
                entrySchema(BlueprintConstants.DATA_TYPE_STRING)
                validValues(arrayListOf("ansible_galaxy".asJsonPrimitive(), "pip".asJsonPrimitive()))
            }
        }
        property("package", BlueprintConstants.DATA_TYPE_LIST, true, "") {
            entrySchema(BlueprintConstants.DATA_TYPE_STRING)
        }
    }
}

/** Component Builder */
fun BlueprintTypes.nodeTemplateComponentRemotePythonExecutor(
    id: String,
    description: String,
    block: ComponentRemotePythonExecutorNodeTemplateBuilder.() -> Unit
):
    NodeTemplate {
        return ComponentRemotePythonExecutorNodeTemplateBuilder(id, description).apply(block).build()
    }

class DtSystemPackageDataTypeBuilder : PropertiesAssignmentBuilder() {

    fun type(type: String) = type(type.asJsonPrimitive())

    fun type(type: JsonNode) {
        property("type", type)
    }

    fun packages(packages: String) = packages(packages.asJsonType())

    fun packages(packages: List<String>) = packages(packages.asJsonType())

    fun packages(packages: JsonNode) {
        property("package", packages)
    }
}

class ComponentRemotePythonExecutorNodeTemplateBuilder(id: String, description: String) :
    AbstractNodeTemplateOperationImplBuilder<PropertiesAssignmentBuilder, ComponentRemotePythonExecutorNodeTemplateBuilder.InputsBuilder,
        ComponentRemotePythonExecutorNodeTemplateBuilder.OutputsBuilder>(
        id, "component-remote-python-executor",
        "ComponentRemotePythonExecutor", description
    ) {

    class InputsBuilder : PropertiesAssignmentBuilder() {

        private var packageList: ArrayNode? = null

        fun endpointSelector(endpointSelector: String) = endpointSelector(endpointSelector.asJsonPrimitive())

        fun endpointSelector(endpointSelector: JsonNode) {
            property(ComponentRemotePythonExecutor.INPUT_ENDPOINT_SELECTOR, endpointSelector)
        }

        fun dynamicProperties(dynamicProperties: String) = dynamicProperties(dynamicProperties.asJsonType())

        fun dynamicProperties(dynamicProperties: JsonNode) {
            property(ComponentRemotePythonExecutor.INPUT_DYNAMIC_PROPERTIES, dynamicProperties)
        }

        fun argumentProperties(argumentProperties: String) = argumentProperties(argumentProperties.asJsonType())

        fun argumentProperties(argumentProperties: JsonNode) {
            property(ComponentRemotePythonExecutor.INPUT_ARGUMENT_PROPERTIES, argumentProperties)
        }

        fun command(command: String) = command(command.asJsonPrimitive())

        fun command(command: JsonNode) {
            property(ComponentRemotePythonExecutor.INPUT_COMMAND, command)
        }

        fun packages(block: DtSystemPackageDataTypeBuilder.() -> Unit) {
            if (packageList == null)
                packageList = JacksonUtils.objectMapper.createArrayNode()
            val dtSysyemPackagePropertyAssignments = DtSystemPackageDataTypeBuilder().apply(block).build()
            packageList!!.add(dtSysyemPackagePropertyAssignments.asJsonType())
        }

        override fun build(): MutableMap<String, JsonNode> {
            val propertyAssignments = super.build()
            if (packageList != null) {
                propertyAssignments[ComponentRemotePythonExecutor.INPUT_PACKAGES] = packageList!!
            }
            return propertyAssignments
        }
    }

    class OutputsBuilder : PropertiesAssignmentBuilder()
}