Refactoring to enable on_failure for imperative workflow
[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  *  Copyright © 2021 Orange.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
19
20 import com.fasterxml.jackson.databind.node.ArrayNode
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
22 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
23 import org.springframework.beans.factory.config.ConfigurableBeanFactory
24 import org.springframework.context.annotation.Scope
25 import org.springframework.stereotype.Component
26
27 /**
28  * This is generic Script Component Executor function
29  * @author Brinda Santh
30  */
31 @Component("component-script-executor")
32 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
33 open class ComponentScriptExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) :
34     AbstractComponentFunction() {
35
36     companion object {
37
38         const val INPUT_SCRIPT_TYPE = "script-type"
39         const val INPUT_SCRIPT_CLASS_REFERENCE = "script-class-reference"
40         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
41         const val INPUT_INSTANCE_DEPENDENCIES = "instance-dependencies"
42
43         const val ATTRIBUTE_RESPONSE_DATA = "response-data"
44         const val ATTRIBUTE_STATUS = "status"
45
46         const val OUTPUT_RESPONSE_DATA = "response-data"
47         const val OUTPUT_STATUS = "status"
48     }
49
50     lateinit var scriptComponentFunction: AbstractScriptComponentFunction
51
52     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
53
54         val scriptType = operationInputs.getAsString(INPUT_SCRIPT_TYPE)
55         val scriptClassReference = operationInputs.getAsString(INPUT_SCRIPT_CLASS_REFERENCE)
56
57         val scriptDependencies: MutableList<String> = arrayListOf()
58         populateScriptDependencies(scriptDependencies)
59
60         scriptComponentFunction = componentFunctionScriptingService.scriptInstance(
61             this, scriptType,
62             scriptClassReference, scriptDependencies
63         )
64
65         // Handles both script processing and error handling
66         scriptComponentFunction.executeScript(executionServiceInput)
67     }
68
69     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
70         addError("Failed in ComponentScriptExecutor : ${runtimeException.message}")
71     }
72
73     open fun populateScriptDependencies(scriptDependencies: MutableList<String>) {
74         val instanceDependenciesNode = operationInputs.get(INPUT_INSTANCE_DEPENDENCIES) as? ArrayNode
75         instanceDependenciesNode?.forEach { instanceName ->
76             scriptDependencies.add(instanceName.textValue())
77         }
78     }
79 }