def _juniper_static_converter(hostname:str(), cmd_outputs:json) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    if "route-information" in cmd_outputs.keys():
        for instance_route in cmd_outputs.get("route-information")[0].get("route-table"):
            if "rt" in instance_route.keys():
                for route in instance_route.get("rt"):

                    nexthops_lst = ListNexthop(
                        nexthops_lst=list()
                    )

                    for route_entry in route.get("rt-entry"):
                        for nexthop in route_entry.get("nh"):

                            nexthops_lst.nexthops_lst.append(
                                Nexthop(
                                    ip_address=nexthop.get('to')[0].get("data", NOT_SET),
                                    is_in_fib=nexthop.get('always_true_in_juniper', True),
                                    out_interface=_mapping_interface_name(
                                        nexthop.get('via')[0].get("data", NOT_SET)
                                    ),
                                    preference=route_entry.get('preference')[0].get("data", NOT_SET),
                                    metric=NOT_SET,
                                    active=nexthop.get('always_true_in_juniper', True)
                                )
                            )

                        # Example of default table route => "data" : "inet.0"
                        if instance_route.get("table-name")[0].get("data") == "inet.0":
                            vrf_name = "default"
                        else:
                            index_dot = instance_route.get("table-name")[0].get("data").find(".")
                            vrf_name = instance_route.get("table-name")[0].get("data")[:index_dot]

                        # Output is => "data" : "10.255.255.103/32"
                        index_slash = str(route.get("rt-destination")[0].get("data", NOT_SET)).find("/")

                        static_routes_lst.static_routes_lst.append(
                            Static(
                                vrf_name=vrf_name,
                                prefix=str(route.get("rt-destination")[0].get("data", NOT_SET))[:index_slash],
                                netmask=str(route.get("rt-destination")[0].get("data", NOT_SET))[index_slash + 1:],
                                nexthop=nexthops_lst
                            )
                        )

    return static_routes_lst
def _arista_static_converter(hostname:str(), cmd_outputs:list) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    for cmd_output in cmd_outputs:

        for vrf_name in cmd_output.get('vrfs').keys():

            for prefix, facts in cmd_output.get('vrfs').get(vrf_name).get("routes").items():

                index_slash = str(prefix).find("/")

                nexthops_lst = ListNexthop(
                    nexthops_lst=list()
                )

                for nexthop in facts.get('vias'):

                    nexthop_obj = Nexthop(
                        ip_address=nexthop.get('nexthopAddr', NOT_SET),
                        is_in_fib=nexthop.get('always_true_in_arista', True),
                        out_interface=_mapping_interface_name(nexthop.get('interface', NOT_SET)),
                        preference=facts.get('preference', NOT_SET),
                        metric=facts.get('metric', NOT_SET),
                        active=nexthop.get('always_true_in_arista', True)
                    )

                    nexthops_lst.nexthops_lst.append(nexthop_obj)

                static_obj = Static(
                    vrf_name=vrf_name,
                    prefix=str(prefix)[:index_slash],
                    netmask=str(prefix)[index_slash + 1:],
                    nexthop=nexthops_lst
                )

                static_routes_lst.static_routes_lst.append(static_obj)

    return static_routes_lst
def _ios_static_converter(cmd_outputs:dict) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    for vrf in cmd_outputs:

        for route in cmd_outputs.get(vrf):
            nexthops_lst = ListNexthop(
                nexthops_lst=list()
            )

            out_int = route[4] if str(route[4]).find('.') == -1 else NOT_SET
            next_hop = _mapping_interface_name(route[4]) if str(route[4]).find('.') == -1 else route[4]

            nexthops_lst.nexthops_lst.append(
                Nexthop(
                    ip_address=next_hop,
                    is_in_fib=True,
                    out_interface=_mapping_interface_name(
                        out_int
                    ),
                    preference=route[1] if route[1] != '' else 1 ,
                    metric=route[2] if route[2] != '' else 1,
                    active=True
                )
            )

            static_routes_lst.static_routes_lst.append(
                Static(
                    vrf_name=vrf,
                    prefix=route[0],
                    netmask=route[3],
                    nexthop=nexthops_lst
                )
            )

    return static_routes_lst
