Changes to FFDC to support ffdc_dir_path and ffdc_prefix as parms.

lib/openbmc_ffdc.py
  - New py counterpart to openbmc_ffdc.robot.  Converted ffdc keyword to
    python.  I did this as an effort to reduce dependencies on global
    variables like FFDC_DIR_PATH, FFDC_LOG_PATH, etc and to support
    ffdc_dir_path and ffdc_prefix as parms to the keyword.
lib/openbmc_ffdc.robot:
  - Added line to bring in new lib/openbmc_ffdc.py.
  - Removed ROBOT version of FFDC keyword.

Change-Id: Ic82255f4fbba3b0e6e8eb7faa942c948ce6a522d
Signed-off-by: Michael Walsh <micwalsh@us.ibm.com>
diff --git a/lib/obmc_boot_test.py b/lib/obmc_boot_test.py
index 72c0499..24be494 100755
--- a/lib/obmc_boot_test.py
+++ b/lib/obmc_boot_test.py
@@ -77,8 +77,8 @@
     time_string = time.strftime("%y%m%d.%H%M%S.", loc_time)
 
     openbmc_nickname = BuiltIn().get_variable_value("${openbmc_nickname}")
+    openbmc_host = BuiltIn().get_variable_value("${openbmc_host}")
     if openbmc_nickname == "":
-        openbmc_host = BuiltIn().get_variable_value("${openbmc_host}")
         ffdc_prefix = openbmc_host
     else:
         ffdc_prefix = openbmc_nickname
