Merge "Creating findOneBluePrintModel (configuration)"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / RemoteScriptExecutionService.kt
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.PrepareRemoteEnvInput
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteIdentifier
26 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
27 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
28 import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
29 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcClientService
30 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcLibPropertyService
31 import org.onap.ccsdk.cds.controllerblueprints.command.api.CommandExecutorServiceGrpc
32 import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionInput
33 import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionOutput
34 import org.onap.ccsdk.cds.controllerblueprints.command.api.Identifiers
35 import org.onap.ccsdk.cds.controllerblueprints.command.api.Packages
36 import org.onap.ccsdk.cds.controllerblueprints.command.api.PrepareEnvInput
37 import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
38 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
39 import org.slf4j.LoggerFactory
40 import org.springframework.beans.factory.config.ConfigurableBeanFactory
41 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
42 import org.springframework.context.annotation.Scope
43 import org.springframework.stereotype.Service
44
45 interface RemoteScriptExecutionService {
46     suspend fun init(selector: Any)
47     suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
48     suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
49     suspend fun close()
50 }
51
52 @Service(ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION)
53 @ConditionalOnProperty(
54     prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
55     havingValue = "true", matchIfMissing = false
56 )
57 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
58 class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService) :
59     RemoteScriptExecutionService {
60
61     private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
62
63     private var channel: ManagedChannel? = null
64     private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
65
66     override suspend fun init(selector: Any) {
67         // Get the GRPC Client Service based on selector
68         val grpcClientService: BluePrintGrpcClientService
69         if (selector is JsonNode) {
70             grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
71         } else {
72             grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector.toString())
73         }
74         // Get the GRPC Channel
75         channel = grpcClientService.channel()
76         // Create Non Blocking Stub
77         commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newBlockingStub(channel)
78
79         checkNotNull(commandExecutorServiceGrpc) {
80             "failed to create command executor grpc client for selector($selector)"
81         }
82     }
83
84     override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput):
85             RemoteScriptExecutionOutput {
86         val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData())
87
88         checkNotNull(grpResponse.status) {
89             "failed to get GRPC prepare env response status for requestId(${prepareEnvInput.requestId})"
90         }
91
92         val remoteScriptExecutionOutput = grpResponse.asJavaData()
93         log.debug("Received prepare env response from command server for requestId(${prepareEnvInput.requestId})")
94
95         return remoteScriptExecutionOutput
96     }
97
98     override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput):
99             RemoteScriptExecutionOutput {
100
101         val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData())
102
103         checkNotNull(grpResponse.status) {
104             "failed to get GRPC response status for requestId(${remoteExecutionInput.requestId})"
105         }
106
107         val remoteScriptExecutionOutput = grpResponse.asJavaData()
108         log.debug("Received response from command server for requestId(${remoteExecutionInput.requestId})")
109
110         return remoteScriptExecutionOutput
111     }
112
113     override suspend fun close() {
114         channel?.shutdownNow()
115     }
116
117     fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
118         val correlationId = this.correlationId ?: this.requestId
119
120         val packageList = mutableListOf<Packages>()
121
122         this.packages.toList().forEach {
123             val pckage = Packages.newBuilder()
124             JsonFormat.parser().merge(it.toString(), pckage)
125             packageList.add(pckage.build())
126         }
127
128         return PrepareEnvInput.newBuilder()
129             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
130             .setRequestId(this.requestId)
131             .setCorrelationId(correlationId)
132             .setTimeOut(this.timeOut.toInt())
133             .addAllPackages(packageList)
134             .setProperties(this.properties.asGrpcData())
135             .build()
136     }
137
138     fun RemoteScriptExecutionInput.asGrpcData(): ExecutionInput {
139         val correlationId = this.correlationId ?: this.requestId
140         return ExecutionInput.newBuilder()
141             .setRequestId(this.requestId)
142             .setCorrelationId(correlationId)
143             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
144             .setCommand(this.command)
145             .setTimeOut(this.timeOut.toInt())
146             .setProperties(this.properties.asGrpcData())
147             .setTimestamp(Timestamp.getDefaultInstance())
148             .build()
149     }
150
151     fun RemoteIdentifier.asGrpcData(): Identifiers? {
152         return Identifiers.newBuilder()
153             .setBlueprintName(this.blueprintName)
154             .setBlueprintVersion(this.blueprintVersion)
155             .build()
156     }
157
158     fun Map<String, JsonNode>.asGrpcData(): Struct {
159         val struct = Struct.newBuilder()
160         JsonFormat.parser().merge(JacksonUtils.getJson(this), struct)
161         return struct.build()
162     }
163
164     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
165         return RemoteScriptExecutionOutput(
166             requestId = this.requestId,
167             response = this.responseList,
168             status = StatusType.valueOf(this.status.name),
169             payload = payload.jsonAsJsonType()
170         )
171     }
172 }