示例#4
0
def _compare_static(host_keys,
                    hostname: str,
                    groups: list,
                    static_host_data: ListStatic,
                    test=False,
                    own_vars={},
                    task=Task) -> bool:

    verity_static = ListStatic(static_routes_lst=list())

    if (own_vars is not None and 'enable' in own_vars.keys()
            and own_vars.get('enable') is True):
        verity_static = _retrieve_in_ansible_vars(
            task=task,
            dict_keys=own_vars.get('dict_keys', str()),
            your_keys=own_vars.get('your_keys', list()))
    else:
        if test:
            static_yaml_data = open_file(
                path="tests/features/src/static_tests.yml")
        else:
            static_yaml_data = select_host_vars(hostname=hostname,
                                                groups=groups,
                                                protocol="static")

        if STATIC_DATA_HOST_KEY in host_keys and hostname in static_yaml_data.keys(
        ):
            for vrf_name, facts_lst in static_yaml_data.get(hostname).items():
                for facts in facts_lst:

                    if facts.get('netmask', NOT_SET) == NOT_SET:

                        index_slash = str(facts.get('prefix')).find("/")

                        if is_cidr_notation(
                                str(facts.get('prefix'))[index_slash + 1:]):
                            if is_valid_cidr_netmask(
                                    str(facts.get('prefix'))[index_slash +
                                                             1:]):
                                netmask = convert_cidr_to_netmask(
                                    str(facts.get('prefix'))[index_slash + 1:])
                            else:
                                netmask = NOT_SET
                        else:
                            netmask = str(facts.get('prefix'))[index_slash +
                                                               1:]

                        nexthops_lst = ListNexthop(nexthops_lst=list())

                        for nexthop in facts.get('nexthop', NOT_SET):

                            nexthop_obj = Nexthop(
                                ip_address=nexthop.get('ip_address', NOT_SET),
                                is_in_fib=nexthop.get('is_in_fib', False),
                                out_interface=nexthop.get(
                                    'out_interface', NOT_SET),
                                preference=nexthop.get('preference', NOT_SET),
                                metric=nexthop.get('metric', NOT_SET),
                                active=nexthop.get('metric', NOT_SET))

                            nexthops_lst.nexthops_lst.append(nexthop_obj)

                        static_obj = Static(
                            vrf_name=vrf_name,
                            prefix=str(facts.get('prefix'))[:index_slash],
                            netmask=netmask,
                            nexthop=nexthops_lst)

                    else:

                        if is_cidr_notation(facts.get('netmask', NOT_SET)):
                            if is_valid_cidr_netmask(
                                    facts.get('netmask', NOT_SET)):
                                netmask = convert_cidr_to_netmask(
                                    facts.get('netmask', NOT_SET))
                            else:
                                netmask = NOT_SET
                        else:
                            netmask = facts.get('netmask', NOT_SET)

                        nexthops_lst = ListNexthop(nexthops_lst=list())

                        for nexthop in facts.get('nexthop', NOT_SET):

                            nexthop_obj = Nexthop(
                                ip_address=nexthop.get('ip_address', NOT_SET),
                                is_in_fib=nexthop.get('is_in_fib', False),
                                out_interface=nexthop.get(
                                    'out_interface', NOT_SET),
                                preference=nexthop.get('preference', NOT_SET),
                                metric=nexthop.get('metric', NOT_SET),
                                active=nexthop.get('metric', NOT_SET))

                            nexthops_lst.nexthops_lst.append(nexthop_obj)

                        static_obj = Static(vrf_name=vrf_name,
                                            prefix=facts.get(
                                                'prefix', NOT_SET),
                                            netmask=netmask,
                                            nexthop=nexthops_lst)

                    verity_static.static_routes_lst.append(static_obj)
        else:
            print(f"Key {STATIC_DATA_HOST_KEY} is missing for {hostname}")

    return verity_static == static_host_data
