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 org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
20 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.AbstractComponentFunction
21 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.ExecutionServiceConstant
22 import org.onap.ccsdk.cds.blueprintsprocessor.services.execution.RemoteScriptExecutionService
23 import org.onap.ccsdk.cds.controllerblueprints.core.*
24 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
25 import org.slf4j.LoggerFactory
26 import org.springframework.beans.factory.config.ConfigurableBeanFactory
27 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
28 import org.springframework.context.annotation.Scope
29 import org.springframework.stereotype.Component
31 @ConditionalOnBean(name = [ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION])
32 @Component("component-remote-python-executor")
33 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
34 open class ComponentRemotePythonExecutor(private val remoteScriptExecutionService: RemoteScriptExecutionService)
35 : AbstractComponentFunction() {
37 private val log = LoggerFactory.getLogger(ComponentRemotePythonExecutor::class.java)!!
40 const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
41 const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
42 const val INPUT_ARGUMENT_PROPERTIES = "argument-properties"
43 const val INPUT_COMMAND = "command"
44 const val INPUT_PACKAGES = "packages"
46 const val ATTRIBUTE_PREPARE_ENV_LOG = "prepare-environment-logs"
47 const val ATTRIBUTE_EXEC_CMD_LOG = "execute-command-logs"
50 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
52 log.info("Processing : $operationInputs")
54 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
55 val blueprintName = bluePrintContext.name()
56 val blueprintVersion = bluePrintContext.version()
58 val operationAssignment: OperationAssignment = bluePrintContext
59 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
61 val artifactName: String = operationAssignment.implementation?.primary
62 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
64 val artifactDefinition =
65 bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
67 checkNotBlank(artifactDefinition.file) { "couldn't get python script path($artifactName)" }
69 val pythonScript = normalizedFile(bluePrintContext.rootPath, artifactDefinition.file)
71 checkFileExists(pythonScript) { "python script(${pythonScript.absolutePath}) doesn't exists" }
73 val endPointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
74 val dynamicProperties = getOptionalOperationInput(INPUT_DYNAMIC_PROPERTIES)
75 val packages = getOptionalOperationInput(INPUT_PACKAGES)?.returnNullIfMissing()
77 val args = getOptionalOperationInput(INPUT_ARGUMENT_PROPERTIES)?.returnNullIfMissing()
78 ?.rootFieldsToMap()?.toSortedMap()?.values?.map { it.textValue() }?.joinToString(" ")
80 val command = getOperationInput(INPUT_COMMAND).asText()
81 var scriptCommand = command.replace(pythonScript.name, pythonScript.absolutePath)
82 if (args != null && args.isNotEmpty()) {
83 scriptCommand = scriptCommand.plus(" ").plus(args)
87 // Open GRPC Connection
88 remoteScriptExecutionService.init(endPointSelector.asText())
90 // If packages are defined, then install in remote server
91 if (packages != null) {
92 val prepareEnvInput = PrepareRemoteEnvInput(requestId = processId,
93 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName,
94 blueprintVersion = blueprintVersion),
97 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
98 setAttribute(ATTRIBUTE_PREPARE_ENV_LOG, prepareEnvOutput.response.asJsonPrimitive())
99 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, "N/A".asJsonPrimitive())
100 check(prepareEnvOutput.status == StatusType.SUCCESS) {
101 "failed to get prepare remote env response status for requestId(${prepareEnvInput.requestId})"
104 // Populate command execution properties and pass it to the remote server
105 val properties = dynamicProperties?.returnNullIfMissing()?.rootFieldsToMap() ?: hashMapOf()
107 val remoteExecutionInput = RemoteScriptExecutionInput(
108 requestId = processId,
109 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
110 command = scriptCommand,
111 properties = properties)
112 val remoteExecutionOutput = remoteScriptExecutionService.executeCommand(remoteExecutionInput)
113 setAttribute(ATTRIBUTE_EXEC_CMD_LOG, remoteExecutionOutput.response.asJsonPrimitive())
114 check(remoteExecutionOutput.status == StatusType.SUCCESS) {
115 "failed to get prepare remote command response status for requestId(${remoteExecutionOutput.requestId})"
118 } catch (e: Exception) {
119 log.error("Failed to process on remote executor", e)
121 remoteScriptExecutionService.close()
125 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
126 bluePrintRuntimeService.getBluePrintError()
127 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")