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