Exemplo n.º 1
0
def _compare_facts(host_keys,
                   hostname: str,
                   groups: list,
                   facts_host_data: Facts,
                   test=False,
                   options={},
                   task=Task) -> bool:
    if ('own_vars' in options.keys() and options.get('own_vars') is not None
            and 'enable' in options.get('own_vars').keys()
            and options.get('own_vars').get('enable') is True):
        raise NetestsOverideTruthVarsKeyUnsupported()
    else:
        if test:
            facts_yaml_data = open_file(
                path="tests/features/src/facts_tests.yml").get(hostname)
        else:
            facts_yaml_data = select_host_vars(hostname=hostname,
                                               groups=groups,
                                               protocol="facts")

        if (FACTS_DATA_HOST_KEY in host_keys and facts_yaml_data is not None):
            verity_facts = Facts(
                hostname=hostname,
                domain=facts_yaml_data.get('domain', NOT_SET),
                version=facts_yaml_data.get('version', NOT_SET),
                build=facts_yaml_data.get('build', NOT_SET),
                serial=facts_yaml_data.get('serial', NOT_SET),
                base_mac=facts_yaml_data.get('serial', NOT_SET),
                memory=facts_yaml_data.get('memory', NOT_SET),
                vendor=facts_yaml_data.get('vendor', NOT_SET),
                model=facts_yaml_data.get('model', NOT_SET),
                interfaces_lst=facts_yaml_data.get('interfaces', list()),
                options=facts_host_data.options)

        else:
            print(f"{HEADER} Key {FACTS_DATA_HOST_KEY} is missing"
                  f"for {hostname} or no Facts data has been found.")
            return False

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL2):
        print(f"{HEADER} Return value for host {hostname}"
              f"is {verity_facts == facts_host_data}")

    return verity_facts == facts_host_data
Exemplo n.º 2
0
def _compare_vrf(host_keys,
                 hostname: str,
                 groups: list,
                 vrf_host_data: ListVRF,
                 test=False,
                 options={},
                 task=Task) -> bool:
    verity_vrf = ListVRF(list())

    if ('own_vars' in options.keys() and options.get('own_vars') is not None
            and 'enable' in options.get('own_vars').keys()
            and options.get('own_vars').get('enable') is True):
        raise NetestsOverideTruthVarsKeyUnsupported()
    else:
        if test:
            vrf_yaml_data = open_file(
                path="tests/features/src/vrf_tests.yml").get(hostname)
        else:
            vrf_yaml_data = select_host_vars(hostname=hostname,
                                             groups=groups,
                                             protocol="vrf")

        if (VRF_DATA_KEY in host_keys and vrf_yaml_data is not None):
            if vrf_yaml_data is not None:
                for vrf in vrf_yaml_data:
                    verity_vrf.vrf_lst.append(
                        VRF(vrf_name=vrf.get('vrf_name', NOT_SET),
                            vrf_id=vrf.get('vrf_id', NOT_SET),
                            l3_vni=vrf.get('l3_vni', NOT_SET),
                            rd=vrf.get('rd', NOT_SET),
                            rt_imp=vrf.get('rt_imp', NOT_SET),
                            rt_exp=vrf.get('rt_exp', NOT_SET),
                            imp_targ=vrf.get('imp_targ', NOT_SET),
                            exp_targ=vrf.get('exp_targ', NOT_SET)))
        else:
            print(f"{HEADER} Key {VRF_DATA_KEY} is missing"
                  f"for {hostname} or no VRF data has been found.")

    if verbose_mode(user_value=os.environ.get("NETESTS_VERBOSE", NOT_SET),
                    needed_value=LEVEL2):
        print(f"{HEADER} Return value for host {hostname}"
              f"is {verity_vrf == vrf_host_data}")
    return verity_vrf == vrf_host_data
