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
11
12
13def pldmtool(option_string, parse_results=1, **bsu_options):
14    r"""
15    Run pldmtool on the BMC with the caller's option string and return the result.
16
17    Example:
18
19    ${pldm_results}=  Pldmtool  base GetPLDMTypes
20    Rprint Vars  pldm_results
21
22    pldm_results:
23      [supported_types]:
24        [raw]:
25          [0]:                                        0
26          [1]:                                        2
27          [2]:                                        3
28        [text]:
29          [0]:                                        base
30          [1]:                                        platform
31          [2]:                                        bios
32
33    Description of argument(s):
34    option_string                   A string of options which are to be processed by the pldmtool command.
35    parse_results                   Parse the pldmtool results and return a dictionary rather than the raw
36                                    pldmtool output.
37    bsu_options                     Options to be passed directly to bmc_execute_command.  See its prolog for
38                                    details.
39    """
40
41    # This allows callers to specify arguments in python style (e.g. print_out=1 vs. print_out=${1}).
42    bsu_options = fa.args_to_objects(bsu_options)
43
44    stdout, stderr, rc = bsu.bmc_execute_command('pldmtool ' + option_string, **bsu_options)
45
46    if parse_results:
47        result = vf.key_value_outbuf_to_dict(stdout)
48        if 'supported_types' in result:
49            # 'supported types' begins like this:
50            # 0(base) 2(platform) 3(bios)
51            # Parsing it to look like it does in the example above.
52            supported_types = {'raw': [], 'text': []}
53            for entry in result['supported_types'].split(" "):
54                record = entry.split("(")
55                supported_types['raw'].append(record[0])
56                supported_types['text'].append(record[1].rstrip(")"))
57            result['supported_types'] = supported_types
58
59        elif 'supported_commands' in result:
60            commands = result['supported_commands'].split(":")[0].split(" ")
61            return commands
62
63        elif 'yyyy-mm-dd_hh' in result:
64            # Date & Time :
65            # YYYY-MM-DD HH:MM:SS - 2020-02-24 06:44:16
66            return result['yyyy-mm-dd_hh'].split(' - ')[1]
67
68        # Simplfying dict output for GetPDR with type PDREntityAssociation.
69        # Example :
70
71        # pldmtool platform GetPDR -d 10
72        # Entity Association
73        # nextRecordHandle: 0
74        # responseCount: 56
75        # recordHandle: 10
76        # PDRHeaderVersion: 1
77        # PDRType: 15
78        # recordChangeNumber: 0
79        # dataLength: 46
80        # containerID: 1
81        # associationType: Physical
82        # containerEntityType: System Board
83        # containerEntityInstanceNumber: 1
84        # containerEntityContainerID: 0
85        # containedEntityCount: 6
86        # containedEntityType[1]: Chassis front panel board (control panel)
87        # containedEntityInstanceNumber[1]: 1
88        # containedEntityContainerID[1]: 1
89        # containedEntityType[2]: Chassis front panel board (control panel)
90        # containedEntityInstanceNumber[2]: 2
91        # containedEntityContainerID[2]: 1
92        elif 'containerentitycontainerid' in result:
93            dict_data1, dict_data2 = vf.split_dict_on_key('containerentitycontainerid', result)
94            return dict_data1
95
96        # Collect bios strings from bios string table in to list.
97        # Example output for pldmtool GetBIOSTable --type stringTable
98        # PLDM StringTable:
99        # BIOSStringHandle : BIOSString
100        # 0 : Allowed
101        # 1 : Disabled
102        # 2 : Enabled
103        elif 'pldm_stringtable' in result:
104            result.pop('pldm_stringtable')
105            result.pop('biosstringhandle')
106            bios_string_list = []
107            for data in result:
108                bios_string_list.append(result[data])
109            # Example for bios_string_list:
110            # bios_string_list = ['Allowed', 'Disabled', 'Enabled']
111            return bios_string_list
112
113        # Check if parameter pldm_attributetable/pldm_attributevaluetable present for
114        # pldmtool GetBIOSTable --type AttributeTable/AttributeValueTable.
115        # Note: Output for AttributeTable/AttributeValueTable is huge and verification of
116        #       table content is not available.
117        elif 'pldm_attributetable' in result:
118            result['pldm_attributetable'] = True
119            return result
120        elif 'pldm_attributevaluetable' in result:
121            result['pldm_attributevaluetable'] = True
122            return result
123
124        return result
125
126    return stdout
127