Generalize execution for protocols and YAML update

Changes:
      - Update YAML file for redfishtool execution
      - Common function for many of the protocol execution
      - Simplify flow and documentation changes.

Tested:
      - default for OPENBMC , AIX and others.
      - -rp option for IPMI, REDFISH,SSH,SCP, SHELL, TELENET

Change-Id: Idceafa25644c1cf547a36a553335a5cbda389ab6
Signed-off-by: George Keishing <gkeishin@in.ibm.com>
diff --git a/ffdc/ffdc_collector.py b/ffdc/ffdc_collector.py
index 53fe9dd..b980425 100644
--- a/ffdc/ffdc_collector.py
+++ b/ffdc/ffdc_collector.py
@@ -147,8 +147,8 @@
 
         run_env_ok = True
 
-        redfishtool_version = self.run_redfishtool('-V').split(' ')[2].strip('\n')
-        ipmitool_version = self.run_ipmitool('ipmitool -V').split(' ')[2]
+        redfishtool_version = self.run_tool_cmd('redfishtool -V').split(' ')[2].strip('\n')
+        ipmitool_version = self.run_tool_cmd('ipmitool -V').split(' ')[2]
 
         self.logger.info("\n\t---- Script host environment ----")
         self.logger.info("\t{:<10}  {:<10}".format('Script hostname', os.uname()[1]))
@@ -318,19 +318,19 @@
 
                 if protocol == 'REDFISH':
                     if protocol in working_protocol_list:
-                        self.protocol_redfish(target_type, k)
+                        self.protocol_execute(protocol, target_type, k)
                     else:
                         self.logger.error("\n\tERROR: REDFISH is not available for %s." % self.hostname)
 
                 if protocol == 'IPMI':
                     if protocol in working_protocol_list:
-                        self.protocol_ipmi(target_type, k)
+                        self.protocol_execute(protocol, target_type, k)
                     else:
                         self.logger.error("\n\tERROR: IPMI is not available for %s." % self.hostname)
 
                 if protocol == 'SHELL':
                     if protocol in working_protocol_list:
-                        self.protocol_shell_script(target_type, k)
+                        self.protocol_execute(protocol, target_type, k)
                     else:
                         self.logger.error("\n\tERROR: can't execute SHELL script")
 
@@ -394,76 +394,32 @@
         for file in telnet_files_saved:
             self.logger.info("\n\t\tSuccessfully save file " + file + ".")
 
-    def protocol_redfish(self,
+    def protocol_execute(self,
+                         protocol,
                          target_type,
                          sub_type):
         r"""
-        Perform actions using Redfish protocol.
+        Perform actions for a given protocol.
 
         Description of argument(s):
+        protocol            Protocol to execute.
         target_type         OS Type of remote host.
         sub_type            Group type of commands.
         """
 
-        self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'REDFISH'))
-        redfish_files_saved = []
-        progress_counter = 0
-        list_of_URL = self.ffdc_actions[target_type][sub_type]['URL']
-        for index, each_url in enumerate(list_of_URL, start=0):
-            redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
-                           + self.hostname + ' -S Always raw GET ' + each_url
-
-            result = self.run_redfishtool(redfish_parm)
-            if result:
-                try:
-                    targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
-                except IndexError:
-                    targ_file = each_url.split('/')[-1]
-                    self.logger.warning(
-                        "\n\t[WARN] Missing filename to store data from redfish URL %s." % each_url)
-                    self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
-
-                targ_file_with_path = (self.ffdc_dir_path
-                                       + self.ffdc_prefix
-                                       + targ_file)
-
-                # Creates a new file
-                with open(targ_file_with_path, 'w') as fp:
-                    fp.write(result)
-                    fp.close
-                    redfish_files_saved.append(targ_file)
-
-            progress_counter += 1
-            self.print_progress(progress_counter)
-
-        self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
-
-        for file in redfish_files_saved:
-            self.logger.info("\n\t\tSuccessfully save file " + file + ".")
-
-    def protocol_ipmi(self,
-                      target_type,
-                      sub_type):
-        r"""
-        Perform actions using ipmitool over LAN protocol.
-
-        Description of argument(s):
-        target_type         OS Type of remote host.
-        sub_type            Group type of commands.
-        """
-
-        self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'IPMI'))
-        ipmi_files_saved = []
+        self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, protocol))
+        executed_files_saved = []
         progress_counter = 0
         list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
         for index, each_cmd in enumerate(list_of_cmd, start=0):
-            result = self.run_ipmitool(each_cmd)
+            result = self.run_tool_cmd(each_cmd)
             if result:
                 try:
                     targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
                 except IndexError:
                     targ_file = each_cmd.split('/')[-1]
