2 * Copyright © 2019 IBM.
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.cds.blueprintsprocessor.functions.python.executor
19 import com.fasterxml.jackson.databind.node.MissingNode
20 import com.fasterxml.jackson.databind.node.NullNode
21 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
22 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
23 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceConstant
24 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
25 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintProcessorException
26 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
27 import org.onap.ccsdk.cds.controllerblueprints.core.checkFileExists
28 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotBlank
29 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintVelocityTemplateService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
33 import org.slf4j.LoggerFactory
34 import org.springframework.beans.factory.config.ConfigurableBeanFactory
35 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
36 import org.springframework.context.annotation.Scope
37 import org.springframework.stereotype.Component
38 import java.lang.Exception
40 @ConditionalOnBean(name = [ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION])
41 @Component("component-remote-python-executor")
42 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
43 open class ComponentRemotePythonExecutor(private val remoteScriptExecutionService: RemoteScriptExecutionService)
44 : AbstractComponentFunction() {
46 private val log = LoggerFactory.getLogger(ComponentRemotePythonExecutor::class.java)!!
49 const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
50 const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
51 const val INPUT_ARGUMENT_PROPERTIES = "argument-properties"
52 const val INPUT_COMMAND = "command"
53 const val INPUT_PACKAGES = "packages"
55 const val ATTRIBUTE_PREPARE_ENV_LOG = "prepare-environment-logs"
56 const val ATTRIBUTE_EXEC_CMD_LOG = "execute-command-logs"
59 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
61 log.info("Processing : $operationInputs")
63 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
64 val blueprintName = bluePrintContext.name()
65 val blueprintVersion = bluePrintContext.version()
67 val operationAssignment: OperationAssignment = bluePrintContext
68 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
70 val artifactName: String = operationAssignment.implementation?.primary
71 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
73 val artifactDefinition =
74 bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
76 checkNotBlank(artifactDefinition.file) { "couldn't get python script path($artifactName)" }
78 val pythonScript = normalizedFile(bluePrintContext.rootPath, artifactDefinition.file)
80 checkFileExists(pythonScript) { "python script(${pythonScript.absolutePath}) doesn't exists" }
82 val endPointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
83 val dynamicProperties = getOperationInput(INPUT_DYNAMIC_PROPERTIES)
84 val packages = getOperationInput(INPUT_PACKAGES)
85 val argumentProperties = getOperationInput(INPUT_ARGUMENT_PROPERTIES)
87 var command = getOperationInput(INPUT_COMMAND).asText()
88 command = command.replace(pythonScript.name, pythonScript.absolutePath)
89 val scriptCommand = BluePrintVelocityTemplateService.generateContent(command, json = JacksonUtils.getJson(argumentProperties))
92 // Open GRPC Connection
93 remoteScriptExecutionService.init(endPointSelector.asText())
95 // If packages are defined, then install in remote server
96 if (packages !is MissingNode && packages !is NullNode) {
97 val prepareEnvInput = PrepareRemoteEnvInput(requestId = processId,
98 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName,
99 blueprintVersion = blueprintVersion),
102 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
103 setAttribute(ATTRIBUTE_PREPARE_ENV_LOG, prepareEnvOutput.response.asJsonPrimitive())
104 check(prepareEnvOutput.status == StatusType.SUCCESS) {
105 "failed to get prepare remote env response status for requestId(${prepareEnvInput.requestId})"
109 val remoteExecutionInput = RemoteScriptExecutionInput(
110 requestId = processId,
111 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
112 command = scriptCommand)
113 val remoteExecutionOutput = remoteScriptExecutionService.executeCommand(remoteExecutionInput)
114 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, remoteExecutionOutput.response.asJsonPrimitive())
115 check(remoteExecutionOutput.status == StatusType.SUCCESS) {
116 "failed to get prepare remote command response status for requestId(${remoteExecutionOutput.requestId})"
119 } catch (e: Exception) {
120 log.error("Failed to process on remote executor", e)
122 remoteScriptExecutionService.close()
126 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
127 bluePrintRuntimeService.getBluePrintError()
128 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")