2 * Copyright © 2019 IBM, Bell Canada.
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.
16 package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor
18 import com.fasterxml.jackson.databind.JsonNode
19 import com.fasterxml.jackson.databind.node.ArrayNode
20 import org.apache.commons.io.FilenameUtils
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.plugin.BlueprintPythonHost
22 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
24 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
25 import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment
26 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
27 import org.slf4j.Logger
28 import org.slf4j.LoggerFactory
29 import org.springframework.context.ApplicationContext
30 import org.springframework.stereotype.Service
33 class BlueprintJythonService(val pythonExecutorProperty: PythonExecutorProperty,
34 private val applicationContext: ApplicationContext) {
36 val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
38 inline fun <reified T> jythonInstance(blueprintContext: BluePrintContext, pythonClassName: String, content: String,
39 dependencyInstanceNames: MutableMap<String, Any>?): T {
41 val blueprintBasePath: String = blueprintContext.rootPath
42 val pythonPath: MutableList<String> = arrayListOf()
43 pythonPath.add(blueprintBasePath)
44 pythonPath.addAll(pythonExecutorProperty.modulePaths)
46 val blueprintPythonConfigurations = BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
48 val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
49 val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
51 log.info("Component Object {}", pyObject)
53 return pyObject.__tojava__(T::class.java) as T
56 fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
58 val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
59 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
60 val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
61 val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
63 val operationAssignment: OperationAssignment = bluePrintContext
64 .nodeTemplateInterfaceOperation(abstractComponentFunction.nodeTemplateName,
65 abstractComponentFunction.interfaceName, abstractComponentFunction.operationName)
67 val blueprintBasePath: String = bluePrintContext.rootPath
69 val artifactName: String = operationAssignment.implementation?.primary
70 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
72 val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
74 val pythonFileName = artifactDefinition.file
75 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
77 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
78 log.info("Getting Jython Script Class($pythonClassName)")
80 val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
82 checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
84 val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
85 pythonPath.add(blueprintBasePath)
86 pythonPath.addAll(pythonExecutorProperty.modulePaths)
88 val jythonInstances: MutableMap<String, Any> = hashMapOf()
89 jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
91 val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
92 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
94 instanceDependenciesNode.forEach { instanceName ->
95 jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
98 val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName,
99 content!!, jythonInstances)
101 return scriptComponentFunction