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 / ComponentJythonExecutor.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
4  * Modifications Copyright © 2019 IBM, Bell Canada.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
20
21 import com.fasterxml.jackson.databind.node.ArrayNode
22 import org.apache.commons.io.FilenameUtils
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor.scripts.BlueprintJythonServiceImpl
25 import org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor.scripts.PythonExecutorConstants
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
27 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.BlueprintProcessorException
29 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
30 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
31 import org.slf4j.LoggerFactory
32 import org.springframework.beans.factory.config.ConfigurableBeanFactory
33 import org.springframework.context.ApplicationContext
34 import org.springframework.context.annotation.Scope
35 import org.springframework.stereotype.Component
36
37 @Component("component-jython-executor")
38 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
39 open class ComponentJythonExecutor(
40     private var applicationContext: ApplicationContext,
41     private val blueprintJythonService: BlueprintJythonServiceImpl
42 ) : AbstractComponentFunction() {
43
44     private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java)
45
46     private lateinit var componentFunction: JythonComponentFunction
47
48     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
49
50         log.info("Processing : $operationInputs")
51         // Populate Component Instance
52         populateJythonComponentInstance()
53
54         // Invoke Jython Component Script
55         componentFunction.executeScript(executionServiceInput)
56     }
57
58     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
59         bluePrintRuntimeService.getBlueprintError()
60             .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")
61     }
62
63     private suspend fun populateJythonComponentInstance() {
64         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
65
66         val operationAssignment: OperationAssignment = bluePrintContext
67             .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
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
79         val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
80
81         checkNotEmpty(content) { "artifact ($artifactName) content is empty" }
82
83         val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
84             ?: throw BlueprintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
85
86         val jythonInstance: MutableMap<String, Any> = hashMapOf()
87         jythonInstance["log"] = LoggerFactory.getLogger(pythonClassName)
88         jythonInstance["bluePrintRuntimeService"] = bluePrintRuntimeService
89
90         instanceDependenciesNode.forEach { instanceName ->
91             jythonInstance[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
92         }
93
94         // Setup componentFunction
95         componentFunction = blueprintJythonService.jythonInstance(bluePrintContext, pythonClassName, content!!, jythonInstance)
96         componentFunction.bluePrintRuntimeService = bluePrintRuntimeService
97         componentFunction.executionServiceInput = executionServiceInput
98         componentFunction.stepName = stepName
99         componentFunction.interfaceName = interfaceName
100         componentFunction.operationName = operationName
101         componentFunction.processId = processId
102         componentFunction.workflowName = workflowName
103         componentFunction.scriptType = BlueprintConstants.SCRIPT_JYTHON
104     }
105 }