Example #1
0
def fab_obj_for_user_name(proj_obj, name):
    """Returns a list of fabric objects matching a user friendly name

    :param proj_obj: Project object
    :type proj_obj: brcddb.classes.project.ProjectObj
    :param fab_obj: List of brcddb fabric objects whose user friendly name matches name
    :type fab_obj: brcddb.classes.fabric.FabricObj
    """
    sl = brcddb_search.match_test(
        proj_obj.r_switch_objects(),
        dict(
            k=
            'brocade-fibrechannel-switch/fibrechannel-switch/fabric-user-friendly-name',
            v=name,
            t='exact',
            i=False))
    return brcddb_util.remove_duplicates(
        [switch_obj.r_fabric_obj() for switch_obj in sl])
Example #2
0
def obj_extract(from_obj, to_type):
    """Extracts a list of objects from an object

    :param from_obj: The brcddb object the to_obj is to be extracted from
    :type from_obj: brcddb.classes.*
    :param to_type: The object simple object type, brcddb.classes.util.get_simple_class_type(), to extract from from_obj
    :type to_type: str
    :return: List of objects extracted from from_obj associated with to_obj. Redundant objects removed
    :rtype: brcddb.classes.* (whatever type to_obj is)
    """
    from_type = brcddb_class_util.get_simple_class_type(from_obj)
    if from_type is None:
        brcdapi_log.exception('Unknown from_obj object type: ' +
                              str(type(from_type)))
        return list()
    return brcddb_util.remove_duplicates([
        obj for obj in _obj_convert_tbl[from_type][to_type](from_obj)
        if obj is not None
    ])
Example #3
0
def _isl_bw(obj, t_obj):
    """Check to see that all trunk masters logged in at the same speed

    :param obj: Switch object from the object list, obj_list, passed to best_practice()
    :type obj: brcddb.classes.switch.switch_obj
    :param t_obj: Individual test item from the test_list passed to best_practice(). Not used
    :type t_obj: dict
    :return: List of alert dictionaries {'a': alert number, 'p0': p0, 'p1': p1, 'k': '_isl_map'}
    :rtype: list
    """
    r_list = list()
    # Validate input
    obj_type = brcddb_class_util.get_simple_class_type(obj)
    if obj_type is None:
        obj_type = str(type(obj))
    if obj_type != 'SwitchObj':
        brcdapi_log.exception('Invalid object type. Expected switch_obj. Received: ' + obj_type, True)
        return r_list

    proj_obj = obj.r_project_obj()
    isl_map = obj.c_trunk_map()
    for k in isl_map.keys():
        switch_pair = isl_map.get(k)
        if _amp_in_switch_pair(obj, k, switch_pair):
            continue
        speeds = list()
        for k1 in switch_pair.keys():
            try:
                s = switch_pair.get(k1)[0][0].r_get('fibrechannel/speed')
                if s is not None:
                    speeds.append(s)
            except:
                pass  # We get here if all switches in the project were not polled
        if len(brcddb_util.remove_duplicates(speeds)) > 1:
            r_list.append({'a': t_obj.get('m'),
                           'p0': brcddb_switch.best_switch_name(obj),
                           'p1': brcddb_switch.best_switch_name(proj_obj.r_switch_obj(k)),
                           'k': 'trunk'})
    return r_list
