2 * Copyright © 2018 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.blueprintsprocessor.services.execution
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.interfaces.BlueprintFunctionNode
24 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
25 import org.slf4j.LoggerFactory
26 import org.springframework.context.ApplicationContext
27 import org.springframework.stereotype.Service
30 class ComponentFunctionScriptingService(private val applicationContext: ApplicationContext,
31 private val bluePrintScriptsService: BluePrintScriptsService,
32 private val blueprintJythonService: BlueprintJythonService) {
34 private val log = LoggerFactory.getLogger(ComponentFunctionScriptingService::class.java)
36 fun <T : AbstractScriptComponentFunction> scriptInstance(componentFunction: AbstractComponentFunction, scriptType: String,
37 scriptClassReference: String,
38 instanceDependencies: List<String>): T {
40 log.info("creating component function of script type($scriptType), reference name($scriptClassReference) and " +
41 "instanceDependencies($instanceDependencies)")
43 val scriptComponent: T = scriptInstance(componentFunction.bluePrintRuntimeService.bluePrintContext(),
44 scriptType, scriptClassReference)
46 checkNotNull(scriptComponent) { "failed to initialize script component" }
48 scriptComponent.bluePrintRuntimeService = componentFunction.bluePrintRuntimeService
49 scriptComponent.processId = componentFunction.processId
50 scriptComponent.workflowName = componentFunction.workflowName
51 scriptComponent.stepName = componentFunction.stepName
52 scriptComponent.interfaceName = componentFunction.interfaceName
53 scriptComponent.operationName = componentFunction.operationName
54 scriptComponent.nodeTemplateName = componentFunction.nodeTemplateName
55 scriptComponent.operationInputs = componentFunction.operationInputs
57 // Populate Instance Properties
58 instanceDependencies.forEach { instanceDependency ->
59 scriptComponent.functionDependencyInstances[instanceDependency] = applicationContext
60 .getBean(instanceDependency)
62 return scriptComponent
66 fun <T : BlueprintFunctionNode<*, *>> scriptInstance(bluePrintContext: BluePrintContext, scriptType: String,
67 scriptClassReference: String): T {
68 var scriptComponent: T? = null
71 BluePrintConstants.SCRIPT_INTERNAL -> {
72 scriptComponent = bluePrintScriptsService.scriptInstance<T>(scriptClassReference)
74 BluePrintConstants.SCRIPT_KOTLIN -> {
75 scriptComponent = bluePrintScriptsService.scriptInstance<T>(bluePrintContext, scriptClassReference, false)
77 BluePrintConstants.SCRIPT_JYTHON -> {
78 scriptComponent = blueprintJythonService.jythonComponentInstance(bluePrintContext, scriptClassReference) as T
81 throw BluePrintProcessorException("script type($scriptType) is not supported")
84 return scriptComponent