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