Example #4
0
def _isl_fru(obj, t_obj):
    """Check to see if the trunks land on different FRUs. Ignores connections to AMP and fixed port switches

    :param obj: Switch object from the object list, obj_list, passed to best_practice()
    :type obj: brcddb.classes.switch.switch_obj
    :param t_obj: Individual test item from the test_list passed to best_practice(). Not used
    :type t_obj: dict
    :return: List of alert dictionaries {'a': alert number, 'p0': p0, 'p1': p1, 'k': '_isl_map'}
    :rtype: list
    """
    r_list = list()
    # Validate input
    obj_type = brcddb_class_util.get_simple_class_type(obj)
    if obj_type is None:
        obj_type = str(type(obj))
    if obj_type != 'SwitchObj':
        brcdapi_log.exception('Invalid object type. Expected switch_obj. Received: ' + obj_type, True)
        return r_list

    proj_obj = obj.r_project_obj()
    isl_map = obj.c_trunk_map()
    for k in isl_map.keys():
        switch_pair = isl_map.get(k)
        if _amp_in_switch_pair(obj, k, switch_pair):
            continue
        slots = list()
        for k1 in switch_pair.keys():
            tl = switch_pair.get(k1)
            for trunk in tl:
                if trunk[0] is not None:
                    slots.append(trunk[0].r_obj_key().split('/')[0])
        if len(brcddb_util.remove_duplicates(slots)) == 1 and slots[0] != '0':
            r_list.append({'a': t_obj.get('m'),
                           'p0': brcddb_switch.best_switch_name(obj),
                           'p1': brcddb_switch.best_switch_name(proj_obj.r_switch_obj(k)),
                           'k': 'trunk'})
    return r_list
Example #5
0
def _isl_num_links(obj, t_obj):
    """Check to see if the number of ISL in each trunk group is the same

    :param obj: Switch object from the object list, obj_list, passed to best_practice()
    :type obj: brcddb.classes.switch.switch_obj
    :param t_obj: Individual test item from the test_list passed to best_practice(). Not used
    :type t_obj: dict
    :return: List of alert dictionaries {'a': alert number, 'p0': p0, 'p1': p1, 'k': '_isl_map'}
    :rtype: list
    """
    r_list = list()
    # Validate input
    obj_type = brcddb_class_util.get_simple_class_type(obj)
    if obj_type is None:
        obj_type = str(type(obj))
    if obj_type != 'SwitchObj':
        brcdapi_log.exception('Invalid object type. Expected switch_obj. Received: ' + obj_type, True)
        return r_list

    proj_obj = obj.r_project_obj()
    isl_map = obj.c_trunk_map()
    for k in isl_map.keys():
        switch_pair = isl_map.get(k)
        if _amp_in_switch_pair(obj, k, switch_pair):
            continue
        isls_per_trunk = list()
        for k1 in switch_pair.keys():
            tl = switch_pair.get(k1)
            for trunk in tl:
                isls_per_trunk.append(len(trunk))
        if len(brcddb_util.remove_duplicates(isls_per_trunk)) > 1:
            r_list.append({'a': t_obj.get('m'),
                           'p0': brcddb_switch.best_switch_name(obj),
                           'p1': brcddb_switch.best_switch_name(proj_obj.r_switch_obj(k)),
                           'k': 'trunk'})
    return r_list
