Refactor Netconf script component parent.
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / restconf-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / restconf / executor / ComponentRestconfExecutor.kt
1 /*
2  *  Copyright © 2018 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.restconf.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.functions.resource.resolution.ResourceResolutionConstants
22 import org.onap.ccsdk.cds.blueprintsprocessor.rest.RestLibConstants
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
24 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ComponentFunctionScriptingService
25 import org.onap.ccsdk.cds.controllerblueprints.core.getAsString
26 import org.springframework.beans.factory.config.ConfigurableBeanFactory
27 import org.springframework.context.annotation.Scope
28 import org.springframework.stereotype.Component
29
30 @Component("component-restconf-executor")
31 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
32 open class ComponentRestconfExecutor(private var componentFunctionScriptingService: ComponentFunctionScriptingService) :
33         AbstractComponentFunction() {
34
35     lateinit var scriptComponent: RestconfComponentFunction
36
37     companion object {
38         const val SCRIPT_TYPE = "script-type"
39         const val SCRIPT_CLASS_REFERENCE = "script-class-reference"
40         const val INSTANCE_DEPENDENCIES = "instance-dependencies"
41     }
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.get(INSTANCE_DEPENDENCIES) as? ArrayNode
48
49         val scriptDependencies: MutableList<String> = arrayListOf()
50         scriptDependencies.add(RestLibConstants.SERVICE_BLUEPRINT_REST_LIB_PROPERTY)
51         scriptDependencies.add(ResourceResolutionConstants.SERVICE_RESOURCE_RESOLUTION)
52
53         instanceDependenciesNode?.forEach { instanceName ->
54             scriptDependencies.add(instanceName.textValue())
55         }
56         /**
57          * Populate the Script Instance based on the Type
58          */
59         scriptComponent = componentFunctionScriptingService
60                 .scriptInstance<RestconfComponentFunction>(this, scriptType,
61                         scriptClassReference, scriptDependencies)
62
63         checkNotNull(scriptComponent) { "failed to get restconf script component" }
64
65         // Handles both script processing and error handling
66         scriptComponent.executeScript(executionServiceInput)
67     }
68
69     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
70         bluePrintRuntimeService.getBluePrintError()
71                 .addError("Failed in ComponentRestconfExecutor : ${runtimeException.message}")
72     }
73 }