cmd-exec 0-return is success; env.prep.fix. 04/100704/3
authorOleg Mitsura <oleg.mitsura@amdocs.com>
Thu, 23 Jan 2020 17:39:50 +0000 (12:39 -0500)
committerOleg Mitsura <oleg.mitsura@amdocs.com>
Thu, 23 Jan 2020 18:00:15 +0000 (13:00 -0500)
Issue-ID: CCSDK-1855

This is cherry-pick of https://gerrit.onap.org/r/c/ccsdk/cds/+/98609
rev2. pasted wrong file, modified back...
rev3. added catch-all.
Change-Id: I30eccc3b18b428970bfa873b9f0ce359a1f5e97d
Signed-off-by: Oleg Mitsura <oleg.mitsura@amdocs.com>
ms/blueprintsprocessor/functions/python-executor/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/python/executor/ComponentRemotePythonExecutor.kt
ms/command-executor/src/main/python/command_executor_server.py

index 3250cd3..6ad1e1f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright © 2019 IBM.
+ *  Copyright Â© 2019 IBM.
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -114,46 +114,51 @@ open class ComponentRemotePythonExecutor(private val remoteScriptExecutionServic
                 )
                 val prepareEnvOutput = remoteScriptExecutionService.prepareEnv(prepareEnvInput)
                 log.info("$ATTRIBUTE_PREPARE_ENV_LOG - ${prepareEnvOutput.response}")
-                val logs = JacksonUtils.jsonNodeFromObject(prepareEnvOutput.response)
-                setAttribute(ATTRIBUTE_PREPARE_ENV_LOG, logs)
-                setAttribute(ATTRIBUTE_EXEC_CMD_LOG, "N/A".asJsonPrimitive())
+                val logs = prepareEnvOutput.response
+                val logsEnv = logs.toString().asJsonPrimitive()
+                setAttribute(ATTRIBUTE_PREPARE_ENV_LOG, logsEnv)
 
                 if (prepareEnvOutput.status != StatusType.SUCCESS) {
-                    setNodeOutputErrors(prepareEnvOutput.status.name, logs)
+                    setAttribute(ATTRIBUTE_EXEC_CMD_LOG, "N/A".asJsonPrimitive())
+                    setNodeOutputErrors(prepareEnvOutput.status.name, logsEnv)
                 } else {
-                    setNodeOutputProperties(prepareEnvOutput.status.name.asJsonPrimitive(), logs, "".asJsonPrimitive())
+                    setNodeOutputProperties(prepareEnvOutput.status.name.asJsonPrimitive(), logsEnv, "".asJsonPrimitive())
                 }
             }
 
-            // Populate command execution properties and pass it to the remote server
-            val properties = dynamicProperties?.returnNullIfMissing()?.rootFieldsToMap() ?: hashMapOf()
+            // if Env preparation was successful, then proceed with command execution in this Env
+            if (bluePrintRuntimeService.getBluePrintError().errors.isEmpty()) {
+                // Populate command execution properties and pass it to the remote server
+                val properties = dynamicProperties?.returnNullIfMissing()?.rootFieldsToMap() ?: hashMapOf()
 
-            val remoteExecutionInput = RemoteScriptExecutionInput(
-                requestId = processId,
-                remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
-                command = scriptCommand,
-                properties = properties,
-                timeOut = timeout.toLong())
+                val remoteExecutionInput = RemoteScriptExecutionInput(
+                    requestId = processId,
+                    remoteIdentifier = RemoteIdentifier(blueprintName = blueprintName, blueprintVersion = blueprintVersion),
+                    command = scriptCommand,
+                    properties = properties,
+                    timeOut = timeout.toLong())
 
 
-            val remoteExecutionOutputDeferred = GlobalScope.async {
-                remoteScriptExecutionService.executeCommand(remoteExecutionInput)
-            }
+                val remoteExecutionOutputDeferred = GlobalScope.async {
+                    remoteScriptExecutionService.executeCommand(remoteExecutionInput)
+                }
 
-            val remoteExecutionOutput = withTimeout(timeout * 1000L) {
-                remoteExecutionOutputDeferred.await()
-            }
+                val remoteExecutionOutput = withTimeout(timeout * 1000L) {
+                    remoteExecutionOutputDeferred.await()
+                }
 
-            checkNotNull(remoteExecutionOutput) {
-                "Error: Request-id $processId did not return a restul from remote command execution."
-            }
+                checkNotNull(remoteExecutionOutput) {
+                    "Error: Request-id $processId did not return a restul from remote command execution."
+                }
 
-            val logs = JacksonUtils.jsonNodeFromObject(remoteExecutionOutput.response)
-            if (remoteExecutionOutput.status != StatusType.SUCCESS) {
-                setNodeOutputErrors(remoteExecutionOutput.status.name, logs, remoteExecutionOutput.payload)
-            } else {
-                setNodeOutputProperties(remoteExecutionOutput.status.name.asJsonPrimitive(), logs,
-                    remoteExecutionOutput.payload)
+
+                val logs = JacksonUtils.jsonNodeFromObject(remoteExecutionOutput.response)
+                if (remoteExecutionOutput.status != StatusType.SUCCESS) {
+                    setNodeOutputErrors(remoteExecutionOutput.status.name, logs, remoteExecutionOutput.payload)
+                } else {
+                    setNodeOutputProperties(remoteExecutionOutput.status.name.asJsonPrimitive(), logs,
+                        remoteExecutionOutput.payload)
+                }
             }
 
         } catch (timeoutEx: TimeoutCancellationException) {
@@ -201,9 +206,12 @@ open class ComponentRemotePythonExecutor(private val remoteScriptExecutionServic
      */
     private fun setNodeOutputErrors(status: String, message: JsonNode, artifacts: JsonNode = "".asJsonPrimitive()) {
         setAttribute(ATTRIBUTE_EXEC_CMD_STATUS, status.asJsonPrimitive())
+        log.info("Executor status   : $status")
         setAttribute(ATTRIBUTE_EXEC_CMD_LOG, message)
+        log.info("Executor message  : $message")
         setAttribute(ATTRIBUTE_RESPONSE_DATA, artifacts)
+        log.info("Executor artifacts: $artifacts")
 
-        addError(status, ATTRIBUTE_EXEC_CMD_LOG, message.asText())
+        addError(status, ATTRIBUTE_EXEC_CMD_LOG, message.toString())
     }
 }
index 577c8a0..9f6fb8f 100644 (file)
@@ -53,12 +53,12 @@ class CommandExecutorServer(CommandExecutor_pb2_grpc.CommandExecutorServiceServi
         payload_result = {}
         handler = CommandExecutorHandler(request)
         payload_result = handler.execute_command(request, log_results)
-        if not payload_result["cds_return_code"]:
+        if payload_result["cds_return_code"] != 0:
             self.logger.info("{} - Failed to executeCommand. {}".format(blueprint_id, log_results))
         else:
             self.logger.info("{} - Execution finished successfully.".format(blueprint_id))
 
-        ret = utils.build_response(request, log_results, payload_result, payload_result["cds_return_code"])
+        ret = utils.build_response(request, log_results, payload_result, payload_result["cds_return_code"] == 0)
         self.logger.info("Payload returned %s" % payload_result)
 
-        return ret
\ No newline at end of file
+        return ret