Revert "Renaming Files having BluePrint to have Blueprint"
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / python-executor / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / python / executor / scripts / BlueprintJythonServiceImpl.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
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.functions.python.executor.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.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
33 import java.io.File
34
35 @Deprecated("CDS won't support JythonService")
36 @Service("blueprintJythonService")
37 class BlueprintJythonServiceImpl(
38     val pythonExecutorProperty: PythonExecutorProperty,
39     private val applicationContext: ApplicationContext
40 ) : BlueprintJythonService {
41
42     val log: Logger = LoggerFactory.getLogger(BlueprintJythonService::class.java)
43
44     inline fun <reified T> jythonInstance(
45         blueprintContext: BluePrintContext,
46         pythonClassName: String,
47         content: String,
48         dependencyInstanceNames: MutableMap<String, Any>?
49     ): T {
50
51         val blueprintBasePath: String = blueprintContext.rootPath
52         val pythonPath: MutableList<String> = arrayListOf()
53         pythonPath.add(blueprintBasePath)
54         pythonPath.addAll(pythonExecutorProperty.modulePaths)
55
56         val blueprintPythonConfigurations =
57             BluePrintPython(pythonExecutorProperty.executionPath, pythonPath, arrayListOf())
58
59         val blueprintPythonHost = BlueprintPythonHost(blueprintPythonConfigurations)
60         val pyObject = blueprintPythonHost.getPythonComponent(content, pythonClassName, dependencyInstanceNames)
61
62         log.info("Component Object {}", pyObject)
63
64         return pyObject.__tojava__(T::class.java) as T
65     }
66
67     override fun jythonComponentInstance(bluePrintContext: BluePrintContext, scriptClassReference: String):
68         BlueprintFunctionNode<*, *> {
69
70             val pythonFileName = bluePrintContext.rootPath
71                 .plus(File.separator)
72                 .plus(scriptClassReference)
73
74             val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
75             log.info("Getting Jython Script Class($pythonClassName)")
76
77             val content: String = JacksonUtils.getContent(pythonFileName)
78
79             val jythonInstances: MutableMap<String, Any> = hashMapOf()
80             jythonInstances["log"] = LoggerFactory.getLogger(pythonClassName)
81
82             return jythonInstance<BlueprintFunctionNode<*, *>>(
83                 bluePrintContext, pythonClassName,
84                 content, jythonInstances
85             )
86         }
87
88     suspend fun jythonComponentInstance(abstractComponentFunction: AbstractComponentFunction): AbstractComponentFunction {
89
90         val bluePrintRuntimeService = abstractComponentFunction.bluePrintRuntimeService
91         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
92         val nodeTemplateName: String = abstractComponentFunction.nodeTemplateName
93         val operationInputs: MutableMap<String, JsonNode> = abstractComponentFunction.operationInputs
94
95         val operationAssignment: OperationAssignment = bluePrintContext
96             .nodeTemplateInterfaceOperation(
97                 abstractComponentFunction.nodeTemplateName,
98                 abstractComponentFunction.interfaceName, abstractComponentFunction.operationName
99             )
100
101         val blueprintBasePath: String = bluePrintContext.rootPath
102
103         val artifactName: String = operationAssignment.implementation?.primary
104             ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
105
106         val artifactDefinition =
107             bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
108
109         val pythonFileName = artifactDefinition.file
110             ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
111
112         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
113         log.info("Getting Jython Script Class($pythonClassName)")
114
115         val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
116
117         checkNotEmpty(content) { "artifact ($artifactName) content is empty" }
118
119         val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
120         pythonPath.add(blueprintBasePath)
121         pythonPath.addAll(pythonExecutorProperty.modulePaths)
122
123         val jythonInstances: MutableMap<String, Any> = hashMapOf()
124         jythonInstances["log"] = LoggerFactory.getLogger(nodeTemplateName)
125
126         val instanceDependenciesNode: ArrayNode =
127             operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
128                 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
129
130         instanceDependenciesNode.forEach { instanceName ->
131             jythonInstances[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
132         }
133
134         val scriptComponentFunction = jythonInstance<AbstractComponentFunction>(
135             bluePrintContext, pythonClassName,
136             content!!, jythonInstances
137         )
138
139         return scriptComponentFunction
140     }
141 }