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 = "${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.isNotEmpty()) {
83 val prepareEnvInput = PrepareRemoteEnvInput(requestId = processId,
84 remoteScriptType = RemoteScriptType.PYTHON,
85 packages = dependencies
87 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
88 checkNotNull(prepareEnvOutput.status) {
89 "failed to get prepare remote env response status for requestId(${prepareEnvInput.requestId})"
93 val remoteExecutionInput = RemoteScriptExecutionInput(
94 requestId = processId,
95 remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
96 remoteScriptType = RemoteScriptType.PYTHON,
97 command = scriptCommand)
98 val remoteExecutionOutput = remoteScriptExecutionService.executeCommand(remoteExecutionInput)
99 checkNotNull(remoteExecutionOutput.status) {
100 "failed to get prepare remote command response status for requestId(${remoteExecutionOutput.requestId})"
103 remoteScriptExecutionService.close()
107 override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
108 bluePrintRuntimeService.getBluePrintError()
109 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")