示例#5
0
def _retrieve_in_ansible_vars(task, dict_keys="", your_keys={}) -> ListStatic:
    """
    This function will automatically retrieve data in Ansible vars.

    :param task: Nornir Task
    :param dict_keys: String contains keys to retreieve static route
    :param your_keys: Dictionnary containing keys using in vars_files
    :return ListStatic: List of static routes retrieve in Ansible vars files
    """

    static_routes_lst = ListStatic(static_routes_lst=list())

    if dict_keys != "":

        dyn_keys_lst = str(dict_keys).split(">")
        res = task.host.get(dyn_keys_lst[0], dict)

        if len(dyn_keys_lst) > 1:
            for key in dyn_keys_lst[1:]:
                res = res.get(key, {})

        for vrf_name, facts_lst in res.items():
            for facts in facts_lst:

                prefix_key = your_keys.get('prefix', 'prefix')
                netmask_key = your_keys.get('netmask', 'netmask')
                nexthop_key = your_keys.get('nexthop', 'nexthop')
                preference_key = your_keys.get('preference', 'preference')
                metric_key = your_keys.get('metric', 'metric')
                ip_address_key = your_keys.get('ip_address', 'ip')

                if facts.get(netmask_key, NOT_SET) == NOT_SET:

                    index_slash = str(facts.get(prefix_key)).find("/")

                    if is_cidr_notation(
                            str(facts.get(prefix_key))[index_slash + 1:]):
                        if is_valid_cidr_netmask(
                                str(facts.get(prefix_key))[index_slash + 1:]):
                            netmask = convert_cidr_to_netmask(
                                str(facts.get(prefix_key))[index_slash + 1:])
                        else:
                            netmask = NOT_SET
                    else:
                        netmask = str(facts.get(prefix_key))[index_slash + 1:]

                    nexthops_lst = ListNexthop(nexthops_lst=list())

                    for nexthop in facts.get(nexthop_key, NOT_SET):

                        nexthop_obj = Nexthop(
                            ip_address=nexthop.get(ip_address_key, NOT_SET),
                            is_in_fib=nexthop.get('is_in_fib', False),
                            out_interface=nexthop.get('out_interface',
                                                      NOT_SET),
                            preference=nexthop.get(preference_key, NOT_SET),
                            metric=nexthop.get(metric_key, NOT_SET),
                            active=nexthop.get('active', NOT_SET))

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                    static_obj = Static(
                        vrf_name=vrf_name,
                        prefix=str(facts.get(prefix_key))[:index_slash],
                        netmask=netmask,
                        nexthop=nexthops_lst)

                else:

                    if is_cidr_notation(facts.get(netmask_key, NOT_SET)):
                        if is_valid_cidr_netmask(
                                facts.get(netmask_key, NOT_SET)):
                            netmask = convert_cidr_to_netmask(
                                facts.get(netmask_key, NOT_SET))
                        else:
                            netmask = NOT_SET
                    else:
                        netmask = facts.get(netmask_key, NOT_SET)

                    nexthops_lst = ListNexthop(nexthops_lst=list())

                    for nexthop in facts.get(nexthop_key, NOT_SET):

                        nexthop_obj = Nexthop(
                            ip_address=nexthop.get(ip_address_key, NOT_SET),
                            is_in_fib=nexthop.get('is_in_fib', False),
                            out_interface=nexthop.get('out_interface',
                                                      NOT_SET),
                            preference=nexthop.get(preference_key, NOT_SET),
                            metric=nexthop.get(metric_key, NOT_SET),
                            active=nexthop.get('active', NOT_SET))

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                    static_obj = Static(vrf_name=vrf_name,
                                        prefix=facts.get(prefix_key, NOT_SET),
                                        netmask=netmask,
                                        nexthop=nexthops_lst)

                static_routes_lst.static_routes_lst.append(static_obj)

    return static_routes_lst
