class OpTestSwitchEndianSyscall():
    ## Initialize this object
    #  @param i_bmcIP The IP address of the BMC
    #  @param i_bmcUser The userid to log into the BMC with
    #  @param i_bmcPasswd The password of the userid to log into the BMC with
    #  @param i_bmcUserIpmi The userid to issue the BMC IPMI commands with
    #  @param i_bmcPasswdIpmi The password of BMC IPMI userid
    #  @param i_ffdcDir Optional param to indicate where to write FFDC
    #
    # "Only required for inband tests" else Default = None
    # @param i_lparIP The IP address of the LPAR
    # @param i_lparuser The userid to log into the LPAR
    # @param i_lparPasswd The password of the userid to log into the LPAR with
    #
    def __init__(self, i_bmcIP, i_bmcUser, i_bmcPasswd,
                 i_bmcUserIpmi, i_bmcPasswdIpmi, i_ffdcDir=None, i_lparip=None,
                 i_lparuser=None, i_lparPasswd=None):
        self.cv_BMC = OpTestBMC(i_bmcIP, i_bmcUser, i_bmcPasswd, i_ffdcDir)
        self.cv_IPMI = OpTestIPMI(i_bmcIP, i_bmcUserIpmi, i_bmcPasswdIpmi,
                                  i_ffdcDir)
        self.cv_LPAR = OpTestLpar(i_lparip, i_lparuser, i_lparPasswd,i_bmcIP)
        self.util = OpTestUtil()

    ##
    # @brief  If git and gcc commands are availble on lpar, this function will clone linux
    #         git repository and check for switch_endian_test directory and make
    #         the required files. And finally execute bin file switch_endian_test.
    #
    # @return BMC_CONST.FW_SUCCESS-success or BMC_CONST.FW_FAILED-fail
    #
    def testSwitchEndianSysCall(self):

        # Get OS level
        self.cv_LPAR.lpar_get_OS_Level()

        # Check whether git and gcc commands are available on lpar
        self.cv_LPAR.lpar_check_command("git")
        self.cv_LPAR.lpar_check_command("gcc")

        # Clone latest linux git repository into l_dir
        l_dir = "/tmp/linux"
        self.cv_LPAR.lpar_clone_linux_source(l_dir)

        # Check for switch_endian test directory.
        self.check_dir_exists(l_dir)

        # make the required files
        self.make_test(l_dir)

        # Run the switch_endian sys call test once
        l_rc = self.run_once(l_dir)
        if int(l_rc) == 1:
            print "Switch endian sys call test got succesful"
            return BMC_CONST.FW_SUCCESS
        else:
            print "Switch endian sys call test failed"
            return BMC_CONST.FW_FAILED

    ##
    # @brief  It will check for existence of switch_endian directory in the cloned repository
    #
    # @param i_dir @type string: linux source directory
    #
    # @return 1-success or raise OpTestError
    #
    def check_dir_exists(self, i_dir):
        l_dir = '%s/tools/testing/selftests/powerpc/switch_endian' % i_dir
        l_cmd = "test -d %s; echo $?" % l_dir
        print l_cmd
        l_res = self.cv_LPAR.lpar_run_command(l_cmd)
        print l_res
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Switch endian test directory exists"
            return 1
        else:
            l_msg = "Switch endian directory is not present"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief  It will prepare for executable bin files using make command
    #         At the end it will check for bin file switch_endian_test and
    #         will throw an exception in case of missing bin file after make
    #
    # @param i_dir @type string: linux source directory
    #
    # @return 1-success or raise OpTestError
    #
    def make_test(self, i_dir):
        l_cmd = "cd %s/tools/testing/selftests/powerpc/switch_endian;\
                 make;" % i_dir
        print l_cmd
        l_res = self.cv_LPAR.lpar_run_command(l_cmd)
        l_cmd = "test -f %s/tools/testing/selftests/powerpc/switch_endian/switch_endian_test; echo $?" % i_dir
        l_res = self.cv_LPAR.lpar_run_command(l_cmd)
        l_res = l_res.replace("\r\n", "")
        if int(l_res) == 0:
            print "Executable binary switch_endian_test is available"
            return 1
        else:
            l_msg = "Switch_endian_test bin file is not present after make"
            print l_msg
            raise OpTestError(l_msg)

    ##
    # @brief This function will run executable file switch_endian_test and
    #        check for switch_endian() sys call functionality
    #
    # @param i_dir @type string: linux source directory
    #
    # @return 1-success ; 0-fail
    #
    def run_once(self, i_dir):
        l_cmd = "cd %s/tools/testing/selftests/powerpc/switch_endian/;\
                 ./switch_endian_test" % i_dir
        print l_cmd
        l_res = self.cv_LPAR.lpar_run_command(l_cmd)
        if (l_res.__contains__('success: switch_endian_test')):
            return 1
        else:
            return 0