e1643b576e21961acd8d48815a9cfe86fcacea69
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restful-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restful / executor / ComponentRestfulExecutor.kt
1 /*
2  * Copyright © 2020 Huawei Intellectual Property.
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 package org.onap.ccsdk.cds.blueprintsprocessor.functions.restful.executor
17
18 import com.fasterxml.jackson.databind.node.ArrayNode
19 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
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.controllerblueprints.core.getAsString
24 import org.springframework.beans.factory.config.ConfigurableBeanFactory
25 import org.springframework.context.annotation.Scope
26 import org.springframework.stereotype.Component
27
28 @Component("component-restful-executor")
29 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
30 open class ComponentRestfulExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) : AbstractComponentFunction() {
31
32     lateinit var scriptComponent: RestfulCMComponentFunction
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     }
39
40     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
41
42         val scriptType = operationInputs.getAsString(SCRIPT_TYPE)
43         val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE)
44         val instanceDependenciesNode = operationInputs.get(INSTANCE_DEPENDENCIES) as? ArrayNode
45
46         val scriptDependencies: MutableList<String> = arrayListOf()
47         scriptDependencies.add(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
48
49         instanceDependenciesNode?.forEach { instanceName ->
50             scriptDependencies.add(instanceName.textValue())
51         }
52         /**
53          * Populate the Script Instance based on the Type
54          */
55         scriptComponent = componentFunctionScriptingService
56                 .scriptInstance<RestfulCMComponentFunction>(this, scriptType,
57                         scriptClassReference, scriptDependencies)
58
59         checkNotNull(scriptComponent) { "failed to get restfulCM script component" }
60
61         // Handles both script processing and error handling
62         scriptComponent.executeScript(executionServiceInput)
63     }
64
65     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
66         bluePrintRuntimeService.getBluePrintError()
67                 .addError("Failed in ComponentRestfulExecutor : ${runtimeException.message}")
68     }
69 }