Revert "Renaming Files having BluePrint to have Blueprint"
[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(
32     private val applicationContext: ApplicationContext,
33     private val blueprintJythonService: BlueprintJythonService
34 ) {
35
36     private val log = LoggerFactory.getLogger(ComponentFunctionScriptingService::class.java)
37
38     suspend fun <T : AbstractScriptComponentFunction> scriptInstance(
39         componentFunction: AbstractComponentFunction,
40         scriptType: String,
41         scriptClassReference: String,
42         instanceDependencies: List<String>
43     ): T {
44
45         log.info(
46             "creating component function of script type($scriptType), reference name($scriptClassReference) and " +
47                 "instanceDependencies($instanceDependencies)"
48         )
49
50         val scriptComponent: T = scriptInstance(
51             componentFunction.bluePrintRuntimeService.bluePrintContext(),
52             scriptType, scriptClassReference
53         )
54
55         checkNotNull(scriptComponent) { "failed to initialize script component" }
56
57         scriptComponent.bluePrintRuntimeService = componentFunction.bluePrintRuntimeService
58         scriptComponent.processId = componentFunction.processId
59         scriptComponent.workflowName = componentFunction.workflowName
60         scriptComponent.stepName = componentFunction.stepName
61         scriptComponent.interfaceName = componentFunction.interfaceName
62         scriptComponent.operationName = componentFunction.operationName
63         scriptComponent.nodeTemplateName = componentFunction.nodeTemplateName
64         scriptComponent.operationInputs = componentFunction.operationInputs
65         scriptComponent.executionServiceInput = componentFunction.executionServiceInput
66         scriptComponent.scriptType = scriptType
67
68         // Populate Instance Properties
69         instanceDependencies.forEach { instanceDependency ->
70             scriptComponent.functionDependencyInstances[instanceDependency] = applicationContext
71                 .getBean(instanceDependency)
72         }
73         return scriptComponent
74     }
75
76     suspend fun <T : BlueprintFunctionNode<*, *>> scriptInstance(
77         bluePrintContext: BluePrintContext,
78         scriptType: String,
79         scriptClassReference: String
80     ): T {
81         var scriptComponent: T? = null
82
83         when (scriptType) {
84             BluePrintConstants.SCRIPT_INTERNAL -> {
85                 val bluePrintScriptsService: BluePrintScriptsService = BluePrintScriptsServiceImpl()
86                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(scriptClassReference)
87             }
88             BluePrintConstants.SCRIPT_KOTLIN -> {
89                 val bluePrintScriptsService: BluePrintScriptsService = BluePrintScriptsServiceImpl()
90                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(
91                     bluePrintContext.rootPath,
92                     bluePrintContext.name(), bluePrintContext.version(), scriptClassReference, false
93                 )
94             }
95             BluePrintConstants.SCRIPT_JYTHON -> {
96                 scriptComponent = blueprintJythonService.jythonComponentInstance(bluePrintContext, scriptClassReference) as T
97             }
98             else -> {
99                 throw BluePrintProcessorException("script type($scriptType) is not supported")
100             }
101         }
102         return scriptComponent
103     }
104 }