2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
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
31 * This is generic Script Component Executor function
32 * @author Brinda Santh
34 @Component("component-script-executor")
35 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
36 open class ComponentScriptExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService)
37 : AbstractComponentFunction() {
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"
47 lateinit var scriptComponentFunction: AbstractScriptComponentFunction
49 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
51 val scriptType = operationInputs.getAsString(SCRIPT_TYPE)
52 val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE)
54 val scriptDependencies: MutableList<String> = arrayListOf()
55 populateScriptDependencies(scriptDependencies)
57 scriptComponentFunction = componentFunctionScriptingService.scriptInstance(this, scriptType,
58 scriptClassReference, scriptDependencies)
60 // Handles both script processing and error handling
61 scriptComponentFunction.executeScript(executionServiceInput)
64 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
65 bluePrintRuntimeService.getBluePrintError()
66 .addError("Failed in ComponentCliExecutor : ${runtimeException.message}")
70 open fun populateScriptDependencies(scriptDependencies: MutableList<String>) {
71 /** Place holder for Child to add extra dependencies */
75 /** Component Extensions **/
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)
84 operation("ComponentScriptExecutor", "ComponentScriptExecutor Operation") {
86 property(ComponentScriptExecutor.SCRIPT_TYPE, BluePrintConstants.DATA_TYPE_STRING, true,
88 defaultValue(BluePrintConstants.SCRIPT_INTERNAL)
90 validValues(listOf(BluePrintConstants.SCRIPT_INTERNAL.asJsonPrimitive(),
91 BluePrintConstants.SCRIPT_JYTHON.asJsonPrimitive(),
92 BluePrintConstants.SCRIPT_KOTLIN.asJsonPrimitive()))
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.")
101 property(ComponentScriptExecutor.RESPONSE_DATA, BluePrintConstants.DATA_TYPE_JSON, false,
103 property(ComponentScriptExecutor.STATUS, BluePrintConstants.DATA_TYPE_STRING, true,
104 "Status of the Component Execution ( success or failure )")