1b29d2e84SChris Austen#!/usr/bin/python -u
2b29d2e84SChris Austenimport sys
3b29d2e84SChris Austenfrom robot.libraries.BuiltIn import BuiltIn
4b29d2e84SChris Austenimport imp
5b29d2e84SChris Austenimport string
6757d80c1SRahul Maheshwariimport random
78b270ecfSMichael Walshimport subprocess
88b270ecfSMichael Walshfrom robot.utils import DotDict
98b270ecfSMichael Walsh
10b29d2e84SChris Austen
11757d80c1SRahul Maheshwaridef random_mac():
12757d80c1SRahul Maheshwari    r"""
13757d80c1SRahul Maheshwari    Return random mac address in the following format.
14757d80c1SRahul Maheshwari    Example: 00:01:6C:80:02:78
15757d80c1SRahul Maheshwari    """
16757d80c1SRahul Maheshwari    return ":".join(map(lambda x: "%02x" % x, (random.randint(0x00, 0xff)
17757d80c1SRahul Maheshwari        for _ in range(6))))
18757d80c1SRahul Maheshwari
19757d80c1SRahul Maheshwaridef random_ip():
20757d80c1SRahul Maheshwari    r"""
21757d80c1SRahul Maheshwari    Return random ip address in the following format.
22757d80c1SRahul Maheshwari    Example: 9.3.128.100
23757d80c1SRahul Maheshwari    """
24757d80c1SRahul Maheshwari    return ".".join(map(str, (random.randint(0, 255)
25757d80c1SRahul Maheshwari        for _ in range(4))))
26b29d2e84SChris Austen
27b29d2e84SChris Austendef get_sensor(module_name, value):
28b29d2e84SChris Austen    m = imp.load_source('module.name', module_name)
29b29d2e84SChris Austen
30b29d2e84SChris Austen    for i in m.ID_LOOKUP['SENSOR']:
31b29d2e84SChris Austen
32b29d2e84SChris Austen        if m.ID_LOOKUP['SENSOR'][i] == value:
33b29d2e84SChris Austen            return i
34b29d2e84SChris Austen
35b29d2e84SChris Austen    return 0xFF
36b29d2e84SChris Austen
37b29d2e84SChris Austen
38b29d2e84SChris Austendef get_inventory_sensor (module_name, value):
39b29d2e84SChris Austen    m = imp.load_source('module.name', module_name)
40b29d2e84SChris Austen
41b29d2e84SChris Austen    value = string.replace(value, m.INVENTORY_ROOT, '<inventory_root>')
42b29d2e84SChris Austen
43b29d2e84SChris Austen    for i in m.ID_LOOKUP['SENSOR']:
44b29d2e84SChris Austen
45b29d2e84SChris Austen        if m.ID_LOOKUP['SENSOR'][i] == value:
46b29d2e84SChris Austen            return i
47b29d2e84SChris Austen
48b29d2e84SChris Austen    return 0xFF
49b29d2e84SChris Austen
50b29d2e84SChris Austen
51b29d2e84SChris Austen################################################################
52b29d2e84SChris Austen#  This will return the URI's of the FRU type
53b29d2e84SChris Austen#
54b29d2e84SChris Austen#  i.e.  get_inventory_list('../data/Palmetto.py')
55b29d2e84SChris Austen#
56b29d2e84SChris Austen#  [/org/openbmc/inventory//system/chassis/motherboard/cpu0/core0,
57b29d2e84SChris Austen#   /org/openbmc/inventory/system/chassis/motherboard/dimm0]
58b29d2e84SChris Austen################################################################
59b29d2e84SChris Austendef get_inventory_list(module_name):
60b29d2e84SChris Austen
61b29d2e84SChris Austen    l = []
62b29d2e84SChris Austen    m = imp.load_source('module.name', module_name)
63b29d2e84SChris Austen
64b29d2e84SChris Austen
65b29d2e84SChris Austen    for i in m.ID_LOOKUP['FRU']:
66b29d2e84SChris Austen        s = m.ID_LOOKUP['FRU'][i]
67b29d2e84SChris Austen        s = s.replace('<inventory_root>',m.INVENTORY_ROOT)
68b29d2e84SChris Austen        l.append(s)
69b29d2e84SChris Austen
70b29d2e84SChris Austen    return l
71b29d2e84SChris Austen
72b29d2e84SChris Austen
73b29d2e84SChris Austen################################################################
74b29d2e84SChris Austen#  This will return the URI's of the FRU type
75b29d2e84SChris Austen#
76b29d2e84SChris Austen#  i.e.  get_inventory_fru_type_list('../data/Barreleye.py', 'CPU')
77b29d2e84SChris Austen#
78b29d2e84SChris Austen#  [/org/openbmc/inventory//system/chassis/motherboard/cpu0,
79b29d2e84SChris Austen#   /org/openbmc/inventory//system/chassis/motherboard/cpu1]
80b29d2e84SChris Austen################################################################
81b29d2e84SChris Austendef  get_inventory_fru_type_list(module_name, fru):
82b29d2e84SChris Austen    l = []
83b29d2e84SChris Austen    m = imp.load_source('module.name', module_name)
84b29d2e84SChris Austen
85b29d2e84SChris Austen    for i in m.FRU_INSTANCES.keys():
86b29d2e84SChris Austen        if m.FRU_INSTANCES[i]['fru_type'] == fru:
87b29d2e84SChris Austen            s = i.replace('<inventory_root>',m.INVENTORY_ROOT)
88b29d2e84SChris Austen            l.append(s)
89b29d2e84SChris Austen
90b29d2e84SChris Austen    return l
91b29d2e84SChris Austen
92b29d2e84SChris Austen
93b29d2e84SChris Austen################################################################
94b29d2e84SChris Austen#  This will return the URI's of the FRU type that contain VPD
95b29d2e84SChris Austen#
96b29d2e84SChris Austen#  i.e.  get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
97b29d2e84SChris Austen#
98b29d2e84SChris Austen#  [/org/openbmc/inventory/system/chassis/motherboard/dimm0,
99b29d2e84SChris Austen#   /org/openbmc/inventory/system/chassis/motherboard/dimm1]
100b29d2e84SChris Austen################################################################
101b29d2e84SChris Austendef  get_vpd_inventory_list(module_name, fru):
102b29d2e84SChris Austen    l = []
103b29d2e84SChris Austen    m = imp.load_source('module.name', module_name)
104b29d2e84SChris Austen
105b29d2e84SChris Austen    for i in m.ID_LOOKUP['FRU_STR']:
106b29d2e84SChris Austen        x = m.ID_LOOKUP['FRU_STR'][i]
107b29d2e84SChris Austen
108b29d2e84SChris Austen        if m.FRU_INSTANCES[x]['fru_type'] == fru:
109b29d2e84SChris Austen            s = x.replace('<inventory_root>',m.INVENTORY_ROOT)
110b29d2e84SChris Austen            l.append(s)
111b29d2e84SChris Austen
112b29d2e84SChris Austen    return l
113b29d2e84SChris Austen
114b29d2e84SChris Austen
115b29d2e84SChris Austendef call_keyword(keyword):
116b29d2e84SChris Austen    return BuiltIn().run_keyword(keyword)
117b29d2e84SChris Austen
118b29d2e84SChris Austen
119b29d2e84SChris Austendef main():
120b29d2e84SChris Austen    print get_vpd_inventory_list('../data/Palmetto.py', 'DIMM')
121b29d2e84SChris Austen
122b29d2e84SChris Austen
123b29d2e84SChris Austenif __name__ == "__main__":
124b29d2e84SChris Austen   main()
1258b270ecfSMichael Walsh
1268b270ecfSMichael Walsh
1278b270ecfSMichael Walsh###############################################################################
1288b270ecfSMichael Walshdef get_mtr_report(host=""):
1298b270ecfSMichael Walsh
1308b270ecfSMichael Walsh    r"""
1318b270ecfSMichael Walsh    Get an mtr report and return it as a dictionary of dictionaries.
1328b270ecfSMichael Walsh
1338b270ecfSMichael Walsh    The key for the top level dictionary will be the host DNS name.  The key
1348b270ecfSMichael Walsh    for the next level dictionary will be the field of a given row of the
1358b270ecfSMichael Walsh    report.
1368b270ecfSMichael Walsh
1378b270ecfSMichael Walsh    Example result:
1388b270ecfSMichael Walsh
1398b270ecfSMichael Walsh    report:
140fb4b1256SGeorge Keishing      report[host_dummy-dnsname.com]:
141fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][row_num]:  1
142fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][host]:     host_dummy-dnsname.com
143fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][loss]:     0.0
144fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][snt]:      10
145fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][last]:     0.2
146fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][avg]:      3.5
147fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][best]:     0.2
148fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][wrst]:     32.5
149fb4b1256SGeorge Keishing        report[host_dummy-dnsname.com][stdev]:    10.2
150fb4b1256SGeorge Keishing      report[bmc-dummy-dnsname.com]:
151fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][row_num]:     2
152fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][host]:        bmc-dummy-dnsname.com
153fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][loss]:        0.0
154fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][snt]:         10
155fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][last]:        0.5
156fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][avg]:         0.5
157fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][best]:        0.5
158fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][wrst]:        0.5
159fb4b1256SGeorge Keishing        report[bmc-dummy-dnsname.com][stdev]:       0.0
1608b270ecfSMichael Walsh
1618b270ecfSMichael Walsh    Description of arguments:
1628b270ecfSMichael Walsh    host   The DNS name or IP address to be passed to the mtr command.
1638b270ecfSMichael Walsh    """
1648b270ecfSMichael Walsh
1658b270ecfSMichael Walsh    # Run the mtr command.  Exlude the header line.  Trim leading space from
1668b270ecfSMichael Walsh    # each line.  Change all multiple spaces delims to single space delims.
1678b270ecfSMichael Walsh    cmd_buf = "mtr --report " + host +\
1688b270ecfSMichael Walsh        " | tail -n +2 | sed -r -e 's/^[ ]+//g' -e 's/[ ]+/ /g'"
1698b270ecfSMichael Walsh    sub_proc = subprocess.Popen(cmd_buf, shell=True, stdout=subprocess.PIPE,
1708b270ecfSMichael Walsh                                stderr=subprocess.STDOUT)
1718b270ecfSMichael Walsh    out_buf, err_buf = sub_proc.communicate()
1728b270ecfSMichael Walsh    shell_rc = sub_proc.returncode
1738b270ecfSMichael Walsh
1748b270ecfSMichael Walsh    # Split the output by line.
1758b270ecfSMichael Walsh    rows = out_buf.rstrip('\n').split("\n")
1768b270ecfSMichael Walsh
1778b270ecfSMichael Walsh    # Initialize report dictionary.
1788b270ecfSMichael Walsh    report = DotDict()
1798b270ecfSMichael Walsh    for row in rows:
1808b270ecfSMichael Walsh        # Process each row of mtr output.
1818b270ecfSMichael Walsh        # Create a list of fields by splitting on space delimiter.
1828b270ecfSMichael Walsh        row_list = row.split(" ")
1838b270ecfSMichael Walsh        # Create dictionary for the row.
1848b270ecfSMichael Walsh        row = DotDict()
1858b270ecfSMichael Walsh        row['row_num'] = row_list[0].rstrip('.')
1868b270ecfSMichael Walsh        row['host'] = row_list[1]
1878b270ecfSMichael Walsh        row['loss'] = row_list[2].rstrip('%')
1888b270ecfSMichael Walsh        row['snt'] = row_list[3]
1898b270ecfSMichael Walsh        row['last'] = row_list[4]
1908b270ecfSMichael Walsh        row['avg'] = row_list[5]
1918b270ecfSMichael Walsh        row['best'] = row_list[6]
1928b270ecfSMichael Walsh        row['wrst'] = row_list[7]
1938b270ecfSMichael Walsh        row['stdev'] = row_list[8]
1948b270ecfSMichael Walsh        report[row['host']] = row
1958b270ecfSMichael Walsh
1968b270ecfSMichael Walsh    # Return the full report as dictionary of dictionaries.
1978b270ecfSMichael Walsh    return report
1988b270ecfSMichael Walsh
1998b270ecfSMichael Walsh###############################################################################
2008b270ecfSMichael Walsh
2018b270ecfSMichael Walsh
2028b270ecfSMichael Walsh###############################################################################
2038b270ecfSMichael Walshdef get_mtr_row(host=""):
2048b270ecfSMichael Walsh
2058b270ecfSMichael Walsh    r"""
2068b270ecfSMichael Walsh    Run an mtr report and get a specified row and return it as a dictionary.
2078b270ecfSMichael Walsh
2088b270ecfSMichael Walsh    Example result:
2098b270ecfSMichael Walsh
2108b270ecfSMichael Walsh    row:
2118b270ecfSMichael Walsh      row[row_num]:              2
212fb4b1256SGeorge Keishing      row[host]:                 bmc-dummy-dnsname.com
2138b270ecfSMichael Walsh      row[loss]:                 0.0
2148b270ecfSMichael Walsh      row[snt]:                  10
2158b270ecfSMichael Walsh      row[last]:                 0.5
2168b270ecfSMichael Walsh      row[avg]:                  0.5
2178b270ecfSMichael Walsh      row[best]:                 0.4
2188b270ecfSMichael Walsh      row[wrst]:                 0.7
2198b270ecfSMichael Walsh      row[stdev]:                0.1
2208b270ecfSMichael Walsh
2218b270ecfSMichael Walsh    Description of arguments:
2228b270ecfSMichael Walsh    host   The DNS name or IP address to be passed to the mtr command as
2238b270ecfSMichael Walsh           well as the indicating which row of the report to return.
2248b270ecfSMichael Walsh    """
2258b270ecfSMichael Walsh
2268b270ecfSMichael Walsh    report = get_mtr_report(host)
2278b270ecfSMichael Walsh
2288b270ecfSMichael Walsh    # The max length of host in output is 28 chars.
2298b270ecfSMichael Walsh    row = [value for key, value in report.items() if host[0:28] in key][0]
2308b270ecfSMichael Walsh
2318b270ecfSMichael Walsh    return row
2328b270ecfSMichael Walsh
2338b270ecfSMichael Walsh###############################################################################
234efa97357SGeorge Keishing
235efa97357SGeorge Keishing
236efa97357SGeorge Keishing###############################################################################
237efa97357SGeorge Keishingdef list_to_set(fru_list=""):
238efa97357SGeorge Keishing    r"""
239efa97357SGeorge Keishing    Pack the list into a set tuple and return.
240efa97357SGeorge Keishing
241efa97357SGeorge Keishing    It may seem that this function is rather trivial. However, it simplifies
242efa97357SGeorge Keishing    the code and improves robot program readability and achieve the result
243efa97357SGeorge Keishing    required.
244efa97357SGeorge Keishing
245efa97357SGeorge Keishing    Example result:
246efa97357SGeorge Keishing
247efa97357SGeorge Keishing    set(['Version', 'PartNumber', 'SerialNumber', 'FieldReplaceable',
248efa97357SGeorge Keishing    'BuildDate', 'Present', 'Manufacturer', 'PrettyName', 'Cached', 'Model'])
249efa97357SGeorge Keishing
250efa97357SGeorge Keishing    # Description of arguments.
251efa97357SGeorge Keishing    fru_list   List of FRU's elements.
252efa97357SGeorge Keishing    """
253efa97357SGeorge Keishing    return set(fru_list)
254efa97357SGeorge Keishing
255efa97357SGeorge Keishing###############################################################################
256*81ae45b6SGeorge Keishing
257*81ae45b6SGeorge Keishing
258*81ae45b6SGeorge Keishingdef min_list_value(value_list):
259*81ae45b6SGeorge Keishing    r"""
260*81ae45b6SGeorge Keishing    Returns the element from the list with minimum value.
261*81ae45b6SGeorge Keishing    """
262*81ae45b6SGeorge Keishing    return min(value_list)
263