restconf resolution service
[ccsdk/apps.git] / ms / blueprintsprocessor / functions / restconf-executor / src / main / kotlin / org / onap / ccsdk / apps / 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.apps.blueprintsprocessor.functions.restconf.executor
18
19 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
20 import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.BlueprintJythonService
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.resource.resolution.ResourceResolutionService
22 import org.onap.ccsdk.apps.blueprintsprocessor.rest.service.BluePrintRestLibPropertyService
23 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.apps.controllerblueprints.core.getAsString
27 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.context.ApplicationContext
31 import org.springframework.context.annotation.Scope
32 import org.springframework.stereotype.Component
33
34 @Component("component-restconf-executor")
35 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
36 open class ComponentRestconfExecutor(private var applicationContext: ApplicationContext,
37                                      private val blueprintJythonService: BlueprintJythonService,
38                                      private val bluePrintScriptsService: BluePrintScriptsService,
39                                      private var bluePrintRestLibPropertyService: BluePrintRestLibPropertyService,
40                                      private var resourceResolutionService: ResourceResolutionService) :
41         AbstractComponentFunction() {
42
43     private val log = LoggerFactory.getLogger(ComponentRestconfExecutor::class.java)
44
45     lateinit var scriptComponent: RestconfComponentFunction
46
47     companion object {
48         const val SCRIPT_TYPE = "script-type"
49         const val SCRIPT_CLASS_REFERENCE = "script-class-reference"
50     }
51
52     override fun process(executionRequest: ExecutionServiceInput) {
53
54         val scriptType = operationInputs.getAsString(SCRIPT_TYPE)
55         val scriptClassReference = operationInputs.getAsString(SCRIPT_CLASS_REFERENCE)
56         /**
57          * Populate the Script Instance based on the Type
58          */
59         restconfComponentFunction(scriptType, scriptClassReference)
60         checkNotNull(scriptComponent) { "failed to get restconf script component" }
61
62         scriptComponent.bluePrintRuntimeService = bluePrintRuntimeService
63         scriptComponent.processId = processId
64         scriptComponent.workflowName = workflowName
65         scriptComponent.stepName = stepName
66         scriptComponent.interfaceName = interfaceName
67         scriptComponent.operationName = operationName
68         scriptComponent.nodeTemplateName = nodeTemplateName
69         scriptComponent.operationInputs = operationInputs
70
71         // Set the Rest Lib Property Service
72         scriptComponent.bluePrintRestLibPropertyService = bluePrintRestLibPropertyService
73         scriptComponent.resourceResolutionService = resourceResolutionService
74
75         scriptComponent.process(executionServiceInput)
76     }
77
78     override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
79         scriptComponent.recover(runtimeException, executionRequest)
80     }
81
82     fun restconfComponentFunction(scriptType: String, scriptClassReference: String): RestconfComponentFunction {
83         log.info("processing restconf script type($scriptType), reference name($scriptClassReference)")
84         when (scriptType) {
85             BluePrintConstants.SCRIPT_INTERNAL -> {
86                 scriptComponent = bluePrintScriptsService.scriptInstance<RestconfComponentFunction>(scriptClassReference)
87             }
88             BluePrintConstants.SCRIPT_KOTLIN -> {
89                 scriptComponent = bluePrintScriptsService.scriptInstance<RestconfComponentFunction>(bluePrintRuntimeService
90                         .bluePrintContext(), scriptClassReference, false)
91             }
92             BluePrintConstants.SCRIPT_JYTHON -> {
93                 scriptComponent = blueprintJythonService.jythonComponentInstance(this) as RestconfComponentFunction
94             }
95             else -> {
96                 throw BluePrintProcessorException("script type($scriptType) is not supported")
97             }
98         }
99         return scriptComponent
100     }
101 }