diff --git a/lib/openbmc_ffdc.py b/lib/openbmc_ffdc.py
new file mode 100644
index 0000000..47ddb19
--- /dev/null
+++ b/lib/openbmc_ffdc.py
@@ -0,0 +1,136 @@
+#!/usr/bin/env python
+
+r"""
+This module is the python counterpart to openbmc_ffdc.robot..
+"""
+
+import os
+
+import gen_robot_print as grp
+import gen_valid as gv
+
+from robot.libraries.BuiltIn import BuiltIn
+
+
+###############################################################################
+def ffdc(ffdc_dir_path=None,
+         ffdc_prefix=None):
+
+    r"""
+    Gather First Failure Data Capture (FFDC).
+
+    This includes:
+    - Set global FFDC_TIME.
+    - Create FFDC work space directory.
+    - Write test info details.
+    - Call BMC methods to write/collect FFDC data.
+
+    Description of arguments:
+    ffdc_dir_path  The dir path where FFDC data should be put.
+    ffdc_prefix    The prefix to be given to each FFDC file name generated.
+    """
+
+    grp.rprint_timen("Collecting FFDC.")
+
+    # Note: Several subordinate functions like 'Get Test Dir and Name' and
+    # 'Header Message' expect global variable FFDC_TIME to be set.
+    cmd_buf = ["Get Current Time Stamp"]
+    grp.rpissuing_keyword(cmd_buf)
+    FFDC_TIME = BuiltIn().run_keyword(*cmd_buf)
+    BuiltIn().set_global_variable("${FFDC_TIME}", FFDC_TIME)
+
+    # Get default values for arguments.
+    ffdc_dir_path, ffdc_prefix = set_ffdc_defaults(ffdc_dir_path, ffdc_prefix)
+    grp.rprint_var(ffdc_dir_path)
+    grp.rprint_var(ffdc_prefix)
+
+    # LOG_PREFIX is used by subordinate functions.
+    LOG_PREFIX = ffdc_dir_path + ffdc_prefix
+    BuiltIn().set_global_variable("${LOG_PREFIX}", LOG_PREFIX)
+
+    cmd_buf = ["Create Directory", ffdc_dir_path]
+    grp.rpissuing_keyword(cmd_buf)
+    status, output = BuiltIn().run_keyword_and_ignore_error(*cmd_buf)
+    if status != "PASS":
+        error_message = grp.sprint_error_report("Create Directory failed with the following error:\n" + output)
+        BuiltIn().fail(error_message)
+
+    # FFDC_FILE_PATH is used by Header Message.
+    FFDC_FILE_PATH = ffdc_dir_path + ffdc_prefix + "BMC_general.txt"
+    BuiltIn().set_global_variable("${FFDC_FILE_PATH}", FFDC_FILE_PATH)
+
+    cmd_buf = ["Header Message"]
+    grp.rpissuing_keyword(cmd_buf)
+    BuiltIn().run_keyword(*cmd_buf)
+
+    cmd_buf = ["Call FFDC Methods"]
+    grp.rpissuing_keyword(cmd_buf)
+    BuiltIn().run_keyword(*cmd_buf)
+
+    grp.rprint_timen("Finished collecting FFDC.")
+
+###############################################################################
+
+
+###############################################################################
+def set_ffdc_defaults(ffdc_dir_path=None,
+                      ffdc_prefix=None):
+
+    r"""
+    Set a default value for ffdc_dir_path and ffdc_prefix if they don't
+    already have values.  Return both values.
+
+    Description of arguments:
+    ffdc_dir_path  The dir path where FFDC data should be put.
+    ffdc_prefix    The prefix to be given to each FFDC file name generated.
+
+    NOTE: If global variable ffdc_dir_path_style is set to ${1}, this function
+    will create default values in a newer way.  Otherwise, its behavior
+    will remain unchanged.
+    """
+
+    ffdc_dir_path_style = BuiltIn().get_variable_value("${ffdc_dir_path_style}")
+
+    if ffdc_dir_path is None:
+        if ffdc_dir_path_style:
+            try:
+                ffdc_dir_path = os.environ['FFDC_DIR_PATH']
+            except KeyError:
+                ffdc_dir_path = os.path.dirname(
+                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
+        else:
+            FFDC_LOG_PATH = BuiltIn().get_variable_value("${FFDC_LOG_PATH}")
+            if FFDC_LOG_PATH is None:
+                FFDC_LOG_PATH = ""
+            if FFDC_LOG_PATH == "":
+                FFDC_LOG_PATH = os.path.dirname(
+                    BuiltIn().get_variable_value("${LOG_FILE}")) + "/"
+            error_message = gv.svalid_value(FFDC_LOG_PATH,
+                                            var_name="FFDC_LOG_PATH")
+            if error_message != "":
+                error_message = grp.sprint_error_report(error_message)
+                BuiltIn().fail(error_message)
+            FFDC_LOG_PATH = os.path.normpath(FFDC_LOG_PATH) + os.sep
+
+            cmd_buf = ["Get Test Dir and Name"]
+            grp.rpissuing_keyword(cmd_buf)
+            suitename, testname = BuiltIn().run_keyword(*cmd_buf)
+
+            ffdc_dir_path = FFDC_LOG_PATH + suitename + "/" + testname + "/"
+
+    # Add trailing slash.
+    ffdc_dir_path = os.path.normpath(ffdc_dir_path) + os.sep
+
+    if ffdc_prefix is None:
+        FFDC_TIME = BuiltIn().get_variable_value("${FFDC_TIME}")
+        if ffdc_prefix is None:
+            if ffdc_dir_path_style:
+                OPENBMC_HOST = BuiltIn().get_variable_value("${OPENBMC_HOST}")
+                ffdc_prefix = OPENBMC_HOST + "." + FFDC_TIME[2:8] + "." +\
+                    FFDC_TIME[8:14] + "."
+            else:
+                ffdc_prefix = FFDC_TIME + "_"
+
+    return ffdc_dir_path, ffdc_prefix
+
+###############################################################################
diff --git a/lib/openbmc_ffdc.robot b/lib/openbmc_ffdc.robot
index d1b67f5..cf90cc4 100755
--- a/lib/openbmc_ffdc.robot
+++ b/lib/openbmc_ffdc.robot
@@ -30,6 +30,7 @@
 
 Resource           openbmc_ffdc_methods.robot
 Resource           openbmc_ffdc_utils.robot
+Library            openbmc_ffdc.py
 
 *** Keywords ***
 
@@ -46,34 +47,3 @@
     ...    FFDC
 
     Log Test Case Status
-
-
-FFDC
-    [Documentation]   Main entry point to gather logs on Test case failure
-    ...               1. Set global FFDC time reference for a failure
-    ...               2. Create FFDC work space directory
-    ...               3. Write test info details
-    ...               4. Calls BMC methods to write/collect FFDC data
-
-    ${cur_time}=      Get Current Time Stamp
-    Set Global Variable    ${FFDC_TIME}     ${cur_time}
-    Log To Console    ${\n}FFDC Collection Started \t: ${cur_time}
-
-    # Log directory setup
-    ${suitename}   ${testname}=    Get Test Dir and Name
-
-    Set Global Variable
-    ...   ${FFDC_DIR_PATH}  ${FFDC_LOG_PATH}${suitename}${/}${testname}
-
-    ${prefix}=   Catenate  SEPARATOR=   ${FFDC_DIR_PATH}${/}   ${FFDC_TIME}_
-    Set Global Variable    ${LOG_PREFIX}    ${prefix}
-
-    Create FFDC Directory
-    Header Message
-
-    # -- FFDC processing entry point --
-    Call FFDC Methods
-
-    ${cur_time}=       Get Current Time Stamp
-    Log To Console     FFDC Collection Completed \t: ${cur_time}
-    Log                ${\n}${FFDC_DIR_PATH}