def _cumulus_static_converter(hostname:str(), cmd_outputs:list, vrf_dict:dict()) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    for cmd_output in cmd_outputs:

        if "ipv4 unicast" in cmd_output.keys():

            for prefix in cmd_output.get('ipv4 unicast').keys():

                nexthops_lst = ListNexthop(
                    nexthops_lst=list()
                )

                index_slash = str(prefix).find("/")

                for facts in cmd_output.get('ipv4 unicast').get(prefix):

                    real_vrf = ""
                    for vrf, vrfid in vrf_dict.items():
                        try:
                            if int(facts.get('vrfId', NOT_SET)) == int(vrfid):
                                real_vrf = vrf
                        except Exception as e:
                            pass

                    for nexthop in facts.get('nexthops'):

                        nexthop_obj = Nexthop(
                            ip_address=nexthop.get('ip', NOT_SET),
                            is_in_fib=nexthop.get('nexthops', False),
                            out_interface=_mapping_interface_name(nexthop.get('interfaceName', NOT_SET)),
                            preference=facts.get('distance', NOT_SET),
                            metric=facts.get('metric', NOT_SET),
                            active=nexthop.get('nexthops', False)
                        )

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                static_obj = Static(
                    vrf_name=real_vrf,
                    prefix=str(prefix)[:index_slash],
                    netmask=str(prefix)[index_slash+1:],
                    nexthop=nexthops_lst
                )

                static_routes_lst.static_routes_lst.append(static_obj)

        else:
            for prefix in cmd_output.keys():

                nexthops_lst = ListNexthop(
                    nexthops_lst=list()
                )

                index_slash = str(prefix).find("/")

                for facts in cmd_output.get(prefix):

                    index_slash = str(facts.get('prefix')).find("/")

                    for nexthop in facts.get('nexthops'):

                        nexthop_obj = Nexthop(
                            ip_address=nexthop.get('ip', NOT_SET),
                            is_in_fib=nexthop.get('nexthops', False),
                            out_interface=_mapping_interface_name(nexthop.get('interfaceName', NOT_SET)),
                            preference=facts.get('distance', NOT_SET),
                            metric=facts.get('metric', NOT_SET),
                            active=nexthop.get('nexthops', False)
                        )

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                static_obj = Static(
                    vrf_name="default",
                    prefix=str(prefix)[:index_slash],
                    netmask=str(prefix)[index_slash+1:],
                    nexthop=nexthops_lst
                )

                static_routes_lst.static_routes_lst.append(static_obj)

    return static_routes_lst
def _extreme_vsp_static_converter(cmd_outputs:dict) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    for vrf in cmd_outputs:

        length = len(cmd_outputs.get(vrf))-1

        while length > 0:

            nb = 0

            nexthops_lst = ListNexthop(
                nexthops_lst=list()
            )

            if cmd_outputs.get(vrf)[length-nb][0] == cmd_outputs.get(vrf)[length-(nb+1)][0]:
                while cmd_outputs.get(vrf)[length-nb][0] == cmd_outputs.get(vrf)[length-(nb+1)][0]:

                    nexthops_lst.nexthops_lst.append(
                        Nexthop(
                            ip_address=cmd_outputs.get(vrf)[length-nb][2],
                            is_in_fib=cmd_outputs.get(vrf)[length-nb][7],
                            out_interface=NOT_SET,
                            preference=cmd_outputs.get(vrf)[length-nb][5],
                            metric=cmd_outputs.get(vrf)[length-nb][5],
                            active=cmd_outputs.get(vrf)[length-nb][7]
                        )
                    )

                    nb += 1

                nexthops_lst.nexthops_lst.append(
                    Nexthop(
                        ip_address=cmd_outputs.get(vrf)[length - nb][2],
                        is_in_fib=cmd_outputs.get(vrf)[length - nb][7],
                        out_interface=NOT_SET,
                        preference=cmd_outputs.get(vrf)[length - nb][5],
                        metric=cmd_outputs.get(vrf)[length - nb][5],
                        active=cmd_outputs.get(vrf)[length - nb][7]
                    )
                )

            else:

                nexthops_lst.nexthops_lst.append(
                    Nexthop(
                        ip_address=cmd_outputs.get(vrf)[length - nb][2],
                        is_in_fib=cmd_outputs.get(vrf)[length - nb][7],
                        out_interface=NOT_SET,
                        preference=cmd_outputs.get(vrf)[length - nb][5],
                        metric=cmd_outputs.get(vrf)[length - nb][5],
                        active=cmd_outputs.get(vrf)[length - nb][7]
                    )
                )

                nb += 1


            static_routes_lst.static_routes_lst.append(
                Static(
                    vrf_name=vrf,
                    prefix=cmd_outputs.get(vrf)[length][0],
                    netmask=cmd_outputs.get(vrf)[length][1],
                    nexthop=nexthops_lst
                )
            )

            length = length - nb

    return  static_routes_lst
