2 * Copyright © 2019 IBM.
3 * Modifications Copyright © 2020 Bell Canada.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package org.onap.ccsdk.cds.blueprintsprocessor.services.execution
20 import com.fasterxml.jackson.databind.JsonNode
21 import com.google.protobuf.Struct
22 import com.google.protobuf.Timestamp
23 import com.google.protobuf.util.JsonFormat
24 import io.grpc.ManagedChannel
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteIdentifier
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
30 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcClientService
31 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcLibPropertyService
32 import org.onap.ccsdk.cds.controllerblueprints.command.api.CommandExecutorServiceGrpc
33 import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionInput
34 import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionOutput
35 import org.onap.ccsdk.cds.controllerblueprints.command.api.Identifiers
36 import org.onap.ccsdk.cds.controllerblueprints.command.api.Packages
37 import org.onap.ccsdk.cds.controllerblueprints.command.api.PrepareEnvInput
38 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
39 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
40 import org.slf4j.LoggerFactory
41 import org.springframework.beans.factory.config.ConfigurableBeanFactory
42 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
43 import org.springframework.context.annotation.Scope
44 import org.springframework.stereotype.Service
45 import java.util.concurrent.TimeUnit
47 interface RemoteScriptExecutionService {
48 suspend fun init(selector: Any)
49 suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
50 suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
54 @Service(ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION)
55 @ConditionalOnProperty(
56 prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
57 havingValue = "true", matchIfMissing = false
59 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
60 class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService) :
61 RemoteScriptExecutionService {
63 private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
65 private var channel: ManagedChannel? = null
66 private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
68 override suspend fun init(selector: Any) {
69 // Get the GRPC Client Service based on selector
70 val grpcClientService: BluePrintGrpcClientService = if (selector is JsonNode) {
71 bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
73 bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector.toString())
76 // Get the GRPC Channel
77 channel = grpcClientService.channel()
78 // Create Non Blocking Stub
79 commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newBlockingStub(channel)
81 checkNotNull(commandExecutorServiceGrpc) {
82 "failed to create command executor grpc client for selector($selector)"
86 override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
87 val grpResponse = commandExecutorServiceGrpc
88 .withDeadlineAfter(prepareEnvInput.timeOut * 1000, TimeUnit.MILLISECONDS)
89 .prepareEnv(prepareEnvInput.asGrpcData())
91 checkNotNull(grpResponse.status) {
92 "failed to get GRPC prepare env response status for requestId(${prepareEnvInput.requestId})"
95 val remoteScriptExecutionOutput = grpResponse.asJavaData()
96 log.debug("Received prepare env response from command server for requestId(${prepareEnvInput.requestId})")
98 return remoteScriptExecutionOutput
101 override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
103 commandExecutorServiceGrpc
104 .withDeadlineAfter(remoteExecutionInput.timeOut * 1000, TimeUnit.MILLISECONDS)
105 .executeCommand(remoteExecutionInput.asGrpcData())
107 checkNotNull(grpResponse.status) {
108 "failed to get GRPC response status for requestId(${remoteExecutionInput.requestId})"
111 log.debug("Received response from command server for requestId(${remoteExecutionInput.requestId})")
112 return grpResponse.asJavaData()
115 override suspend fun close() {
116 channel?.shutdownNow()
119 fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
120 val correlationId = this.correlationId ?: this.requestId
122 val packageList = mutableListOf<Packages>()
124 this.packages.toList().forEach {
125 val pckage = Packages.newBuilder()
126 JsonFormat.parser().merge(it.toString(), pckage)
127 packageList.add(pckage.build())
130 return PrepareEnvInput.newBuilder()
131 .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
132 .setRequestId(this.requestId)
133 .setSubRequestId(this.subRequestId)
134 .setOriginatorId(this.originatorId)
135 .setCorrelationId(correlationId)
136 .setTimeOut(this.timeOut.toInt())
137 .addAllPackages(packageList)
138 .setProperties(this.properties.asGrpcData())
142 fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
143 val correlationId = this.correlationId ?: this.requestId
144 return ExecutionInput.newBuilder()
145 .setRequestId(this.requestId)
146 .setSubRequestId(this.subRequestId)
147 .setOriginatorId(this.originatorId)
148 .setCorrelationId(correlationId)
149 .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
150 .setCommand(this.command)
151 .setTimeOut(this.timeOut.toInt())
152 .setProperties(this.properties.asGrpcData())
153 .setTimestamp(Timestamp.getDefaultInstance())
157 fun RemoteIdentifier.asGrpcData(): Identifiers? {
158 return Identifiers.newBuilder()
159 .setBlueprintName(this.blueprintName)
160 .setBlueprintVersion(this.blueprintVersion)
164 fun Map<String, JsonNode>.asGrpcData(): Struct {
165 val struct = Struct.newBuilder()
166 JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
167 return struct.build()
170 fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
171 return RemoteScriptExecutionOutput(
172 requestId = this.requestId,
173 response = this.responseList,
174 status = StatusType.valueOf(this.status.name),
175 payload = payload.jsonAsJsonType()