2 * Copyright © 2018-2019 AT&T Intellectual Property.
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.cds.blueprintsprocessor.functions.python.executor.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.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
22 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
23 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
25 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
26 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
27 import org.onap.ccsdk.cds.controllerblueprints.core.service.BlueprintContext
28 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
29 import org.slf4j.Logger
30 import org.slf4j.LoggerFactory
31 import org.springframework.context.ApplicationContext
32 import org.springframework.stereotype.Service
35 @Deprecated("CDS won't support JythonService")
36 @Service("blueprintJythonService")
37 class BlueprintJythonServiceImpl(
38 val pythonExecutorProperty: PythonExecutorProperty,
39 private val applicationContext: ApplicationContext
40 ) : BlueprintJythonService {
42 val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
44 inline fun <reified T> jythonInstance(
45 blueprintContext: BlueprintContext,
46 pythonClassName: String,
48 dependencyInstanceNames: MutableMap<String, Any>?
51 val blueprintBasePath: String = blueprintContext.rootPath
52 val pythonPath: MutableList<String> = arrayListOf()
53 pythonPath.add(blueprintBasePath)
54 pythonPath.addAll(pythonExecutorProperty.modulePaths)
56 val blueprintPythonConfigurations =
57 BlueprintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
59 val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
60 val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
62 log.info("Component Object {}", pyObject)
64 return pyObject.__tojava__(T::class.java) as T
67 override fun jythonComponentInstance(bluePrintContext: BlueprintContext, scriptClassReference: String):
68 BlueprintFunctionNode<*, *> {
70 val pythonFileName = bluePrintContext.rootPath
72 .plus(scriptClassReference)
74 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
75 log.info("Getting Jython Script Class($pythonClassName)")
77 val content: String = JacksonUtils.getContent(pythonFileName)
79 val jythonInstances: MutableMap<String, Any> = hashMapOf()
80 jythonInstances["log"] = LoggerFactory.getLogger(pythonClassName)
82 return jythonInstance<BlueprintFunctionNode<*, *>>(
83 bluePrintContext, pythonClassName,
84 content, jythonInstances
88 suspend fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
90 val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
91 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
92 val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
93 val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
95 val operationAssignment: OperationAssignment = bluePrintContext
96 .nodeTemplateInterfaceOperation(
97 abstractComponentFunction.nodeTemplateName,
98 abstractComponentFunction.interfaceName, abstractComponentFunction.operationName
101 val blueprintBasePath: String = bluePrintContext.rootPath
103 val artifactName: String = operationAssignment.implementation?.primary
104 ?: throw BlueprintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
106 val artifactDefinition =
107 bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
109 val pythonFileName = artifactDefinition.file
110 ?: throw BlueprintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
112 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
113 log.info("Getting Jython Script Class($pythonClassName)")
115 val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
117 checkNotEmpty(content) { "artifact ($artifactName) content is empty" }
119 val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
120 pythonPath.add(blueprintBasePath)
121 pythonPath.addAll(pythonExecutorProperty.modulePaths)
123 val jythonInstances: MutableMap<String, Any> = hashMapOf()
124 jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
126 val instanceDependenciesNode: ArrayNode =
127 operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
128 ?: throw BlueprintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
130 instanceDependenciesNode.forEach { instanceName ->
131 jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
134 val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(
135 bluePrintContext, pythonClassName,
136 content!!, jythonInstances
139 return scriptComponentFunction