Component Script Executor Fix
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / ComponentFunctionScriptingService.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.services.execution
18
19 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
20 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
22 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BluePrintScriptsService
23 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
24 import org.onap.ccsdk.cds.controllerblueprints.core.scripts.BluePrintScriptsServiceImpl
25 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
26 import org.slf4j.LoggerFactory
27 import org.springframework.context.ApplicationContext
28 import org.springframework.stereotype.Service
29
30 @Service
31 class ComponentFunctionScriptingService(private val applicationContext: ApplicationContext,
32                                         private val blueprintJythonService: BlueprintJythonService) {
33
34     private val log = LoggerFactory.getLogger(ComponentFunctionScriptingService::class.java)
35
36     suspend fun <T : AbstractScriptComponentFunction> scriptInstance(componentFunction: AbstractComponentFunction,
37                                                                      scriptType: String,
38                                                                      scriptClassReference: String,
39                                                                      instanceDependencies: List<String>): T {
40
41         log.info("creating component function of script type($scriptType), reference name($scriptClassReference) and " +
42                 "instanceDependencies($instanceDependencies)")
43
44         val scriptComponent: T = scriptInstance(componentFunction.bluePrintRuntimeService.bluePrintContext(),
45                 scriptType, scriptClassReference)
46
47         checkNotNull(scriptComponent) { "failed to initialize script component" }
48
49         scriptComponent.bluePrintRuntimeService = componentFunction.bluePrintRuntimeService
50         scriptComponent.processId = componentFunction.processId
51         scriptComponent.workflowName = componentFunction.workflowName
52         scriptComponent.stepName = componentFunction.stepName
53         scriptComponent.interfaceName = componentFunction.interfaceName
54         scriptComponent.operationName = componentFunction.operationName
55         scriptComponent.nodeTemplateName = componentFunction.nodeTemplateName
56         scriptComponent.operationInputs = componentFunction.operationInputs
57         scriptComponent.executionServiceInput = componentFunction.executionServiceInput
58         scriptComponent.scriptType = scriptType
59
60         // Populate Instance Properties
61         instanceDependencies.forEach { instanceDependency ->
62             scriptComponent.functionDependencyInstances[instanceDependency] = applicationContext
63                     .getBean(instanceDependency)
64         }
65         return scriptComponent
66     }
67
68
69     suspend fun <T : BlueprintFunctionNode<*, *>> scriptInstance(bluePrintContext: BluePrintContext, scriptType: String,
70                                                                  scriptClassReference: String): T {
71         var scriptComponent: T? = null
72
73         when (scriptType) {
74             BluePrintConstants.SCRIPT_INTERNAL -> {
75                 val bluePrintScriptsService: BluePrintScriptsService = BluePrintScriptsServiceImpl()
76                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(scriptClassReference)
77             }
78             BluePrintConstants.SCRIPT_KOTLIN -> {
79                 val bluePrintScriptsService: BluePrintScriptsService = BluePrintScriptsServiceImpl()
80                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(bluePrintContext.rootPath,
81                         bluePrintContext.name(), bluePrintContext.version(), scriptClassReference, false)
82             }
83             BluePrintConstants.SCRIPT_JYTHON -> {
84                 scriptComponent = blueprintJythonService.jythonComponentInstance(bluePrintContext, scriptClassReference) as T
85             }
86             else -> {
87                 throw BluePrintProcessorException("script type($scriptType) is not supported")
88             }
89         }
90         return scriptComponent
91     }
92
93 }