feadfbbd4f166dcc1e53cf17c3f98d71988ddbc8
[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 = "${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.isNotEmpty()) {
83                 val prepareEnvInput = PrepareRemoteEnvInput(requestId = processId,
84                         remoteScriptType = RemoteScriptType.PYTHON,
85                         packages = dependencies
86                 )
87                 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
88                 checkNotNull(prepareEnvOutput.status) {
89                     "failed to get prepare remote env response status for requestId(${prepareEnvInput.requestId})"
90                 }
91             }
92
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})"
101             }
102         } finally {
103             remoteScriptExecutionService.close()
104         }
105     }
106
107     override suspend fun recoverNB(runtimeException: RuntimeException, executionRequest: ExecutionServiceInput) {
108         bluePrintRuntimeService.getBluePrintError()
109                 .addError("Failed in ComponentJythonExecutor : ${runtimeException.message}")
110     }
111 }