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.BluePrintProcessorException
24 import org.onap.ccsdk.cds.controllerblueprints.core.checkFileExists
25 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotBlank
26 import org.onap.ccsdk.cds.controllerblueprints.core.data.OperationAssignment
27 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnBean
31 import org.springframework.context.annotation.Scope
32 import org.springframework.stereotype.Component
34 @ConditionalOnBean(name = [ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION])
35 @Component("component-remote-python-executor")
36 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
37 open class ComponentRemotePythonExecutor(private val remoteScriptExecutionService: RemoteScriptExecutionService)
38 : AbstractComponentFunction() {
40 private val log = LoggerFactory.getLogger(ComponentRemotePythonExecutor::class.java)!!
43 const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
44 const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
47 override suspend fun processNB(executionRequest: ExecutionServiceInput) {
49 log.info("Processing : $operationInputs")
51 val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
52 val blueprintName = bluePrintContext.name()
53 val blueprintVersion = bluePrintContext.version()
55 val operationAssignment: OperationAssignment = bluePrintContext
56 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
58 val artifactName: String = operationAssignment.implementation?.primary
59 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
61 val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
63 checkNotBlank(artifactDefinition.file) { "couldn't get python script path($artifactName)" }
65 val pythonScript = normalizedFile(bluePrintContext.rootPath, artifactDefinition.file)
67 checkFileExists(pythonScript) { "python script(${pythonScript.absolutePath}) doesn't exists" }
69 val endPointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
70 val dynamicProperties = getOperationInput(INPUT_DYNAMIC_PROPERTIES)
72 // TODO("Python execution command and Resolve some expressions with dynamic properties")
73 val scriptCommand: String = "python ${pythonScript.absolutePath}"
75 val dependencies = operationAssignment.implementation?.dependencies
78 // Open GRPC Connection
79 remoteScriptExecutionService.init(endPointSelector.asText())
81 // If dependencies are defined, then install in remote server
82 if (dependencies != null && !dependencies.isEmpty()) {
83 val prepareEnvInput = PrepareRemoteEnvInput(
84 requestId = processId,
85 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
86 remoteScriptType = RemoteScriptType.PYTHON,
87 packages = dependencies
89 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
90 checkNotNull(prepareEnvOutput) {
91 "failed to get prepare remote env response for requestId(${prepareEnvInput.requestId})"
95 val remoteExecutionInput = RemoteScriptExecutionInput(
96 requestId = processId,
97 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
98 remoteScriptType = RemoteScriptType.PYTHON,
99 command = scriptCommand)
100 val remoteExecutionOutput = remoteScriptExecutionService.executeCommand(remoteExecutionInput)
101 checkNotNull(remoteExecutionOutput) {
102 "failed to get prepare remote command response for requestId(${remoteExecutionOutput.requestId})"
105 remoteScriptExecutionService.close()
109 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
110 bluePrintRuntimeService.getBluePrintError()
111 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")