0c1c5232080cfba0641516d32ce77b569a7317e8
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / cli-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / cli / executor / ComponentCliExecutor.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.functions.cli.executor
18
19 import com.fasterxml.jackson.databind.node.ArrayNode
20 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
22 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
23 import org.onap.ccsdk.cds.blueprintsprocessor.ssh.SshLibConstants
24 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
25 import org.springframework.beans.factory.config.ConfigurableBeanFactory
26 import org.springframework.context.annotation.Scope
27 import org.springframework.stereotype.Component
28
29 @Component("component-cli-executor")
30 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
31 open class ComponentCliExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService)
32     : AbstractComponentFunction() {
33
34     companion object {
35         const val SCRIPT_TYPE = "script-type"
36         const val SCRIPT_CLASS_REFERENCE = "script-class-reference"
37         const val INSTANCE_DEPENDENCIES = "instance-dependencies"
38         const val RESPONSE_DATA = "response-data"
39     }
40
41     private lateinit var scriptComponent: CliComponentFunction
42
43     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
44
45         val scriptType = operationInputs.getAsString(SCRIPT_TYPE)
46         val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE)
47         val instanceDependenciesNode = operationInputs[INSTANCE_DEPENDENCIES] as? ArrayNode
48
49         val scriptDependencies: MutableList<String> = arrayListOf()
50         scriptDependencies.add(SshLibConstants.SERVICE_BLUEPRINT_SSH_LIB_PROPERTY)
51         // May be injected from model, not by default
52         //scriptDependencies.add(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
53
54         instanceDependenciesNode?.forEach { instanceName ->
55             scriptDependencies.add(instanceName.textValue())
56         }
57
58         scriptComponent = componentFunctionScriptingService.scriptInstance(this, scriptType,
59                 scriptClassReference, scriptDependencies)
60
61
62         // Handles both script processing and error handling
63         scriptComponent.executeScript(executionServiceInput)
64     }
65
66     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
67         bluePrintRuntimeService.getBluePrintError()
68                 .addError("Failed in ComponentCliExecutor : ${runtimeException.message}")
69
70     }
71 }