xref: /openbmc/openbmc-test-automation/lib/external_intf/management_console_utils.py (revision c5e9ebc68157a13c4a6602e10a42c5aed69d887b)
18023a8cfSSushil Singh#!/usr/bin/env python
28023a8cfSSushil Singh
38023a8cfSSushil Singhimport os
48023a8cfSSushil Singhimport re
58023a8cfSSushil Singhimport json
68023a8cfSSushil Singhfrom data import variables
78023a8cfSSushil Singhfrom collections import OrderedDict
88023a8cfSSushil Singh
98023a8cfSSushil Singhbmc_rec_pattern = '^=(.*)\n(.*)\n(.*)\n(.*)\n(.*)'
108023a8cfSSushil Singhbmc_prop_pattern = [r"\w+", r"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}", '443']
118023a8cfSSushil Singhbmc_rec_prop = ['hostname', 'address', 'port', 'txt']
128023a8cfSSushil Singh
138023a8cfSSushil Singh
148023a8cfSSushil Singhclass Exception(Exception):
158023a8cfSSushil Singh    def __init__(self, exc_value):
168023a8cfSSushil Singh        self.exc_value = exc_value
178023a8cfSSushil Singh
188023a8cfSSushil Singh    def __str__(self):
198023a8cfSSushil Singh        return repr(self.exc_value)
208023a8cfSSushil Singh
218023a8cfSSushil Singh
22*c5e9ebc6SSushil Singhdef Check_bmc_record_exists(bmc_records, bmc_ip):
23*c5e9ebc6SSushil Singh    r"""
24*c5e9ebc6SSushil Singh    Parse the BMC records to check for passed input.
25*c5e9ebc6SSushil Singh
26*c5e9ebc6SSushil Singh    Description of arguments:
27*c5e9ebc6SSushil Singh    bmc_records            Contains the list of discoverd BMC records.
28*c5e9ebc6SSushil Singh    bmc_ip                 BMC ip address.
29*c5e9ebc6SSushil Singh    """
30*c5e9ebc6SSushil Singh
31*c5e9ebc6SSushil Singh    try:
32*c5e9ebc6SSushil Singh        for bmc_key, bmc_val in bmc_records.items():
33*c5e9ebc6SSushil Singh            temp_ip = bmc_val.get('address', None)
34*c5e9ebc6SSushil Singh            if bmc_ip.strip() == temp_ip.strip():
35*c5e9ebc6SSushil Singh                return True
36*c5e9ebc6SSushil Singh        else:
37*c5e9ebc6SSushil Singh            return False
38*c5e9ebc6SSushil Singh    except Exception as exc_obj:
39*c5e9ebc6SSushil Singh        return exc_obj
40*c5e9ebc6SSushil Singh
41*c5e9ebc6SSushil Singh
428023a8cfSSushil Singhdef validate_bmc_properties(bmc_prop_pattern, bmc_prop, bmc_value,
438023a8cfSSushil Singh                            bmc_rec_valid):
448023a8cfSSushil Singh    r"""
458023a8cfSSushil Singh    This function is to check pattern match in bmc properties.
468023a8cfSSushil Singh
478023a8cfSSushil Singh    Description of arguments:
488023a8cfSSushil Singh    bmc_prop_pattern       Regex pattern.
498023a8cfSSushil Singh    bmc_prop               BMC property (e.g. hostname, address, port).
508023a8cfSSushil Singh    bmc_value              BMC property value.
518023a8cfSSushil Singh    bmc_rec_valid          Contain BMC properties record.
528023a8cfSSushil Singh    """
538023a8cfSSushil Singh
548023a8cfSSushil Singh    try:
558023a8cfSSushil Singh        status = \
568023a8cfSSushil Singh            [lambda bmc_prop: re.search(bmc_prop_pattern, bmc_prob),
578023a8cfSSushil Singh                bmc_value]
588023a8cfSSushil Singh        if None in status:
598023a8cfSSushil Singh            bmc_rec_valid[bmc_prop] = None
608023a8cfSSushil Singh    except Exception as exc_obj:
618023a8cfSSushil Singh        return exc_obj
628023a8cfSSushil Singh    finally:
638023a8cfSSushil Singh        return bmc_rec_valid
648023a8cfSSushil Singh
658023a8cfSSushil Singh
668023a8cfSSushil Singhdef bmc_record_validation(bmc_rec_valid):
678023a8cfSSushil Singh    r"""
688023a8cfSSushil Singh    Parse the BMC records to validate the data is valid.
698023a8cfSSushil Singh
708023a8cfSSushil Singh    Description of arguments:
718023a8cfSSushil Singh    bmc_rec_valid          Contain BMC properties record.
728023a8cfSSushil Singh    """
738023a8cfSSushil Singh
748023a8cfSSushil Singh    try:
758023a8cfSSushil Singh        for bmc_prop_key, bmc_pattern_val in \
768023a8cfSSushil Singh                zip(bmc_rec_prop, bmc_prop_pattern):
778023a8cfSSushil Singh            bmc_prop_value = bmc_rec_valid.get(bmc_prop_key, False)
788023a8cfSSushil Singh            if bmc_rec_valid[bmc_prop_key] is not False:
798023a8cfSSushil Singh                valid_status = validate_bmc_properties(bmc_pattern_val,
808023a8cfSSushil Singh                                                       bmc_prop_key,
818023a8cfSSushil Singh                                                       bmc_prop_value,
828023a8cfSSushil Singh                                                       bmc_rec_valid)
838023a8cfSSushil Singh                if None not in bmc_rec_valid.values():
848023a8cfSSushil Singh                    return bmc_rec_valid
858023a8cfSSushil Singh                else:
868023a8cfSSushil Singh                    return None
878023a8cfSSushil Singh    except Exception as exc_obj:
888023a8cfSSushil Singh        return exc_obj
898023a8cfSSushil Singh
908023a8cfSSushil Singh
918023a8cfSSushil Singhdef bmc_inventory(service_type, bmc_inv_record):
928023a8cfSSushil Singh    r"""
938023a8cfSSushil Singh    Parse single record of BMC inventory and pack to dictionary form.
948023a8cfSSushil Singh
958023a8cfSSushil Singh    Description of arguments:
968023a8cfSSushil Singh    service_type       Service type (e.g. _obmc_rest._tcp, _obmc_redfish._tcp).
978023a8cfSSushil Singh    bmc_inv_record     Individual BMC inventory record.
988023a8cfSSushil Singh
998023a8cfSSushil Singh    This function will return this variable i.e.
1008023a8cfSSushil Singh    bmc_inv in dictionary form as mention below.
1018023a8cfSSushil Singh
1028023a8cfSSushil Singh    Below are the discovered BMC detail.
1038023a8cfSSushil Singh
1048023a8cfSSushil Singh    [service]:          _obmc_XXXX._tcp
1058023a8cfSSushil Singh    [hostname]:         System Name
1068023a8cfSSushil Singh    [address]:          XXX.XXX.XXX.XXX
1078023a8cfSSushil Singh    [port]:             XXX
1088023a8cfSSushil Singh    [txt]:
1098023a8cfSSushil Singh    """
1108023a8cfSSushil Singh
1118023a8cfSSushil Singh    try:
1128023a8cfSSushil Singh        exc_obj = None
1138023a8cfSSushil Singh        bmc_inv = OrderedDict()
1148023a8cfSSushil Singh        service_count = 0
1158023a8cfSSushil Singh        for line in bmc_inv_record.split('\n'):
1168023a8cfSSushil Singh            if line == "":
1178023a8cfSSushil Singh                pass
1188023a8cfSSushil Singh            elif service_type in line:
1198023a8cfSSushil Singh                bmc_inv['service'] = service_type
1208023a8cfSSushil Singh                service_count += 1
1218023a8cfSSushil Singh            elif not line.startswith('=') and service_count == 1:
1228023a8cfSSushil Singh                bmc_inv[line.split('=')[0].strip()] = \
1238023a8cfSSushil Singh                    str(line.split('=')[-1].strip())[1:-1]
1248023a8cfSSushil Singh    except Exception as exc_obj:
1258023a8cfSSushil Singh        return exc_obj
1268023a8cfSSushil Singh    finally:
1278023a8cfSSushil Singh        valid_status = bmc_record_validation(bmc_inv)
1288023a8cfSSushil Singh        if valid_status is None:
1298023a8cfSSushil Singh            return None, exc_obj
1308023a8cfSSushil Singh        else:
1318023a8cfSSushil Singh            return valid_status, exc_obj
1328023a8cfSSushil Singh
1338023a8cfSSushil Singh
1348023a8cfSSushil Singhdef get_bmc_records(service_type, bmc_records):
1358023a8cfSSushil Singh    r"""
1368023a8cfSSushil Singh    Parse the string to filter BMC discovery.
1378023a8cfSSushil Singh
1388023a8cfSSushil Singh    Description of arguments:
1398023a8cfSSushil Singh    service_type     Service type (e.g. RESTService, RedfishService).
140*c5e9ebc6SSushil Singh    bmc_records      Contains the list of discoverd BMC records.
1418023a8cfSSushil Singh
1428023a8cfSSushil Singh    This function will return this variable i.e.
1438023a8cfSSushil Singh    bmc_inv_list in dictionary form as mention below.
1448023a8cfSSushil Singh
1458023a8cfSSushil Singh    Below are the list of discovered BMC details.
1468023a8cfSSushil Singh    [1]:
1478023a8cfSSushil Singh        [service]:          _obmc_XXXX._tcp
1488023a8cfSSushil Singh        [hostname]:         System Name
1498023a8cfSSushil Singh        [address]:          XXX.XXX.XXX.XXX
1508023a8cfSSushil Singh        [port]:             XXX
1518023a8cfSSushil Singh        [txt]:
1528023a8cfSSushil Singh    [2]:
1538023a8cfSSushil Singh        [service]:          _obmc_XXXX._tcp
1548023a8cfSSushil Singh        [hostname]:         System Name
1558023a8cfSSushil Singh        [address]:          XXX.XXX.XXX.XXX
1568023a8cfSSushil Singh        [port]:             XXX
1578023a8cfSSushil Singh        [txt]:
1588023a8cfSSushil Singh    """
1598023a8cfSSushil Singh
1608023a8cfSSushil Singh    try:
1618023a8cfSSushil Singh        count = 0
1628023a8cfSSushil Singh        exe_obj = None
1638023a8cfSSushil Singh        bmc_inv_list = OrderedDict()
1648023a8cfSSushil Singh        for match in re.finditer(bmc_rec_pattern, bmc_records,
1658023a8cfSSushil Singh                                 re.MULTILINE):
1668023a8cfSSushil Singh            bmc_record, exc_msg = \
1678023a8cfSSushil Singh                bmc_inventory(service_type, match.group())
1688023a8cfSSushil Singh            if bmc_record is not None and exc_msg is None:
1698023a8cfSSushil Singh                count += 1
1708023a8cfSSushil Singh                bmc_inv_list[count] = bmc_record
1718023a8cfSSushil Singh    except Exception as exe_obj:
1728023a8cfSSushil Singh        return exe_obj
1738023a8cfSSushil Singh    finally:
1748023a8cfSSushil Singh        if len(bmc_inv_list) == 0:
1758023a8cfSSushil Singh            '', exe_obj
1768023a8cfSSushil Singh        else:
1778023a8cfSSushil Singh            return bmc_inv_list, exe_obj
178