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