Exemplo n.º 3
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
Exemplo n.º 4
0
def _compare_vlan(host_keys,
                  hostname: str,
                  groups: list,
                  vlan_host_data: ListVLAN,
                  test: False,
                  own_vars={},
                  task=Task) -> bool:
    verity_vlans_lst = ListVLAN(vlans_lst=list())

    if test:
        vlan_yaml_data = open_file(path="tests/features/src/vlan_tests.yml")
    else:
        vlan_yaml_data = select_host_vars(hostname=hostname,
                                          groups=groups,
                                          protocol="vlan")

    if (VLAN_DATA_HOST_KEY in host_keys and vlan_yaml_data is not None
            and hostname in vlan_yaml_data.keys()):
        if vlan_yaml_data.get(hostname) is not None:
            for vlan in vlan_yaml_data.get(hostname):
                ipv4_addresses_lst = ListIPV4(ipv4_addresses_lst=list())

                ipv6_addresses_lst = ListIPV6(ipv6_addresses_lst=list())

                if "ip_address" in vlan.keys():
                    if isinstance(vlan.get("ip_address"), list):
                        for ip_address in vlan.get("ip_address"):
                            index_slash = str(ip_address).find("/")
                            ipv4_addresses_lst.ipv4_addresses_lst.append(
                                IPV4(ip_address_with_mask=str(ip_address)
                                     [:index_slash],
                                     netmask=str(ip_address)[index_slash +
                                                             1:]))
                    elif isinstance(vlan.get("ip_address"), str):
                        index_slash = str(vlan.get("ip_address")).find("/")
                        ipv4_addresses_lst.ipv4_addresses_lst.append(
                            IPV4(ip_address_with_mask=str(
                                vlan.get("ip_address"))[:index_slash],
                                 netmask=str(
                                     vlan.get("ip_address"))[index_slash +
                                                             1:]))

                if "ipv6_address" in vlan.keys():
                    if isinstance(vlan.get("ipv6_address"), list):
                        for ipv6_address in vlan.get("ipv6_address"):
                            ipv6_addresses_lst.ipv6_addresses_lst.append(
                                IPV6(ip_address_with_mask=str(ipv6_address), ))
                    elif isinstance(vlan.get("ip_address"), str):
                        ipv6_addresses_lst.ipv6_addresses_lst.append(
                            IPV6(ip_address_with_mask=str(
                                vlan.get("ip_address"))))

                ports_members = list()
                if "ports_members" in vlan.keys():
                    for port in vlan.get("ports_members"):
                        ports_members.append(_mapping_interface_name(port))

                verity_vlans_lst.vlans_lst.append(
                    VLAN(vlan_name=vlan.get("vlan_name", NOT_SET),
                         vlan_id=vlan.get("vlan_id", NOT_SET),
                         vlan_descr=vlan.get("vlan_descr", NOT_SET),
                         vrf_name=vlan.get("vrf_name", NOT_SET),
                         ipv6_addresses=ipv6_addresses_lst,
                         fhrp_ipv6_address=vlan.get("fhrp_ipv6_address",
                                                    "0.0.0.0"),
                         ipv4_addresses=ipv4_addresses_lst,
                         fhrp_ipv4_address=vlan.get("fhrp_ipv4_address",
                                                    "0.0.0.0"),
                         ports_members=ports_members,
                         mac_address=vlan.get("mac_address", NOT_SET)))

    else:
        print(f"{HEADER}Key {VLAN_DATA_HOST_KEY} is missing for {hostname}")
        return False

    return verity_vlans_lst == vlan_host_data
Exemplo n.º 5
0
def _compare_bgp(
    host_keys,
    hostname,
    groups,
    bgp_host_data: BGP,
    test=False,
    options={},
    task=Task
):
    if (
        'own_vars' in options.keys() and
        options.get('own_vars') is not None and
        'enable' in options.get('own_vars').keys() and
        options.get('own_vars').get('enable') is True
    ):
        raise NetestsOverideTruthVarsKeyUnsupported()
    else:
        if test:
            bgp_yaml_data = open_file(
                path="tests/features/src/bgp_tests.yml"
            ).get(hostname)
        else:
            bgp_yaml_data = select_host_vars(
                hostname=hostname,
                groups=groups,
                protocol="bgp"
            )

        bgp_sessions_vrf_lst = ListBGPSessionsVRF(list())

        if BGP_SESSIONS_HOST_KEY in host_keys:
            for vrf_name, facts in bgp_yaml_data.items():
                bgp_sessions_lst = ListBGPSessions(
                    list()
                )
                for n in facts.get('neighbors', []):
                    bgp_sessions_lst.bgp_sessions.append(
                        BGPSession(
                            src_hostname=hostname,
                            peer_ip=n.get('peer_ip', NOT_SET),
                            peer_hostname=n.get('peer_hostname', NOT_SET),
                            remote_as=n.get('remote_as', NOT_SET),
                            state_brief=n.get('state_brief', NOT_SET),
                            session_state=n.get('session_state', NOT_SET),
                            state_time=n.get('state_time', NOT_SET),
                            prefix_received=n.get('prefix_received', NOT_SET),
                        )
                    )

                bgp_session_vrf = BGPSessionsVRF(
                    vrf_name=vrf_name,
                    as_number=facts.get('as_number', NOT_SET),
                    router_id=facts.get('router_id', NOT_SET),
                    bgp_sessions=bgp_sessions_lst
                )
                bgp_sessions_vrf_lst.bgp_sessions_vrf.append(bgp_session_vrf)

            verity_bgp = BGP(
                hostname=hostname,
                bgp_sessions_vrf_lst=bgp_sessions_vrf_lst
            )

            return verity_bgp == bgp_host_data

        else:
            print(f"Key {BGP_SESSIONS_HOST_KEY} is missing for {hostname}")
            return False