2 * Copyright © 2019 IBM.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
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
36 interface RemoteScriptExecutionService {
37 suspend fun init(selector: Any)
38 suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
39 suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
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 {
50 private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
52 private var channel: ManagedChannel? = null
53 private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
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)
61 grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector.toString())
63 // Get the GRPC Channel
64 channel = grpcClientService.channel()
65 // Create Non Blocking Stub
66 commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newBlockingStub(channel)
68 checkNotNull(commandExecutorServiceGrpc) {
69 "failed to create command executor grpc client for selector($selector)"
73 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput)
74 : RemoteScriptExecutionOutput {
75 val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData())
77 checkNotNull(grpResponse.status) {
78 "failed to get GRPC prepare env response status for requestId(${prepareEnvInput.requestId})"
81 val remoteScriptExecutionOutput = grpResponse.asJavaData()
82 log.debug("Received prepare env response from command server for requestId(${prepareEnvInput.requestId})")
84 return remoteScriptExecutionOutput
87 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput)
88 : RemoteScriptExecutionOutput {
90 val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData())
92 checkNotNull(grpResponse.status) {
93 "failed to get GRPC response status for requestId(${remoteExecutionInput.requestId})"
96 val remoteScriptExecutionOutput = grpResponse.asJavaData()
97 log.debug("Received response from command server for requestId(${remoteExecutionInput.requestId})")
99 return remoteScriptExecutionOutput
102 override suspend fun close() {
103 channel?.shutdownNow()
107 fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
108 val correlationId = this.correlationId ?: this.requestId
110 val packageList = mutableListOf<Packages>()
112 this.packages.toList().forEach {
113 val pckage = Packages.newBuilder()
114 JsonFormat.parser().merge(it.toString(), pckage)
115 packageList.add(pckage.build())
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())
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())
141 fun RemoteIdentifier.asGrpcData(): Identifiers? {
142 return Identifiers.newBuilder()
143 .setBlueprintName(this.blueprintName)
144 .setBlueprintVersion(this.blueprintVersion)
148 fun Map<String, JsonNode>.asGrpcData(): Struct {
149 val struct = Struct.newBuilder()
150 JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
151 return struct.build()
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()