46969b1c50584e13174ca69ff4880eec47364e46
[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.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     lateinit var channel: ManagedChannel
52     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         // TODO('Verify the correct way to close the client conncetion")
98         if (channel != null) {
99             channel.shutdownNow()
100         }
101     }
102
103
104     fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
105         val correlationId = this.correlationId ?: this.requestId
106
107         return PrepareEnvInput.newBuilder()
108             .setIdentifiers(this.remoteIdentifier.asGrpcData())
109             .setRequestId(this.requestId)
110             .setCorrelationId(correlationId)
111             .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
112             .setTimeOut(this.timeOut.toInt())
113             .addAllPackages(this.packages)
114             .setProperties(this.properties.asGrpcData())
115             .build()
116     }
117
118     fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
119         val correlationId = this.correlationId ?: this.requestId
120         return ExecutionInput.newBuilder()
121             .setRequestId(this.requestId)
122             .setCorrelationId(correlationId)
123             .setIdentifiers(this.remoteIdentifier.asGrpcData())
124             .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
125             .setCommand(this.command)
126             .setTimeOut(this.timeOut.toInt())
127             .setProperties(this.properties.asGrpcData())
128             .setTimestamp(Timestamp.getDefaultInstance())
129             .build()
130     }
131
132     fun RemoteIdentifier?.asGrpcData(): Identifiers? {
133         return if (this != null) {
134             Identifiers.newBuilder()
135                 .setBlueprintName(this.blueprintName)
136                 .setBlueprintVersion(this.blueprintVersion)
137                 .build()
138         } else {
139             null
140         }
141     }
142
143     fun Map<String, JsonNode>.asGrpcData(): Struct {
144         val struct = Struct.newBuilder()
145         JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
146         return struct.build()
147     }
148
149     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
150         return RemoteScriptExecutionOutput(
151             requestId = this.requestId,
152             response = this.response,
153             status = StatusType.valueOf(this.status.name)
154         )
155     }
156
157 }