Example #6
0
def match(search_objects, search_key, in_search_term, ignore_case=False, stype='exact'):
    """Performs a regex match/search or wild card search in dict or brcddb class object(s). If search_key is a list of
        more than one, OR logic applies. Performs an iteritive search on any list, tuple, dict, or brcddb object found
        after the last search key. If a list is encountered, an iteritive search is performed on the list. If the search
        keys have not been exhausted, then the remaining search keys are applied to the iteritive searches.
        **WARNING:** Circular references will result in Python stack overflow issues. Since all brcddb objects have a
        link back to the main project object, at least one key must be used to avoid this circular reference

    :param search_objects: Required. These are the objects to search in. Usually a list
    :type search_objects: str, tuple, list, dict or any brcddb object
    :param search_key: Required. The key, or list of keys, in the objects in search_objects to match against. OR logic
    :type search_key: str, list, tuple
    :param in_search_term: Required. This is what to look for.
    :type in_search_term: str, list, tuple, bool
    :param ignore_case: Default is False. If True, ignores case in search_term. Not that keys are always case sensitive
    :type ignore_case: bool
    :param stype: Valid options are: 'exact', 'wild', 'regex-m', or 'regex-s' ('-m' for match and -s for search)
    :param stype: str
    :return return_list:  List of matching the search criteria - subset of search_objects
    :rtype: list
    """

    # Summary of wild card strings (search the web for 'python fnmatch.fnmatch' for additional informaiton):
    # *         matches everything
    # ?         matches any single character
    # [seq]     matches any character in seq
    # [!seq]    matches any character not in seq

    # Summary of ReGex strings (search the web for 'regex' for additional information:
    # abc…          Letters
    # 123…          Digits
    # \d            Any Digit
    # \D            Any Non - digit character
    # .             Any Character
    # \.            Period
    # [abc]         Only a, b, or c
    # [ ^ abc]      Not a, b, nor c
    # [a - z]       Characters a to z
    # [0 - 9]       Numbers 0 to 9
    # \w            Any Alphanumeric character
    # \W            Any Non - alphanumeric character
    # {m}           m Repetitions
    # {m, n}        m to n Repetitions
    # *             Zero or more repetitions
    # +             One or more repetitions
    # ?             Optional character
    # \s            Any Whitespace
    # \S            Any Non - whitespace character
    # ^ …$          Starts and ends
    # (…)           Capture Group
    # (a(bc))       Capture Sub - group
    # (.*)          Capture all
    # (abc | def )  Matches abc or def

    # Programmers tip: The Python re and fmatch are quite effecient. In order to search for anything in any data
    # structure, this method does not make use of list comprehensions. If you need something more effecient, create
    # a seperate method for a more specific purpose and leave this as a general purpose search and match method.

    return_list = list()
    search_term = in_search_term.lower() if ignore_case else in_search_term

    # Validate user input
    if not isinstance(search_term, (str, list, tuple, bool)):
        brcdapi_log.exception('Invalid search_term type: ' + str(type(search_term)), True)
        return return_list
    if isinstance(stype, str):
        if stype in ('regex-m', 'regex-s'):
            regex_obj = re.compile(search_term, re.IGNORECASE) if ignore_case else re.compile(search_term)
        elif stype not in ('wild', 'exact', 'bool'):
            brcdapi_log.exception('Invalid search type: ' + stype, True)
            return return_list
    else:
        brcdapi_log.exception('Search type must be str. Search type is: ' + str(type(stype)),
                              True)
        return return_list

    search_key_list = brcddb_util.convert_to_list(search_key)
    obj_list = brcddb_util.convert_to_list(search_objects)
    for obj in obj_list:
        for sk in search_key_list:
            sub_obj = brcddb_util.get_key_val(obj, sk)
            if sub_obj is not None:
                if isinstance(sub_obj, dict):
                    if len(match(sub_obj, list(sub_obj.keys()), search_term, ignore_case, stype)) > 0:
                        return_list.append(obj)
                elif isinstance(sub_obj, (str, list, tuple)):
                    for buf in brcddb_util.convert_to_list(sub_obj):  # Any match within that list is a match
                        if isinstance(buf, str):
                            test_buf = buf.lower() if ignore_case else buf
                            if stype == 'regex-m':
                                if regex_obj.match(test_buf):
                                    return_list.append(obj)
                                    break
                            elif stype == 'regex-s':
                                if regex_obj.search(test_buf):
                                    return_list.append(obj)
                                    break
                            elif stype == 'exact':
                                if search_term == test_buf:
                                    return_list.append(obj)
                                    break
                            elif stype == 'wild':
                                if fnmatch.fnmatch(test_buf, search_term):
                                    return_list.append(obj)
                                    break
                            elif stype == 'bool':
                                if isinstance(buf, bool) and isinstance(search_term, bool):
                                    if bool({search_term: buf}):
                                        return_list.append(obj)
                                        break
                        elif isinstance(buf, dict):
                            if len(match([buf.get(k) for k in buf], None, search_term, ignore_case, stype)) > 0:
                                return_list.append(obj)
                                break
                        elif isinstance(buf, (list, tuple)):
                            if len(match(buf, None, search_term, ignore_case, stype)) > 0:
                                return_list.append(obj)
                                break
                elif isinstance(sub_obj, bool):
                    if (search_term and sub_obj) or (not search_term and not sub_obj):
                        return_list.append(obj)
                        break

    if len(return_list) > 0 and 'brcddb' in str(type(return_list[0])):
        return brcddb_util.remove_duplicates(return_list)
    else:
        return return_list