1#!/usr/bin/env python3
2
3r"""
4PEL functions.
5"""
6
7import func_args as fa
8import bmc_ssh_utils as bsu
9import json
10
11
12class peltool_exception(Exception):
13    r"""
14    Base class for peltool related exceptions.
15    """
16    def __init__(self, message):
17        self.message = message
18        super().__init__(self.message)
19
20
21def peltool(option_string, parse_json=True, **bsu_options):
22    r"""
23    Run peltool on the BMC with the caller's option string and return the result.
24
25    Example:
26
27    ${pel_results}=  Peltool  -l
28    Rprint Vars  pel_results
29
30    pel_results:
31      [0x50000031]:
32        [CompID]:                       0x1000
33        [PLID]:                         0x50000031
34        [Subsystem]:                    BMC Firmware
35        [Message]:                      An application had an internal failure
36        [SRC]:                          BD8D1002
37        [Commit Time]:                  02/25/2020  04:51:31
38        [Sev]:                          Unrecoverable Error
39        [CreatorID]:                    BMC
40
41    Description of argument(s):
42    option_string                   A string of options which are to be processed by the peltool command.
43    parse_json                      Indicates that the raw JSON data should parsed into a list of
44                                    dictionaries.
45    bsu_options                     Options to be passed directly to bmc_execute_command. See its prolog for
46                                    details.
47    """
48
49    bsu_options = fa.args_to_objects(bsu_options)
50    out_buf, stderr, rc = bsu.bmc_execute_command('peltool ' + option_string, **bsu_options)
51    if parse_json:
52        try:
53            return json.loads(out_buf)
54        except json.JSONDecodeError:
55            return {}
56    return out_buf
57
58
59def fetch_pelId_For_SRC(src_id):
60    r"""
61    Fetch PEL ID for the input SRC ID
62
63    Description of arguments:
64    src_id  SRC ID (e.g. BC20E504)
65    """
66
67    src_pel_id = []
68    try:
69        pel_data = peltool(" -l")
70        pel_id_list = pel_data.keys()
71        for i in pel_id_list:
72            if pel_data[i]["SRC"] == src_id:
73                src_pel_id.append(i)
74    except Exception as e:
75        raise peltool_exception("Failed to fetch PEL ID for required SRC : " + str(e))
76    return src_pel_id
77
78
79def verify_SRC_details(pel_id, attn_type, target_type, signature_desc,
80                       threshold_limit):
81    r"""
82    Verify SRC details for the given PEL ID based on the required
83    target type, attention type, signature description, threshold limit.
84
85    Description of arguments:
86    pel_id          PEL ID for the required SRC details to verify
87    target_type     Target type (e.g. TYPE_OMIC)
88    attn_type       Attention type (e.g. RECCOVERABLE)
89    signature_desc  Signature description of the error inject
90    threshold_limit Threshold limit (e.g. 1, 5, 32)
91    """
92
93    try:
94        pel_cmd = " -i " + pel_id
95        src_data = peltool(pel_cmd)
96        src_dict = src_data["Primary SRC"]["SRC Details"]
97        usr_data = src_data["User Data 4"]
98        if (src_dict["Attention Type"] != attn_type):
99            raise peltool_exception("Required Attention type " + attn_type + " not found")
100        if target_type not in src_dict["Target Type"]:
101            raise peltool_exception("Required Target type " + target_type + " not found")
102        if signature_desc not in src_dict["Signature"]:
103            raise peltool_exception("Required Signature " + signature_desc + " not found")
104
105        if (int(threshold_limit) != usr_data["Error Threshold"]):
106            raise peltool_exception("Required Threshold limit " + threshold_limit + " not found")
107
108    except Exception as e:
109        raise peltool_exception("Failed to verify SRC details : " + str(e))
110
111    return True
112
113
114def fetch_All_SRC():
115    r"""
116    Fetch all list of SRC IDs from peltool
117    """
118
119    src_id = []
120    try:
121        pel_data = peltool(" -l")
122        pel_id_list = pel_data.keys()
123        for i in pel_id_list:
124            src_id.append(pel_data[i]["SRC"])
125    except Exception as e:
126        raise peltool_exception("Failed to fetch all SRCs : " + str(e))
127    return src_id
128