Refractor blueprint script dependency
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / ComponentScriptExecutor.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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
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.data.NodeType
24 import org.onap.ccsdk.cds.controllerblueprints.core.dsl.nodeType
25 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
26 import org.springframework.beans.factory.config.ConfigurableBeanFactory
27 import org.springframework.context.annotation.Scope
28 import org.springframework.stereotype.Component
29
30 /**
31  * This is generic Script Component Executor function
32  * @author Brinda Santh
33  */
34 @Component("component-script-executor")
35 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
36 open class ComponentScriptExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService)
37     : AbstractComponentFunction() {
38
39     companion object {
40         const val SCRIPT_TYPE = "script-type"
41         const val SCRIPT_CLASS_REFERENCE = "script-class-reference"
42         const val DYNAMIC_PROPERTIES = "dynamic-properties"
43         const val RESPONSE_DATA = "response-data"
44         const val STATUS = "status"
45     }
46
47     lateinit var scriptComponentFunction: AbstractScriptComponentFunction
48
49     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
50
51         val scriptType = operationInputs.getAsString(SCRIPT_TYPE)
52         val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE)
53
54         val scriptDependencies: MutableList<String> = arrayListOf()
55         populateScriptDependencies(scriptDependencies)
56
57         scriptComponentFunction = componentFunctionScriptingService.scriptInstance(this, scriptType,
58                 scriptClassReference, scriptDependencies)
59
60         // Handles both script processing and error handling
61         scriptComponentFunction.executeScript(executionServiceInput)
62     }
63
64     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
65         bluePrintRuntimeService.getBluePrintError()
66                 .addError("Failed in ComponentCliExecutor : ${runtimeException.message}")
67
68     }
69
70     open fun populateScriptDependencies(scriptDependencies: MutableList<String>) {
71         /** Place holder for Child to add extra dependencies */
72     }
73 }
74
75 /** Component Extensions **/
76
77 fun BluePrintTypes.componentScriptExecutor(): NodeType {
78     return nodeType(id = "component-script-executor", version = BluePrintConstants.DEFAULT_VERSION_NUMBER,
79             derivedFrom = BluePrintConstants.MODEL_TYPE_NODES_ROOT,
80             description = "Generic Script Component Executor") {
81         attribute(ComponentScriptExecutor.RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false)
82         attribute(ComponentScriptExecutor.STATUS, BluePrintConstants.DATA_TYPE_STRING, true)
83
84         operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") {
85             inputs {
86                 property(ComponentScriptExecutor.SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, true,
87                         "Script Type") {
88                     defaultValue(BluePrintConstants.SCRIPT_INTERNAL)
89                     constrains {
90                         validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
91                                 BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
92                                 BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive()))
93                     }
94                 }
95                 property(ComponentScriptExecutor.SCRIPT_CLASS_REFERENCE, BluePrintConstants.DATA_TYPE_STRING,
96                         true, "Kotlin Script class name or jython script name.")
97                 property(ComponentScriptExecutor.DYNAMIC_PROPERTIES, BluePrintConstants.DATA_TYPE_JSON, false,
98                         "Dynamic Json Content or DSL Json reference.")
99             }
100             outputs {
101                 property(ComponentScriptExecutor.RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false,
102                         "Output Response")
103                 property(ComponentScriptExecutor.STATUS, BluePrintConstants.DATA_TYPE_STRING, true,
104                         "Status of the Component Execution ( success or failure )")
105             }
106         }
107     }
108 }