2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Modifications Copyright © 2019 IBM, Bell Canada.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
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.services.execution.AbstractComponentFunction
25 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.BlueprintJythonService
26 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.scripts.PythonExecutorConstants
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
28 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmptyOrThrow
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory
32 import org.springframework.context.ApplicationContext
33 import org.springframework.context.annotation.Scope
34 import org.springframework.stereotype.Component
36 @Component("component-jython-executor")
37 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
38 open class ComponentJythonExecutor(private var applicationContext: ApplicationContext,
39 private val blueprintJythonService: BlueprintJythonService) : AbstractComponentFunction() {
41 private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java)
43 private var componentFunction: AbstractComponentFunction? = null
45 override fun prepareRequest(executionRequest: ExecutionServiceInput): ExecutionServiceInput {
46 val request = super.prepareRequest(executionRequest)
47 // Populate Component Instance
48 populateJythonComponentInstance()
52 override fun process(executionRequest: ExecutionServiceInput) {
53 log.info("Processing : $operationInputs")
54 // Invoke Jython Component Script
55 componentFunction!!.process(executionServiceInput)
59 override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
60 componentFunction!!.recover(runtimeException, executionRequest)
63 private fun populateJythonComponentInstance() {
64 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
66 val operationAssignment: OperationAssignment = bluePrintContext
67 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
69 val artifactName: String = operationAssignment.implementation?.primary
70 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
72 val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
74 val pythonFileName = artifactDefinition.file
75 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
77 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
79 val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
81 checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
83 val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
84 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
86 val jythonInstance: MutableMap<String, Any> = hashMapOf()
87 jythonInstance["log"] = LoggerFactory.getLogger(pythonClassName)
88 jythonInstance["bluePrintRuntimeService"] = bluePrintRuntimeService
90 instanceDependenciesNode.forEach { instanceName ->
91 jythonInstance[instanceName.textValue()] = applicationContext.getBean(instanceName.textValue())
94 // Setup componentFunction
95 componentFunction = blueprintJythonService.jythonInstance(bluePrintContext, pythonClassName,
96 content!!, jythonInstance)
97 componentFunction?.bluePrintRuntimeService = bluePrintRuntimeService
98 componentFunction?.executionServiceInput = executionServiceInput
99 componentFunction?.stepName = stepName
100 componentFunction?.interfaceName = interfaceName
101 componentFunction?.operationName = operationName
102 componentFunction?.processId = processId
103 componentFunction?.workflowName = workflowName