Migrate ccsdk/apps to ccsdk/cds
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / scripts / BlueprintJythonService.kt
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.cds.blueprintsprocessor.services.execution.scripts
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.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
22 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
23 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmptyOrThrow
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
25 import org.onap.ccsdk.cds.controllerblueprints.core.interfaces.BlueprintFunctionNode
26 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintContext
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.slf4j.Logger
29 import org.slf4j.LoggerFactory
30 import org.springframework.context.ApplicationContext
31 import org.springframework.stereotype.Service
32 import java.io.File
33
34 @Service
35 class BlueprintJythonService(val pythonExecutorProperty: PythonExecutorProperty,
36                              private val applicationContext: ApplicationContext) {
37
38     val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
39
40     inline fun <reified T> jythonInstance(blueprintContext: BluePrintContext, pythonClassName: String, content: String,
41                                           dependencyInstanceNames: MutableMap<String, Any>?): T {
42
43         val blueprintBasePath: String = blueprintContext.rootPath
44         val pythonPath: MutableList<String> = arrayListOf()
45         pythonPath.add(blueprintBasePath)
46         pythonPath.addAll(pythonExecutorProperty.modulePaths)
47
48         val blueprintPythonConfigurations = BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
49
50         val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
51         val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
52
53         log.info("Component Object {}", pyObject)
54
55         return pyObject.__tojava__(T::class.java) as T
56     }
57
58     fun jythonComponentInstance(bluePrintContext: BluePrintContext, scriptClassReference: String):
59             BlueprintFunctionNode<*, *> {
60         val blueprintBasePath: String = bluePrintContext.rootPath
61
62         val pythonFileName = bluePrintContext.rootPath
63                 .plus(File.separator)
64                 .plus(scriptClassReference)
65
66         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
67         log.info("Getting Jython Script Class($pythonClassName)")
68
69         val content: String = JacksonUtils.getContent(pythonFileName)
70
71         val pythonPath: MutableList<String> = arrayListOf()
72         pythonPath.add(blueprintBasePath)
73         pythonPath.addAll(pythonExecutorProperty.modulePaths)
74
75         val jythonInstances: MutableMap<String, Any> = hashMapOf()
76         jythonInstances["log"] = LoggerFactory.getLogger(pythonClassName)
77
78         return jythonInstance<BlueprintFunctionNode<*, *>>(bluePrintContext, pythonClassName,
79                 content, jythonInstances)
80     }
81
82     fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
83
84         val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
85         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
86         val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
87         val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
88
89         val operationAssignment: OperationAssignment = bluePrintContext
90                 .nodeTemplateInterfaceOperation(abstractComponentFunction.nodeTemplateName,
91                         abstractComponentFunction.interfaceName, abstractComponentFunction.operationName)
92
93         val blueprintBasePath: String = bluePrintContext.rootPath
94
95         val artifactName: String = operationAssignment.implementation?.primary
96                 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
97
98         val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
99
100         val pythonFileName = artifactDefinition.file
101                 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
102
103         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
104         log.info("Getting Jython Script Class($pythonClassName)")
105
106         val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
107
108         checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
109
110         val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
111         pythonPath.add(blueprintBasePath)
112         pythonPath.addAll(pythonExecutorProperty.modulePaths)
113
114         val jythonInstances: MutableMap<String, Any> = hashMapOf()
115         jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
116
117         val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
118                 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
119
120         instanceDependenciesNode.forEach { instanceName ->
121             jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
122         }
123
124         val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(bluePrintContext, pythonClassName,
125                 content!!, jythonInstances)
126
127         return scriptComponentFunction
128
129     }
130
131 }