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.services.execution.scripts
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.services.execution.AbstractComponentFunction
22 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
24 import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment
25 import org.onap.ccsdk.apps.controllerblueprints.core.service.BluePrintContext
26 import org.onap.ccsdk.apps.controllerblueprints.core.utils.JacksonUtils
27 import org.slf4j.Logger
28 import org.slf4j.LoggerFactory
29 import org.springframework.context.ApplicationContext
30 import org.springframework.stereotype.Service
34 class BlueprintJythonService(val pythonExecutorProperty: PythonExecutorProperty,
35 private val applicationContext: ApplicationContext) {
37 val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
39 inline fun <reified T> jythonInstance(blueprintContext: BluePrintContext, pythonClassName: String, content: String,
40 dependencyInstanceNames: MutableMap<String, Any>?): T {
42 val blueprintBasePath: String = blueprintContext.rootPath
43 val pythonPath: MutableList<String> = arrayListOf()
44 pythonPath.add(blueprintBasePath)
45 pythonPath.addAll(pythonExecutorProperty.modulePaths)
47 val blueprintPythonConfigurations = BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
49 val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
50 val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
52 log.info("Component Object {}", pyObject)
54 return pyObject.__tojava__(T::class.java) as T
57 fun jythonComponentInstance(bluePrintContext: BluePrintContext, scriptClassReference: String):
58 AbstractComponentFunction {
59 val blueprintBasePath: String = bluePrintContext.rootPath
61 val pythonFileName = bluePrintContext.rootPath
63 .plus(scriptClassReference)
65 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
66 log.info("Getting Jython Script Class($pythonClassName)")
68 val content: String = JacksonUtils.getContent(pythonFileName)
70 val pythonPath: MutableList<String> = arrayListOf()
71 pythonPath.add(blueprintBasePath)
72 pythonPath.addAll(pythonExecutorProperty.modulePaths)
74 val jythonInstances: MutableMap<String, Any> = hashMapOf()
75 jythonInstances["log"] = LoggerFactory.getLogger(pythonClassName)
77 return jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName,
78 content, jythonInstances)
81 fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
83 val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
84 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
85 val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
86 val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
88 val operationAssignment: OperationAssignment = bluePrintContext
89 .nodeTemplateInterfaceOperation(abstractComponentFunction.nodeTemplateName,
90 abstractComponentFunction.interfaceName, abstractComponentFunction.operationName)
92 val blueprintBasePath: String = bluePrintContext.rootPath
94 val artifactName: String = operationAssignment.implementation?.primary
95 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
97 val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
99 val pythonFileName = artifactDefinition.file
100 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
102 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
103 log.info("Getting Jython Script Class($pythonClassName)")
105 val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
107 checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
109 val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
110 pythonPath.add(blueprintBasePath)
111 pythonPath.addAll(pythonExecutorProperty.modulePaths)
113 val jythonInstances: MutableMap<String, Any> = hashMapOf()
114 jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
116 val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
117 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
119 instanceDependenciesNode.forEach { instanceName ->
120 jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
123 val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName,
124 content!!, jythonInstances)
126 return scriptComponentFunction