1#!/usr/bin/env python
2
3r"""
4PLDM functions.
5"""
6
7import re
8import var_funcs as vf
9import func_args as fa
10import bmc_ssh_utils as bsu
11import json
12
13
14def pldmtool(option_string, **bsu_options):
15    r"""
16    Run pldmtool on the BMC with the caller's option string and return the result.
17
18    Example:
19
20    ${pldm_results}=  Pldmtool  base GetPLDMTypes
21    Rprint Vars  pldm_results
22
23    pldm_results:
24      pldmtool base GetPLDMVersion -t 0
25      {
26          "Response": "1.0.0"
27      }
28
29     pldmtool base GetTID
30     {
31         "Response": 1
32     }
33
34    Description of argument(s):
35    option_string                   A string of options which are to be processed by the pldmtool command.
36    parse_results                   Parse the pldmtool results and return a dictionary rather than the raw
37                                    pldmtool output.
38    bsu_options                     Options to be passed directly to bmc_execute_command.  See its prolog for
39                                    details.
40    """
41
42    # This allows callers to specify arguments in python style (e.g. print_out=1 vs. print_out=${1}).
43    bsu_options = fa.args_to_objects(bsu_options)
44
45    stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
46    return json.loads(stdout)
47
48
49def GenerateBIOSAttrHandleValueDict(attr_val_table_data):
50    """
51    From pldmtool bios GetBIOSTable of AttributeValueTable generate dictionary of
52    for BIOS attribute and its value.
53
54    Description of argument(s):
55    attr_val_table_data     pldmtool output from GetBIOSTable table type AttributeValueTable
56                             e.g.
57                            [{
58                                  "AttributeHandle": 20,
59                                  "AttributeNameHandle": "23(pvm-pcie-error-inject)",
60                                  "AttributeType": "BIOSEnumeration",
61                                  "NumberOfPossibleValues": 2,
62                                  "PossibleValueStringHandle[0]": "3(Disabled)",
63                                  "PossibleValueStringHandle[1]": "4(Enabled)",
64                                  "NumberOfDefaultValues": 1,
65                                  "DefaultValueStringHandleIndex[0]": 1,
66                                  "StringHandle": "4(Enabled)"
67                             },
68                             {
69                                  "AttributeHandle": 26,
70                                  "AttributeNameHandle": "28(pvm_fw_boot_side)",
71                                  "AttributeType": "BIOSEnumeration",
72                                  "NumberOfPossibleValues": 2,
73                                  "PossibleValueStringHandle[0]": "11(Perm)",
74                                  "PossibleValueStringHandle[1]": "15(Temp)",
75                                  "NumberOfDefaultValues": 1,
76                                  "DefaultValueStringHandleIndex[0]": 1,
77                                  "StringHandle": "15(Temp)"
78                             }]
79
80    @return                  Dictionary of BIOS attribute and its value.
81                             e.g. {
82                                   'pvm_pcie_error_inject': ['Disabled', 'Enabled'],
83                                   'pvm-fw-boot-side': ['Perm', 'Temp']
84                                  }
85    """
86
87    attr_val_data_dict = {}
88    for item in attr_val_table_data:
89        for attr in item:
90            if (attr == "NumberOfPossibleValues"):
91                value_list = []
92                for i in range(0, int(item[attr])):
93                    attr_values = item["PossibleValueStringHandle[" + str(i) + "]"]
94                    value = re.search(r'\((.*?)\)', attr_values).group(1)
95                    if value:
96                        # Example:
97                        # value = '"Power Off"'
98                        if ' ' in value:
99                            value = '"' + value + '"'
100                        value_list.append(value)
101                    else:
102                        value_list.append('')
103
104                attr_handle = re.findall(r'\(.*?\)', item["AttributeNameHandle"])
105                attr_val_data_dict[attr_handle[0][1:-1]] = value_list
106    return attr_val_data_dict
107