ecdd454e525c3d776a9b35bc292b1d98717470d9
[ccsdk/cds.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.services.execution
18
19 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
20 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
21 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
22 import org.onap.ccsdk.apps.controllerblueprints.core.interfaces.BluePrintScriptsService
23 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
24 import org.slf4j.LoggerFactory
25 import org.springframework.context.ApplicationContext
26 import org.springframework.stereotype.Service
27
28 @Service
29 class ComponentFunctionScriptingService(private val applicationContext: ApplicationContext,
30                                         private val bluePrintScriptsService: BluePrintScriptsService,
31                                         private val blueprintJythonService: BlueprintJythonService) {
32
33     private val log = LoggerFactory.getLogger(ComponentFunctionScriptingService::class.java)
34
35     fun <T : AbstractComponentFunction> scriptInstance(componentFunction: AbstractComponentFunction, scriptType: String,
36                                                        scriptClassReference: String,
37                                                        instanceDependencies: MutableList<String>): T {
38         log.info("creating component function of script type($scriptType), reference name($scriptClassReference) and " +
39                 "instanceDependencies($instanceDependencies)")
40
41         val scriptComponent: T = scriptInstance(componentFunction.bluePrintRuntimeService.bluePrintContext(),
42                 scriptType, scriptClassReference)
43         populateScriptDependencies(scriptComponent, instanceDependencies)
44         return scriptComponent
45     }
46
47
48     fun <T : AbstractComponentFunction> scriptInstance(bluePrintContext: BluePrintContext, scriptType: String,
49                                                        scriptClassReference: String): T {
50         var scriptComponent: T? = null
51
52         when (scriptType) {
53             BluePrintConstants.SCRIPT_INTERNAL -> {
54                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(scriptClassReference)
55             }
56             BluePrintConstants.SCRIPT_KOTLIN -> {
57                 scriptComponent = bluePrintScriptsService.scriptInstance<T>(bluePrintContext, scriptClassReference, false)
58             }
59             BluePrintConstants.SCRIPT_JYTHON -> {
60                 scriptComponent = blueprintJythonService.jythonComponentInstance(bluePrintContext, scriptClassReference) as T
61             }
62             else -> {
63                 throw BluePrintProcessorException("script type($scriptType) is not supported")
64             }
65         }
66         return scriptComponent
67     }
68
69
70     private fun populateScriptDependencies(componentFunction: AbstractComponentFunction,
71                                    instanceDependencies: MutableList<String>) {
72         instanceDependencies.forEach { instanceDependency ->
73             componentFunction.functionDependencyInstances[instanceDependency] = applicationContext
74                     .getBean(instanceDependency)
75         }
76     }
77 }