Improve Remote Python Executor error handling and allow for structured response
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / RemoteScriptExecutionService.kt
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.services.execution
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.google.protobuf.Struct
21 import com.google.protobuf.Timestamp
22 import com.google.protobuf.util.JsonFormat
23 import io.grpc.ManagedChannel
24 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
25 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcClientService
26 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcLibPropertyService
27 import org.onap.ccsdk.cds.controllerblueprints.command.api.*
28 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
29 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
30 import org.slf4j.LoggerFactory
31 import org.springframework.beans.factory.config.ConfigurableBeanFactory
32 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
33 import org.springframework.context.annotation.Scope
34 import org.springframework.stereotype.Service
35
36 interface RemoteScriptExecutionService {
37     suspend fun init(selector: Any)
38     suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
39     suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
40     suspend fun close()
41 }
42
43 @Service(ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION)
44 @ConditionalOnProperty(prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
45         havingValue = "true", matchIfMissing = false)
46 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
47 class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService)
48     : RemoteScriptExecutionService {
49
50     private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
51
52     private var channel: ManagedChannel? = null
53     private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
54
55     override suspend fun init(selector: Any) {
56         // Get the GRPC Client Service based on selector
57         val grpcClientService: BluePrintGrpcClientService
58         if (selector is JsonNode) {
59             grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
60         } else {
61             grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector.toString())
62         }
63         // Get the GRPC Channel
64         channel = grpcClientService.channel()
65         // Create Non Blocking Stub
66         commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newBlockingStub(channel)
67
68         checkNotNull(commandExecutorServiceGrpc) {
69             "failed to create command executor grpc client for selector($selector)"
70         }
71     }
72
73     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput)
74             : RemoteScriptExecutionOutput {
75         val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData())
76
77         checkNotNull(grpResponse.status) {
78             "failed to get GRPC prepare env response status for requestId(${prepareEnvInput.requestId})"
79         }
80
81         val remoteScriptExecutionOutput = grpResponse.asJavaData()
82         log.debug("Received prepare env response from command server for requestId(${prepareEnvInput.requestId})")
83
84         return remoteScriptExecutionOutput
85     }
86
87     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput)
88             : RemoteScriptExecutionOutput {
89
90         val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData())
91
92         checkNotNull(grpResponse.status) {
93             "failed to get GRPC response status for requestId(${remoteExecutionInput.requestId})"
94         }
95
96         val remoteScriptExecutionOutput = grpResponse.asJavaData()
97         log.debug("Received response from command server for requestId(${remoteExecutionInput.requestId})")
98
99         return remoteScriptExecutionOutput
100     }
101
102     override suspend fun close() {
103         channel?.shutdownNow()
104     }
105
106
107     fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
108         val correlationId = this.correlationId ?: this.requestId
109
110         val packageList = mutableListOf<Packages>()
111
112         this.packages.toList().forEach {
113             val pckage = Packages.newBuilder()
114             JsonFormat.parser().merge(it.toString(), pckage)
115             packageList.add(pckage.build())
116         }
117
118         return PrepareEnvInput.newBuilder()
119                 .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
120                 .setRequestId(this.requestId)
121                 .setCorrelationId(correlationId)
122                 .setTimeOut(this.timeOut.toInt())
123                 .addAllPackages(packageList)
124                 .setProperties(this.properties.asGrpcData())
125                 .build()
126     }
127
128     fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
129         val correlationId = this.correlationId ?: this.requestId
130         return ExecutionInput.newBuilder()
131                 .setRequestId(this.requestId)
132                 .setCorrelationId(correlationId)
133                 .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
134                 .setCommand(this.command)
135                 .setTimeOut(this.timeOut.toInt())
136                 .setProperties(this.properties.asGrpcData())
137                 .setTimestamp(Timestamp.getDefaultInstance())
138                 .build()
139     }
140
141     fun RemoteIdentifier.asGrpcData(): Identifiers? {
142         return Identifiers.newBuilder()
143                 .setBlueprintName(this.blueprintName)
144                 .setBlueprintVersion(this.blueprintVersion)
145                 .build()
146     }
147
148     fun Map<String, JsonNode>.asGrpcData(): Struct {
149         val struct = Struct.newBuilder()
150         JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
151         return struct.build()
152     }
153
154     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
155         return RemoteScriptExecutionOutput(
156                 requestId = this.requestId,
157                 response = this.responseList,
158                 status = StatusType.valueOf(this.status.name),
159                 payload =  payload.jsonAsJsonType()
160         )
161     }
162
163 }