Formatting Code base with ktlint
[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         const val INPUT_SCRIPT_TYPE = "script-type"
36         const val INPUT_SCRIPT_CLASS_REFERENCE = "script-class-reference"
37         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
38
39         const val ATTRIBUTE_RESPONSE_DATA = "response-data"
40         const val ATTRIBUTE_STATUS = "status"
41
42         const val OUTPUT_RESPONSE_DATA = "response-data"
43         const val OUTPUT_STATUS = "status"
44     }
45
46     lateinit var scriptComponentFunction: AbstractScriptComponentFunction
47
48     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
49
50         val scriptType = operationInputs.getAsString(INPUT_SCRIPT_TYPE)
51         val scriptClassReference = operationInputs.getAsString(INPUT_SCRIPT_CLASS_REFERENCE)
52
53         val scriptDependencies: MutableList<String> = arrayListOf()
54         populateScriptDependencies(scriptDependencies)
55
56         scriptComponentFunction = componentFunctionScriptingService.scriptInstance(
57             this, scriptType,
58             scriptClassReference, scriptDependencies
59         )
60
61         // Handles both script processing and error handling
62         scriptComponentFunction.executeScript(executionServiceInput)
63     }
64
65     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
66         bluePrintRuntimeService.getBluePrintError()
67             .addError("Failed in ComponentCliExecutor : ${runtimeException.message}")
68     }
69
70     open fun populateScriptDependencies(scriptDependencies: MutableList<String>) {
71         /** Place holder for Child to add extra dependencies */
72     }
73 }