def _nexus_static_converter(hostname:str(), cmd_outputs:list) -> ListStatic:

    static_routes_lst = ListStatic(
        static_routes_lst=list()
    )

    for cmd_output in cmd_outputs:
        if 'TABLE_vrf' in cmd_output.keys():

            if isinstance(cmd_output.get('TABLE_vrf').get('ROW_vrf').get('TABLE_addrf').get('ROW_addrf').get(
                    'TABLE_prefix').get('ROW_prefix'), list):

                for facts in cmd_output.get('TABLE_vrf').get('ROW_vrf').get('TABLE_addrf').get('ROW_addrf').get(
                        'TABLE_prefix').get('ROW_prefix'):

                    nexthops_lst = ListNexthop(
                        nexthops_lst=list()
                    )

                    if isinstance(facts.get('TABLE_path').get('ROW_path'), list):
                        for nexthop in facts.get('TABLE_path').get('ROW_path'):

                            nexthop_obj = Nexthop(
                                ip_address=nexthop.get('ipnexthop', NOT_SET),
                                is_in_fib=nexthop.get('always_true_in_nexus', True),
                                out_interface=NOT_SET,
                                preference=nexthop.get('pref', NOT_SET),
                                metric=nexthop.get('metric', NOT_SET),
                                active=nexthop.get('always_true_in_nexus', True),
                            )

                            nexthops_lst.nexthops_lst.append(nexthop_obj)


                    elif isinstance(facts.get('TABLE_path').get('ROW_path'), dict):

                        nexthop_obj = Nexthop(
                            ip_address=facts.get('TABLE_path').get('ROW_path').get('ipnexthop', NOT_SET),
                            is_in_fib=facts.get('TABLE_path').get('ROW_path').get('always_true_in_nexus', True),
                            out_interface=NOT_SET,
                            preference=facts.get('TABLE_path').get('ROW_path').get('pref', NOT_SET),
                            metric=facts.get('TABLE_path').get('ROW_path').get('metric', NOT_SET),
                            active=facts.get('TABLE_path').get('ROW_path').get('always_true_in_nexus', True)
                        )

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                    index_slash = str(facts.get('ipprefix')).find("/")

                    static_obj = Static(
                        vrf_name=cmd_output.get('TABLE_vrf').get('ROW_vrf').get('vrf-name-out', NOT_SET),
                        prefix=str(facts.get('ipprefix'))[:index_slash],
                        netmask=str(facts.get('ipprefix'))[index_slash + 1:],
                        nexthop=nexthops_lst
                    )

                    static_routes_lst.static_routes_lst.append(static_obj)

            elif isinstance(cmd_output.get('TABLE_vrf').get('ROW_vrf').get('TABLE_addrf').get('ROW_addrf').get(
                    'TABLE_prefix').get('ROW_prefix'), dict):

                for facts in cmd_output.get('TABLE_vrf').get('ROW_vrf').get('TABLE_addrf').get('ROW_addrf').get(
                        'TABLE_prefix').values():

                    nexthops_lst = ListNexthop(
                        nexthops_lst=list()
                    )

                    if isinstance(facts.get('TABLE_path').get('ROW_path'), list):
                        for nexthop in facts.get('TABLE_path').get('ROW_path'):

                            nexthop_obj = Nexthop(
                                ip_address=nexthop.get('ipnexthop', NOT_SET),
                                is_in_fib=nexthop.get('always_true_in_nexus', True),
                                out_interface=NOT_SET,
                                preference=nexthop.get('pref', NOT_SET),
                                metric=nexthop.get('metric', NOT_SET),
                                active=nexthop.get('always_true_in_nexus', True),
                            )

                            nexthops_lst.nexthops_lst.append(nexthop_obj)


                    elif isinstance(facts.get('TABLE_path').get('ROW_path'), dict):

                        nexthop_obj = Nexthop(
                            ip_address=facts.get('TABLE_path').get('ROW_path').get('ipnexthop', NOT_SET),
                            is_in_fib=facts.get('TABLE_path').get('ROW_path').get('always_true_in_nexus', True),
                            out_interface=NOT_SET,
                            preference=facts.get('TABLE_path').get('ROW_path').get('pref', NOT_SET),
                            metric=facts.get('TABLE_path').get('ROW_path').get('metric', NOT_SET),
                            active=facts.get('TABLE_path').get('ROW_path').get('always_true_in_nexus', True)
                        )

                        nexthops_lst.nexthops_lst.append(nexthop_obj)

                    index_slash = str(facts.get('ipprefix')).find("/")

                    static_obj = Static(
                        vrf_name=cmd_output.get('TABLE_vrf').get('ROW_vrf').get('vrf-name-out', NOT_SET),
                        prefix=str(facts.get('ipprefix'))[:index_slash],
                        netmask=str(facts.get('ipprefix'))[index_slash + 1:],
                        nexthop=nexthops_lst
                    )

                    static_routes_lst.static_routes_lst.append(static_obj)

    print(static_routes_lst)
    return static_routes_lst
