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.apps.blueprintsprocessor.functions.python.executor
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
34 @Component("component-jython-executor")
35 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
36 open class ComponentJythonExecutor(private var applicationContext: ApplicationContext,
37 private val blueprintPythonService: BlueprintPythonService) : AbstractComponentFunction() {
39 private val log = LoggerFactory.getLogger(ComponentJythonExecutor::class.java)
41 private var componentFunction: AbstractComponentFunction? = null
43 fun populateJythonComponentInstance(executionServiceInput: ExecutionServiceInput) {
44 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
46 val operationAssignment: OperationAssignment = bluePrintContext
47 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
49 val artifactName: String = operationAssignment.implementation?.primary
50 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
52 val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
54 val pythonFileName = artifactDefinition.file
55 ?: throw BluePrintProcessorException("missing file name for node template ($nodeTemplateName)'s artifactName($artifactName)")
57 val pythonClassName = FilenameUtils.getBaseName(pythonFileName)
59 val content: String? = bluePrintRuntimeService.resolveNodeTemplateArtifact(nodeTemplateName, artifactName)
61 checkNotEmptyOrThrow(content, "artifact ($artifactName) content is empty")
63 val instanceDependenciesNode: ArrayNode = operationInputs[PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES] as? ArrayNode
64 ?: throw BluePrintProcessorException("Failed to get property(${PythonExecutorConstants.INPUT_INSTANCE_DEPENDENCIES})")
66 val jythonContextInstance: MutableMap<String, Any> = hashMapOf()
67 jythonContextInstance["log"] = LoggerFactory.getLogger(pythonClassName)
68 jythonContextInstance["bluePrintRuntimeService"] = bluePrintRuntimeService
69 instanceDependenciesNode?.forEach { instanceName ->
70 val instance = instanceName.textValue()
71 val value = applicationContext.getBean(instance)
72 ?: throw BluePrintProcessorException("couldn't get the dependency instance($instance)")
73 jythonContextInstance[instance] = value
76 componentFunction = blueprintPythonService.jythonInstance(bluePrintContext, pythonClassName,
77 content!!, jythonContextInstance)
81 override fun process(executionServiceInput: ExecutionServiceInput) {
83 log.info("Processing : $operationInputs")
84 checkNotNull(bluePrintRuntimeService) { "failed to get bluePrintRuntimeService" }
86 // Populate Component Instance
87 populateJythonComponentInstance(executionServiceInput)
89 // Invoke Jython Component Script
90 componentFunction!!.process(executionServiceInput)
94 override fun recover(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
95 componentFunction!!.recover(runtimeException, executionRequest)