fcaa57b30df370a462180bd668c0b58b87ba6f72
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 IBM, Bell Canada.
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 package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor
17
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
31
32 @Service
33 class BlueprintJythonService(val pythonExecutorProperty: PythonExecutorProperty,
34                              private val applicationContext: ApplicationContext) {
35
36     val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
37
38     inline fun <reified T> jythonInstance(blueprintContext: BluePrintContext, pythonClassName: String, content: String,
39                                           dependencyInstanceNames: MutableMap<String, Any>?): T {
40
41         val blueprintBasePath: String = blueprintContext.rootPath
42         val pythonPath: MutableList<String> = arrayListOf()
43         pythonPath.add(blueprintBasePath)
44         pythonPath.addAll(pythonExecutorProperty.modulePaths)
45
46         val blueprintPythonConfigurations = BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
47
48         val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
49         val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
50
51         log.info("Component Object {}", pyObject)
52
53         return pyObject.__tojava__(T::class.java) as T
54     }
55
56     fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
57
58         val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
59         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
60         val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
61         val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
62
63         val operationAssignment: OperationAssignment = bluePrintContext
64                 .nodeTemplateInterfaceOperation(abstractComponentFunction.nodeTemplateName,
65                         abstractComponentFunction.interfaceName, abstractComponentFunction.operationName)
66
67         val blueprintBasePath: String = bluePrintContext.rootPath
68
69         val artifactName: String = operationAssignment.implementation?.primary
70                 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
71
72         val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
73
74         val pythonFileName = artifactDefinition.file
75                 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
76
77         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
78         log.info("Getting Jython Script Class($pythonClassName)")
79
80         val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
81
82         checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
83
84         val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
85         pythonPath.add(blueprintBasePath)
86         pythonPath.addAll(pythonExecutorProperty.modulePaths)
87
88         val jythonInstances: MutableMap<String, Any> = hashMapOf()
89         jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
90
91         val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
92                 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
93
94         instanceDependenciesNode.forEach { instanceName ->
95             jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
96         }
97
98         val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName,
99                 content!!, jythonInstances)
100
101         return scriptComponentFunction
102
103     }
104
105 }