2 * Copyright © 2017-2018 AT&T Intellectual Property.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.apps.blueprintsprocessor.functions.python.executor
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
30 @Component("component-python-executor")
31 class ComponentPythonExecutor(private val pythonExecutorProperty: PythonExecutorProperty) : AbstractComponentFunction() {
33 private val log = LoggerFactory.getLogger(ComponentPythonExecutor::class.java)
35 private var componentFunction: AbstractComponentFunction? = null
38 override fun process(executionServiceInput: ExecutionServiceInput) {
40 log.info("Processing : ${executionServiceInput.metadata}")
41 checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" }
43 val bluePrintContext = bluePrintRuntimeService!!.bluePrintContext()
45 val operationAssignment: OperationAssignment = bluePrintContext
46 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
48 val blueprintBasePath: String = bluePrintRuntimeService!!.get(BluePrintConstants.PROPERTY_BLUEPRINT_BASE_PATH)?.asText()
49 ?: throw BluePrintProcessorException("python execute path is missing for node template ($nodeTemplateName)")
51 val artifactName: String = operationAssignment.implementation?.primary
52 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
54 val artifactDefinition = bluePrintRuntimeService!!.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
56 val pythonFileName = artifactDefinition.file
57 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
59 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
61 val content: String? = bluePrintRuntimeService!!.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
63 checkNotEmptyNThrow(content, "artifact ($artifactName) content is empty")
65 val pythonPath: MutableList<String> = operationAssignment.implementation?.dependencies ?: arrayListOf()
66 pythonPath.add(blueprintBasePath)
67 pythonPath.addAll(pythonExecutorProperty.modulePaths)
69 val properties: MutableMap<String, Any> = hashMapOf()
70 properties["log"] = log
72 componentFunction = PythonExecutorUtils.getPythonComponent(pythonExecutorProperty.executionPath, pythonPath, content, pythonClassName, properties)
74 componentFunction!!.process(executionServiceInput)
78 override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
79 componentFunction!!.recover(runtimeException, executionRequest)