-                    self.logger.warning("\n\t[WARN] Missing filename to store data from IPMI %s." % each_cmd)
+                    self.logger.warning(
+                        "\n\t[WARN] Missing filename to store data from %s." % each_cmd)
                     self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
 
                 targ_file_with_path = (self.ffdc_dir_path
@@ -474,14 +430,14 @@
                 with open(targ_file_with_path, 'w') as fp:
                     fp.write(result)
                     fp.close
-                    ipmi_files_saved.append(targ_file)
+                    executed_files_saved.append(targ_file)
 
             progress_counter += 1
             self.print_progress(progress_counter)
 
         self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
 
-        for file in ipmi_files_saved:
+        for file in executed_files_saved:
             self.logger.info("\n\t\tSuccessfully save file " + file + ".")
 
     def collect_and_copy_ffdc(self,
@@ -731,9 +687,9 @@
         Verify remote host has redfish service active
 
         """
-        redfish_parm = '-u ' + self.username + ' -p ' + self.password + ' -r ' \
+        redfish_parm = 'redfishtool -r ' \
                        + self.hostname + ' -S Always raw GET /redfish/v1/'
-        return(self.run_redfishtool(redfish_parm, True))
+        return(self.run_tool_cmd(redfish_parm, True))
 
     def verify_ipmi(self):
         r"""
@@ -747,40 +703,17 @@
             ipmi_parm = 'ipmitool -I lanplus  -P ' \
                 + self.password + ' -H ' + self.hostname + ' power status'
 
-        return(self.run_ipmitool(ipmi_parm, True))
+        return(self.run_tool_cmd(ipmi_parm, True))
 
-    def run_redfishtool(self,
-                        parms_string,
-                        quiet=False):
-        r"""
-        Run CLI redfishtool
-
-        Description of variable:
-        parms_string         redfishtool subcommand and options.
-        quiet                do not print redfishtool error message if True
-        """
-
-        result = subprocess.run(['redfishtool ' + parms_string],
-                                stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE,
-                                shell=True,
-                                universal_newlines=True)
-
-        if result.stderr and not quiet:
-            self.logger.error('\n\tERROR with redfishtool ' + parms_string)
-            self.logger.error('\n\t%s' % result.stderr.split('\n'))
-
-        return result.stdout
-
-    def run_ipmitool(self,
+    def run_tool_cmd(self,
                      parms_string,
                      quiet=False):
         r"""
-        Run CLI IPMI tool.
+        Run CLI standard tool or scripts.
 
         Description of variable:
-        parms_string         ipmitool subcommand and options.
-        quiet                do not print redfishtool error message if True
+        parms_string         tool command options.
+        quiet                do not print tool error message if True
         """
 
         result = subprocess.run([parms_string],
@@ -795,73 +728,6 @@
 
         return result.stdout
 
-    def run_shell_script(self,
-                         parms_string,
-                         quiet=False):
-        r"""
-        Run CLI shell script tool.
-
-        Description of variable:
-        parms_string         script command options.
-        quiet                do not print redfishtool error message if True
-        """
-
-        result = subprocess.run([parms_string],
-                                stdout=subprocess.PIPE,
-                                stderr=subprocess.PIPE,
-                                shell=True,
-                                universal_newlines=True)
-
-        if result.stderr and not quiet:
-            self.logger.error('\n\t\tERROR executing %s' % parms_string)
-            self.logger.error('\t\t' + result.stderr)
-
-        return result.stdout
-
-    def protocol_shell_script(self,
-                              target_type,
-                              sub_type):
-        r"""
-        Perform SHELL script execution locally.
-
-        Description of argument(s):
-        target_type         OS Type of remote host.
-        sub_type            Group type of commands.
-        """
-
-        self.logger.info("\n\t[Run] Executing commands to %s using %s" % (self.hostname, 'SHELL'))
-        shell_files_saved = []
-        progress_counter = 0
-        list_of_cmd = self.get_command_list(self.ffdc_actions[target_type][sub_type])
-        for index, each_cmd in enumerate(list_of_cmd, start=0):
-
-            result = self.run_shell_script(each_cmd)
-            if result:
-                try:
-                    targ_file = self.get_file_list(self.ffdc_actions[target_type][sub_type])[index]
-                except IndexError:
-                    targ_file = each_cmd.split('/')[-1]
-                    self.logger.warning("\n\t[WARN] Missing filename to store data %s." % each_cmd)
-                    self.logger.warning("\t[WARN] Data will be stored in %s." % targ_file)
-
-                targ_file_with_path = (self.ffdc_dir_path
-                                       + self.ffdc_prefix
-                                       + targ_file)
-
-                # Creates a new file
-                with open(targ_file_with_path, 'w') as fp:
-                    fp.write(result)
-                    fp.close
-                    shell_files_saved.append(targ_file)
-
-            progress_counter += 1
-            self.print_progress(progress_counter)
-
-        self.logger.info("\n\t[Run] Commands execution completed.\t\t [OK]")
-
-        for file in shell_files_saved:
-            self.logger.info("\n\t\tSuccessfully save file " + file + ".")
-
     def verify_protocol(self, protocol_list):
         r"""
         Perform protocol working check.
diff --git a/ffdc/ffdc_config.yaml b/ffdc/ffdc_config.yaml
index c9f23b2..d0bc091 100644
--- a/ffdc/ffdc_config.yaml
+++ b/ffdc/ffdc_config.yaml
@@ -101,14 +101,14 @@
     # URLs and Files are one-to-one corresponding.
     # File contains the data returned from 'redfishtool GET URL'
     REDFISH_LOGS:
-        URL:
-            - '/xyz/openbmc_project/software/enumerate'
-            - '/xyz/openbmc_project/state/enumerate'
-            - '/org/open_power/enumerate'
-            - '/redfish/v1/AccountService/Accounts'
-            - '/redfish/v1/Managers/bmc/LogServices/Dump/Entries'
-            - '/redfish/v1/Systems/system/LogServices/Dump/Entries'
-            - '/redfish/v1/Systems/system/LogServices/EventLog/Entries'
+        COMMANDS:
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /xyz/openbmc_project/software/enumerate
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /xyz/openbmc_project/state/enumerate
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /org/open_power/enumerate
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /redfish/v1/AccountService/Accounts
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /redfish/v1/Managers/bmc/LogServices/Dump/Entries
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /redfish/v1/Systems/system/LogServices/Dump/Entries
+            - redfishtool -u ${username} -p ${password} -r ${hostname} -S Always raw GET /redfish/v1/Systems/system/LogServices/EventLog/Entries
         FILES:
             - 'REDFISH_software.json'
             - 'REDFISH_bmc_state.json'