Fixes: manual integration test of CDS
[ccsdk/apps.git] / ms / blueprintsprocessor / functions / python-executor / src / main / kotlin / org / onap / ccsdk / apps / 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.apps.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.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
24 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
25 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyOrThrow
27 import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.annotation.Autowired
30 import org.springframework.beans.factory.config.ConfigurableBeanFactory
31 import org.springframework.context.ApplicationContext
32 import org.springframework.context.annotation.Scope
33 import org.springframework.stereotype.Component
34
35 @Component("component-jython-executor")
36 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
37 open class ComponentJythonExecutor(private var applicationContext: ApplicationContext,
38                                    private val blueprintPythonService: BlueprintPythonService) : AbstractComponentFunction() {
39
40     private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java)
41
42     private var componentFunction: AbstractComponentFunction? = null
43
44     override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
45         val request = super.prepareRequest(executionRequest)
46         // Populate Component Instance
47         populateJythonComponentInstance()
48         return request
49     }
50
51     override fun process(executionRequest: ExecutionServiceInput) {
52         log.info("Processing : $operationInputs")
53         // Invoke Jython Component Script
54         componentFunction!!.process(executionServiceInput)
55
56     }
57
58     override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
59         componentFunction!!.recover(runtimeException, executionRequest)
60     }
61
62     private fun populateJythonComponentInstance() {
63         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
64
65         val operationAssignment: OperationAssignment = bluePrintContext
66                 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
67
68         val artifactName: String = operationAssignment.implementation?.primary
69                 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
70
71         val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
72
73         val pythonFileName = artifactDefinition.file
74                 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
75
76         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
77
78         val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
79
80         checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
81
82         val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
83                 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
84
85         val jythonInstance: MutableMap<String, Any> = hashMapOf()
86         jythonInstance["log"] = LoggerFactory.getLogger(pythonClassName)
87         jythonInstance["bluePrintRuntimeService"] = bluePrintRuntimeService
88
89         instanceDependenciesNode.forEach { instanceName ->
90             jythonInstance[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
91         }
92
93         // Setup componentFunction
94         componentFunction = blueprintPythonService.jythonInstance(bluePrintContext, pythonClassName,
95                 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     }
104 }