Add request IDs in command-executor log
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / services / execution-service / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / services / execution / RemoteScriptExecutionService.kt
index 7db5f52..35f1567 100644 (file)
@@ -1,5 +1,6 @@
 /*
  *  Copyright © 2019 IBM.
+ *  Modifications Copyright © 2020 Bell Canada.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -21,93 +22,119 @@ import com.google.protobuf.Struct
 import com.google.protobuf.Timestamp
 import com.google.protobuf.util.JsonFormat
 import io.grpc.ManagedChannel
-import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.*
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.PrepareRemoteEnvInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteIdentifier
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionInput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.RemoteScriptExecutionOutput
+import org.onap.ccsdk.cds.blueprintsprocessor.core.api.data.StatusType
+import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcClientService
 import org.onap.ccsdk.cds.blueprintsprocessor.grpc.service.BluePrintGrpcLibPropertyService
-import org.onap.ccsdk.cds.controllerblueprints.command.api.*
+import org.onap.ccsdk.cds.controllerblueprints.command.api.CommandExecutorServiceGrpc
+import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionInput
+import org.onap.ccsdk.cds.controllerblueprints.command.api.ExecutionOutput
+import org.onap.ccsdk.cds.controllerblueprints.command.api.Identifiers
+import org.onap.ccsdk.cds.controllerblueprints.command.api.Packages
+import org.onap.ccsdk.cds.controllerblueprints.command.api.PrepareEnvInput
+import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.slf4j.LoggerFactory
 import org.springframework.beans.factory.config.ConfigurableBeanFactory
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
 import org.springframework.context.annotation.Scope
 import org.springframework.stereotype.Service
-
+import java.util.concurrent.TimeUnit
 
 interface RemoteScriptExecutionService {
-    suspend fun init(selector: String)
+    suspend fun init(selector: Any)
     suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput
     suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput
     suspend fun close()
 }
 
 @Service(ExecutionServiceConstant.SERVICE_GRPC_REMOTE_SCRIPT_EXECUTION)
-@ConditionalOnProperty(prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
-    havingValue = "true", matchIfMissing = false)
+@ConditionalOnProperty(
+    prefix = "blueprintprocessor.remoteScriptCommand", name = arrayOf("enabled"),
+    havingValue = "true", matchIfMissing = false
+)
 @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
-class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService)
-    RemoteScriptExecutionService {
+class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyService: BluePrintGrpcLibPropertyService) :
+    RemoteScriptExecutionService {
 
     private val log = LoggerFactory.getLogger(GrpcRemoteScriptExecutionService::class.java)!!
 
     private var channel: ManagedChannel? = null
-    private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceFutureStub
+    private lateinit var commandExecutorServiceGrpc: CommandExecutorServiceGrpc.CommandExecutorServiceBlockingStub
 
-    override suspend fun init(selector: String) {
+    override suspend fun init(selector: Any) {
         // Get the GRPC Client Service based on selector
-        val grpcClientService = bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
+        val grpcClientService: BluePrintGrpcClientService = if (selector is JsonNode) {
+            bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector)
+        } else {
+            bluePrintGrpcLibPropertyService.blueprintGrpcClientService(selector.toString())
+        }
+
         // Get the GRPC Channel
         channel = grpcClientService.channel()
         // Create Non Blocking Stub
-        commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newFutureStub(channel)
+        commandExecutorServiceGrpc = CommandExecutorServiceGrpc.newBlockingStub(channel)
 
         checkNotNull(commandExecutorServiceGrpc) {
             "failed to create command executor grpc client for selector($selector)"
         }
     }
 
-    override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput)
-            : RemoteScriptExecutionOutput {
-        val grpResponse = commandExecutorServiceGrpc.prepareEnv(prepareEnvInput.asGrpcData()).get()
+    override suspend fun prepareEnv(prepareEnvInput: PrepareRemoteEnvInput): RemoteScriptExecutionOutput {
+        val grpResponse = commandExecutorServiceGrpc
+            .withDeadlineAfter(prepareEnvInput.timeOut * 1000, TimeUnit.MILLISECONDS)
+            .prepareEnv(prepareEnvInput.asGrpcData())
 
         checkNotNull(grpResponse.status) {
-            "failed to get GRPC prepare env response status for requestId($prepareEnvInput.requestId)"
+            "failed to get GRPC prepare env response status for requestId(${prepareEnvInput.requestId})"
         }
 
         val remoteScriptExecutionOutput = grpResponse.asJavaData()
-        log.debug("Received prepare env response from command server for requestId($prepareEnvInput.requestId)")
+        log.debug("Received prepare env response from command server for requestId(${prepareEnvInput.requestId})")
 
         return remoteScriptExecutionOutput
     }
 
-    override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput)
-            : RemoteScriptExecutionOutput {
-
-        val grpResponse = commandExecutorServiceGrpc.executeCommand(remoteExecutionInput.asGrpcData()).get()
+    override suspend fun executeCommand(remoteExecutionInput: RemoteScriptExecutionInput): RemoteScriptExecutionOutput {
+        val grpResponse =
+            commandExecutorServiceGrpc
+                .withDeadlineAfter(remoteExecutionInput.timeOut * 1000, TimeUnit.MILLISECONDS)
+                .executeCommand(remoteExecutionInput.asGrpcData())
 
         checkNotNull(grpResponse.status) {
-            "failed to get GRPC response status for requestId($remoteExecutionInput.requestId)"
+            "failed to get GRPC response status for requestId(${remoteExecutionInput.requestId})"
         }
 
-        val remoteScriptExecutionOutput = grpResponse.asJavaData()
-        log.debug("Received response from command server for requestId($remoteExecutionInput.requestId)")
-
-        return remoteScriptExecutionOutput
+        log.debug("Received response from command server for requestId(${remoteExecutionInput.requestId})")
+        return grpResponse.asJavaData()
     }
 
     override suspend fun close() {
         channel?.shutdownNow()
     }
 
-
     fun PrepareRemoteEnvInput.asGrpcData(): PrepareEnvInput {
         val correlationId = this.correlationId ?: this.requestId
 
+        val packageList = mutableListOf<Packages>()
+
+        this.packages.toList().forEach {
+            val pckage = Packages.newBuilder()
+            JsonFormat.parser().merge(it.toString(), pckage)
+            packageList.add(pckage.build())
+        }
+
         return PrepareEnvInput.newBuilder()
             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
             .setRequestId(this.requestId)
+            .setSubRequestId(this.subRequestId)
+            .setOriginatorId(this.originatorId)
             .setCorrelationId(correlationId)
-            .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
             .setTimeOut(this.timeOut.toInt())
-            .addAllPackages(this.packages)
+            .addAllPackages(packageList)
             .setProperties(this.properties.asGrpcData())
             .build()
     }
@@ -116,9 +143,10 @@ class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyServi
         val correlationId = this.correlationId ?: this.requestId
         return ExecutionInput.newBuilder()
             .setRequestId(this.requestId)
+            .setSubRequestId(this.subRequestId)
+            .setOriginatorId(this.originatorId)
             .setCorrelationId(correlationId)
             .setIdentifiers(this.remoteIdentifier!!.asGrpcData())
-            .setScriptType(ScriptType.valueOf(this.remoteScriptType.name))
             .setCommand(this.command)
             .setTimeOut(this.timeOut.toInt())
             .setProperties(this.properties.asGrpcData())
@@ -142,9 +170,9 @@ class GrpcRemoteScriptExecutionService(private val bluePrintGrpcLibPropertyServi
     fun ExecutionOutput.asJavaData(): RemoteScriptExecutionOutput {
         return RemoteScriptExecutionOutput(
             requestId = this.requestId,
-            response = this.response,
-            status = StatusType.valueOf(this.status.name)
+            response = this.responseList,
+            status = StatusType.valueOf(this.status.name),
+            payload = payload.jsonAsJsonType()
         )
     }
-
-}
\ No newline at end of file
+}