Merge "Dynamic API sample request and 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.BluePrintGrpcLibPropertyService
26 import org.onap.ccsdk.cds.controllerblueprints.command.api.*
27 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
28 import org.slf4j.LoggerFactory
29 import org.springframework.beans.factory.config.ConfigurableBeanFactory
30 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
31 import org.springframework.context.annotation.Scope
32 import org.springframework.stereotype.Service
33
34
35 interface RemoteScriptExecutionService {
36     suspend fun init(selector: String)
37     suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
38     suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
39     suspend fun close()
40 }
41
42 @Service(ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION)
43 @ConditionalOnProperty(prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
44     havingValue = "true", matchIfMissing = false)
45 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
46 class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService)
47     : RemoteScriptExecutionService {
48
49     private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
50
51     private var channel: ManagedChannel? = null
52     private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceFutureStub
53
54     override suspend fun init(selector: String) {
55         // Get the GRPC Client Service based on selector
56         val grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
57         // Get the GRPC Channel
58         channel = grpcClientService.channel()
59         // Create Non Blocking Stub
60         commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newFutureStub(channel)
61
62         checkNotNull(commandExecutorServiceGrpc) {
63             "failed to create command executor grpc client for selector($selector)"
64         }
65     }
66
67     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput)
68             : RemoteScriptExecutionOutput {
69         val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData()).get()
70
71         checkNotNull(grpResponse.status) {
72             "failed to get GRPC prepare env response status for requestId($prepareEnvInput.requestId)"
73         }
74
75         val remoteScriptExecutionOutput = grpResponse.asJavaData()
76         log.debug("Received prepare env response from command server for requestId($prepareEnvInput.requestId)")
77
78         return remoteScriptExecutionOutput
79     }
80
81     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput)
82             : RemoteScriptExecutionOutput {
83
84         val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData()).get()
85
86         checkNotNull(grpResponse.status) {
87             "failed to get GRPC response status for requestId($remoteExecutionInput.requestId)"
88         }
89
90         val remoteScriptExecutionOutput = grpResponse.asJavaData()
91         log.debug("Received response from command server for requestId($remoteExecutionInput.requestId)")
92
93         return remoteScriptExecutionOutput
94     }
95
96     override suspend fun close() {
97         channel?.shutdownNow()
98     }
99
100
101     fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
102         val correlationId = this.correlationId ?: this.requestId
103
104         return PrepareEnvInput.newBuilder()
105             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
106             .setRequestId(this.requestId)
107             .setCorrelationId(correlationId)
108             .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
109             .setTimeOut(this.timeOut.toInt())
110             .addAllPackages(this.packages)
111             .setProperties(this.properties.asGrpcData())
112             .build()
113     }
114
115     fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
116         val correlationId = this.correlationId ?: this.requestId
117         return ExecutionInput.newBuilder()
118             .setRequestId(this.requestId)
119             .setCorrelationId(correlationId)
120             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
121             .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
122             .setCommand(this.command)
123             .setTimeOut(this.timeOut.toInt())
124             .setProperties(this.properties.asGrpcData())
125             .setTimestamp(Timestamp.getDefaultInstance())
126             .build()
127     }
128
129     fun RemoteIdentifier.asGrpcData(): Identifiers? {
130         return Identifiers.newBuilder()
131             .setBlueprintName(this.blueprintName)
132             .setBlueprintVersion(this.blueprintVersion)
133             .build()
134     }
135
136     fun Map<String, JsonNode>.asGrpcData(): Struct {
137         val struct = Struct.newBuilder()
138         JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
139         return struct.build()
140     }
141
142     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
143         return RemoteScriptExecutionOutput(
144             requestId = this.requestId,
145             response = this.response,
146             status = StatusType.valueOf(this.status.name)
147         )
148     }
149
150 }