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.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
35 interface RemoteScriptExecutionService {
36 suspend fun init(selector: String)
37 suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
38 suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
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 {
49 private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
51 private var channel: ManagedChannel? = null
52 private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
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)
62 checkNotNull(commandExecutorServiceGrpc) {
63 "failed to create command executor grpc client for selector($selector)"
67 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput)
68 : RemoteScriptExecutionOutput {
69 val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData())
71 checkNotNull(grpResponse.status) {
72 "failed to get GRPC prepare env response status for requestId($prepareEnvInput.requestId)"
75 val remoteScriptExecutionOutput = grpResponse.asJavaData()
76 log.debug("Received prepare env response from command server for requestId($prepareEnvInput.requestId)")
78 return remoteScriptExecutionOutput
81 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput)
82 : RemoteScriptExecutionOutput {
84 val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData())
86 checkNotNull(grpResponse.status) {
87 "failed to get GRPC response status for requestId($remoteExecutionInput.requestId)"
90 val remoteScriptExecutionOutput = grpResponse.asJavaData()
91 log.debug("Received response from command server for requestId($remoteExecutionInput.requestId)")
93 return remoteScriptExecutionOutput
96 override suspend fun close() {
97 channel?.shutdownNow()
101 fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
102 val correlationId = this.correlationId ?: this.requestId
104 val packageList = mutableListOf<Packages>()
106 this.packages.toList().forEach {
107 val pckage = Packages.newBuilder()
108 JsonFormat.parser().merge(it.toString(), pckage)
109 packageList.add(pckage.build())
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())
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())
135 fun RemoteIdentifier.asGrpcData(): Identifiers? {
136 return Identifiers.newBuilder()
137 .setBlueprintName(this.blueprintName)
138 .setBlueprintVersion(this.blueprintVersion)
142 fun Map<String, JsonNode>.asGrpcData(): Struct {
143 val struct = Struct.newBuilder()
144 JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
145 return struct.build()
148 fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
149 return RemoteScriptExecutionOutput(
150 requestId = this.requestId,
151 response = this.responseList,
152 status = StatusType.valueOf(this.status.name)