31eb38ca4ddd050d781ba2cc0ebafcf390d18f91
[ccsdk/cds.git] /
1 /*
2  *  Copyright © 2019 IBM.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.python.executor
18
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
33
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() {
39
40     private val log = LoggerFactory.getLogger(ComponentRemotePythonExecutor::class.java)!!
41
42     companion object {
43         const val INPUT_ENDPOINT_SELECTOR = "endpoint-selector"
44         const val INPUT_DYNAMIC_PROPERTIES = "dynamic-properties"
45     }
46
47     override suspend fun processNB(executionRequest: ExecutionServiceInput) {
48
49         log.info("Processing : $operationInputs")
50
51         val bluePrintContext = bluePrintRuntimeService.bluePrintContext()
52         val blueprintName = bluePrintContext.name()
53         val blueprintVersion = bluePrintContext.version()
54
55         val operationAssignment: OperationAssignment = bluePrintContext
56                 .nodeTemplateInterfaceOperation(nodeTemplateName, interfaceName, operationName)
57
58         val artifactName: String = operationAssignment.implementation?.primary
59                 ?: throw BluePrintProcessorException("missing primary field to get artifact name for node template ($nodeTemplateName)")
60
61         val artifactDefinition = bluePrintRuntimeService.resolveNodeTemplateArtifactDefinition(nodeTemplateName, artifactName)
62
63         checkNotBlank(artifactDefinition.file) { "couldn't get python script path($artifactName)" }
64
65         val pythonScript = normalizedFile(bluePrintContext.rootPath, artifactDefinition.file)
66
67         checkFileExists(pythonScript) { "python script(${pythonScript.absolutePath}) doesn't exists" }
68
69         val endPointSelector = getOperationInput(INPUT_ENDPOINT_SELECTOR)
70         val dynamicProperties = getOperationInput(INPUT_DYNAMIC_PROPERTIES)
71
72         // TODO("Python execution command and Resolve some expressions with dynamic properties")
73         val scriptCommand: String = "python ${pythonScript.absolutePath}"
74
75         val dependencies = operationAssignment.implementation?.dependencies
76
77         try {
78             // Open GRPC Connection
79             remoteScriptExecutionService.init(endPointSelector.asText())
80
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
88                 )
89                 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
90                 checkNotNull(prepareEnvOutput) {
91                     "failed to get prepare remote env response for requestId(${prepareEnvInput.requestId})"
92                 }
93             }
94
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})"
103             }
104         } finally {
105             remoteScriptExecutionService.close()
106         }
107     }
108
109     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
110         bluePrintRuntimeService.getBluePrintError()
111                 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")
112     }
113 }