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