99d4f8c2428d5a0195c9db7f8944aef7e5e56a89
[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     private var channel: ManagedChannel? = null
52     private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
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.newBlockingStub(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())
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())
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         val packageList = mutableListOf<Packages>()
105
106         this.packages.toList().forEach {
107             val pckage = Packages.newBuilder()
108             JsonFormat.parser().merge(it.toString(), pckage)
109             packageList.add(pckage.build())
110             }
111
112         return PrepareEnvInput.newBuilder()
113             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
114             .setRequestId(this.requestId)
115             .setCorrelationId(correlationId)
116             .setTimeOut(this.timeOut.toInt())
117             .addAllPackages(packageList)
118             .setProperties(this.properties.asGrpcData())
119             .build()
120     }
121
122     fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
123         val correlationId = this.correlationId ?: this.requestId
124         return ExecutionInput.newBuilder()
125             .setRequestId(this.requestId)
126             .setCorrelationId(correlationId)
127             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
128             .setCommand(this.command)
129             .setTimeOut(this.timeOut.toInt())
130             .setProperties(this.properties.asGrpcData())
131             .setTimestamp(Timestamp.getDefaultInstance())
132             .build()
133     }
134
135     fun RemoteIdentifier.asGrpcData(): Identifiers? {
136         return Identifiers.newBuilder()
137             .setBlueprintName(this.blueprintName)
138             .setBlueprintVersion(this.blueprintVersion)
139             .build()
140     }
141
142     fun Map<String, JsonNode>.asGrpcData(): Struct {
143         val struct = Struct.newBuilder()
144         JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
145         return struct.build()
146     }
147
148     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
149         return RemoteScriptExecutionOutput(
150             requestId = this.requestId,
151             response = this.response,
152             status = StatusType.valueOf(this.status.name)
153         )
154     }
155
156 }