Renaming Files having BluePrint to have Blueprint
[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.getAsString
21 import org.springframework.beans.factory.config.ConfigurableBeanFactory
22 import org.springframework.context.annotation.Scope
23 import org.springframework.stereotype.Component
24
25 /**
26  * This is generic Script Component Executor function
27  * @author Brinda Santh
28  */
29 @Component("component-script-executor")
30 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
31 open class ComponentScriptExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) :
32     AbstractComponentFunction() {
33
34     companion object {
35
36         const val INPUT_SCRIPT_TYPE = "script-type"
37         const val INPUT_SCRIPT_CLASS_REFERENCE = "script-class-reference"
38         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
39
40         const val ATTRIBUTE_RESPONSE_DATA = "response-data"
41         const val ATTRIBUTE_STATUS = "status"
42
43         const val OUTPUT_RESPONSE_DATA = "response-data"
44         const val OUTPUT_STATUS = "status"
45     }
46
47     lateinit var scriptComponentFunction: AbstractScriptComponentFunction
48
49     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
50
51         val scriptType = operationInputs.getAsString(INPUT_SCRIPT_TYPE)
52         val scriptClassReference = operationInputs.getAsString(INPUT_SCRIPT_CLASS_REFERENCE)
53
54         val scriptDependencies: MutableList<String> = arrayListOf()
55         populateScriptDependencies(scriptDependencies)
56
57         scriptComponentFunction = componentFunctionScriptingService.scriptInstance(
58             this, scriptType,
59             scriptClassReference, scriptDependencies
60         )
61
62         // Handles both script processing and error handling
63         scriptComponentFunction.executeScript(executionServiceInput)
64     }
65
66     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
67         bluePrintRuntimeService.getBlueprintError()
68             .addError("Failed in ComponentScriptExecutor : ${runtimeException.message}")
69     }
70
71     open fun populateScriptDependencies(scriptDependencies: MutableList<String>) {
72         /** Place holder for Child to add extra dependencies */
73     }
74 }