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