2965cb5d1c993de5e59d6481b555143e2ffb404d
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2017-2018 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
17 package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor
18
19 import org.apache.commons.io.FilenameUtils
20 import org.onap.ccsdk.apps.blueprintsprocessor.core.api.data.ExecutionServiceInput
21 import org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor.utils.PythonExecutorUtils
22 import org.onap.ccsdk.apps.blueprintsprocessor.services.execution.AbstractComponentFunction
23 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintConstants
24 import org.onap.ccsdk.apps.controllerblueprints.core.BluePrintProcessorException
25 import org.onap.ccsdk.apps.controllerblueprints.core.checkNotEmptyNThrow
26 import org.onap.ccsdk.apps.controllerblueprints.core.data.OperationAssignment
27 import org.slf4j.LoggerFactory
28 import org.springframework.stereotype.Component
29
30 @Component("component-jython-executor")
31 class ComponentJythonExecutor(private val pythonExecutorProperty: PythonExecutorProperty) : AbstractComponentFunction() {
32
33     private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java)
34
35     private var componentFunction: AbstractComponentFunction? = null
36
37
38     override fun process(executionServiceInput: ExecutionServiceInput) {
39
40         log.info("Processing : ${executionServiceInput.metadata}")
41         checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" }
42
43         val bluePrintContext = bluePrintRuntimeService!!.bluePrintContext()
44
45         val operationAssignment: OperationAssignment = bluePrintContext
46                 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
47
48         val blueprintBasePath: String = bluePrintRuntimeService!!.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)?.asText()
49                 ?: throw BluePrintProcessorException("python execute path is missing for node template ($nodeTemplateName)")
50
51         val artifactName: String = operationAssignment.implementation?.primary
52                 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
53
54         val artifactDefinition = bluePrintRuntimeService!!.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
55
56         val pythonFileName = artifactDefinition.file
57                 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
58
59         val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
60
61         val content: String? = bluePrintRuntimeService!!.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
62
63         checkNotEmptyNThrow(content, "artifact ($artifactName) content is empty")
64
65         val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
66         pythonPath.add(blueprintBasePath)
67         pythonPath.addAll(pythonExecutorProperty.modulePaths)
68
69         val properties: MutableMap<String, Any> = hashMapOf()
70         properties["log"] = log
71
72         componentFunction = PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath,
73                 pythonPath, content, pythonClassName, properties)
74
75         componentFunction!!.process(executionServiceInput)
76
77     }
78
79     override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
80         componentFunction!!.recover(runtimeException, executionRequest)
81     }
82
83 }