示例#9
0
def create_a_static_object_manually(context) -> None:
    """
    Create a Static object manually

    :param context:
    :return None:
    """

    static_routes_lst = ListStatic(static_routes_lst=list())

    #
    ## 1st Object
    #
    nexthop_lst = ListNexthop(list())

    nexthop_lst.nexthops_lst.append(
        Nexthop(ip_address='10.22.33.1',
                is_in_fib=True,
                out_interface='eth1/3',
                preference='1',
                metric='0',
                active=True))

    static_routes_lst.static_routes_lst.append(
        Static(vrf_name='default',
               prefix='10.255.255.202',
               netmask='255.255.255.255',
               nexthop=nexthop_lst))

    #
    ## 2nd Object
    #
    nexthop_lst = ListNexthop(list())

    nexthop_lst.nexthops_lst.append(
        Nexthop(ip_address='10.1.3.1',
                is_in_fib=True,
                out_interface='eth1/1',
                preference='1',
                metric='0',
                active=True))

    static_routes_lst.static_routes_lst.append(
        Static(vrf_name='default',
               prefix='10.255.255.101',
               netmask='255.255.255.255',
               nexthop=nexthop_lst))

    #
    ## 3rd Object
    #
    nexthop_lst = ListNexthop(list())

    nexthop_lst.nexthops_lst.append(
        Nexthop(ip_address='10.0.5.1',
                is_in_fib=True,
                out_interface='mgmt1',
                preference='1',
                metric='0',
                active=True))

    static_routes_lst.static_routes_lst.append(
        Static(vrf_name='mgmt',
               prefix='0.0.0.0',
               netmask='0.0.0.0',
               nexthop=nexthop_lst))

    context.object_01 = static_routes_lst