New Device Write command keyword

Change-Id: Ie9d08348894578a1e7bb80b482e08e972bbfda9b
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/bmc_ssh_utils.py b/lib/bmc_ssh_utils.py
index c5fdc58..8729fd5 100755
--- a/lib/bmc_ssh_utils.py
+++ b/lib/bmc_ssh_utils.py
@@ -191,3 +191,65 @@
     return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
                                    print_out, print_err, ignore_err, fork,
                                    quiet, test_mode)
+
+
+def device_write(cmd_buf,
+                 print_out=0,
+                 quiet=None,
+                 test_mode=None):
+    r"""
+    Write the given command in a device SSH session and return the stdout,
+    stderr and the return code.
+
+    This function is useful for writing to a switch.
+
+    This function will obtain the global values for DEVICE_HOST,
+    DEVICE_USERNAME, etc.
+
+    Description of arguments:
+    cmd_buf                         The command string to be run in an SSH
+                                    session.
+    print_out                       If this is set, this function will print
+                                    the stdout/stderr generated by the shell
+                                    command.
+    print_err                       If show_err is set, this function will
+                                    print a standardized error report if the
+                                    shell command returns non-zero.
+    ignore_err                      Indicates that errors encountered on the
+                                    sshlib.execute_command are to be ignored.
+    fork                            Indicates that sshlib.start is to be used
+                                    rather than sshlib.execute_command.
+    quiet                           Indicates whether this function should run
+                                    the pissuing() function prints an
+                                    "Issuing: <cmd string>" to stdout.  This
+                                    defaults to the global quiet value.
+    test_mode                       If test_mode is set, this function will
+                                    not actually run the command.  This
+                                    defaults to the global test_mode value.
+    """
+
+    # Get global DEVICE variable values.
+    device_host = BuiltIn().get_variable_value("${DEVICE_HOST}", default="")
+    device_username = BuiltIn().get_variable_value("${DEVICE_USERNAME}",
+                                                   default="")
+    device_password = BuiltIn().get_variable_value("${DEVICE_PASSWORD}",
+                                                   default="")
+    device_port = BuiltIn().get_variable_value("${DEVICE_PORT}",
+                                               default="22")
+
+    if not gv.valid_value(device_host):
+        return "", "", 1
+    if not gv.valid_value(device_username):
+        return "", "", 1
+    if not gv.valid_value(device_password):
+        return "", "", 1
+    if not gv.valid_value(device_port):
+        return "", "", 1
+
+    open_connection_args = {'host': device_host, 'alias': 'device_connection',
+                            'port': device_port}
+    login_args = {'username': device_username, 'password': device_password}
+
+    return grs.execute_ssh_command(cmd_buf, open_connection_args, login_args,
+                                   print_out, print_err=0, ignore_err=1,
+                                   fork=0, quiet=quiet, test_mode=test_mode)
diff --git a/lib/gen_robot_ssh.py b/lib/gen_robot_ssh.py
index cdd0b4f..5a4dbe1 100755
--- a/lib/gen_robot_ssh.py
+++ b/lib/gen_robot_ssh.py
@@ -168,6 +168,12 @@
     Likewise, if the caller has not yet logged in to the connection, this
     function will do the login.
 
+    NOTE: There is special handling when open_connection_args['alias'] equals
+    "device_connection".
+    - A write, rather than an execute_command, is done.
+    - Only stdout is returned (no stderr or rc).
+    - print_err, ignore_err and fork are not supported.
+
     Description of arguments:
     cmd_buf                         The command string to be run in an SSH
                                     session.
@@ -243,10 +249,16 @@
             if fork:
                 sshlib.start_command(cmd_buf)
             else:
-                stdout, stderr, rc = sshlib.execute_command(cmd_buf,
-                                                            return_stdout=True,
-                                                            return_stderr=True,
-                                                            return_rc=True)
+                if open_connection_args['alias'] == "device_connection":
+                    stdout = sshlib.write(cmd_buf)
+                    stderr = ""
+                    rc = 0
+                else:
+                    stdout, stderr, rc = \
+                        sshlib.execute_command(cmd_buf,
+                                               return_stdout=True,
+                                               return_stderr=True,
+                                               return_rc=True)
         except Exception as execute_exception:
             except_type, except_value, except_traceback = sys.exc_info()
             gp.lprint_var(except_type)
@@ -304,4 +316,6 @@
                                   "\n")
         BuiltIn().should_be_equal(rc, 0, message)
 
+    if open_connection_args['alias'] == "device_connection":
+        return stdout
     return stdout, stderr, rc