Пример #1
0
Файл: ospf.py Проект: stoza/frr
def verify_ospf_rib(tgen,
                    dut,
                    input_dict,
                    next_hop=None,
                    tag=None,
                    metric=None,
                    fib=None):
    """
    This API is to verify ospf routes by running
    show ip ospf route command.

    Parameters
    ----------
    * `tgen` : Topogen object
    * `dut`: device under test
    * `input_dict` : Input dict data, required when configuring from testcase
    * `next_hop` : next to be verified
    * `tag` : tag to be verified
    * `metric` : metric to be verified
    * `fib` : True if the route is installed in FIB.

    Usage
    -----
    input_dict = {
        "r1": {
            "static_routes": [
                {
                    "network": ip_net,
                    "no_of_ip": 1,
                    "routeType": "N"
                }
            ]
        }
    }

    result = verify_ospf_rib(tgen, dut, input_dict,next_hop=nh)

    Returns
    -------
    True or False (Error Message)
    """

    logger.info("Entering lib API: verify_ospf_rib()")
    result = False
    router_list = tgen.routers()
    additional_nexthops_in_required_nhs = []
    found_hops = []
    for routerInput in input_dict.keys():
        for router, rnode in router_list.items():
            if router != dut:
                continue

            logger.info("Checking router %s RIB:", router)

            # Verifying RIB routes
            command = "show ip ospf route"

            found_routes = []
            missing_routes = []

            if ("static_routes" in input_dict[routerInput]
                    or "prefix" in input_dict[routerInput]):
                if "prefix" in input_dict[routerInput]:
                    static_routes = input_dict[routerInput]["prefix"]
                else:
                    static_routes = input_dict[routerInput]["static_routes"]

                for static_route in static_routes:
                    cmd = "{}".format(command)

                    cmd = "{} json".format(cmd)

                    ospf_rib_json = run_frr_cmd(rnode, cmd, isjson=True)

                    # Verifying output dictionary ospf_rib_json is not empty
                    if bool(ospf_rib_json) is False:
                        errormsg = ("[DUT: {}] No routes found in OSPF route "
                                    "table".format(router))
                        return errormsg

                    network = static_route["network"]
                    no_of_ip = static_route.setdefault("no_of_ip", 1)
                    _tag = static_route.setdefault("tag", None)
                    _rtype = static_route.setdefault("routeType", None)

                    # Generating IPs for verification
                    ip_list = generate_ips(network, no_of_ip)
                    st_found = False
                    nh_found = False

                    for st_rt in ip_list:
                        st_rt = str(ipaddr.IPNetwork(frr_unicode(st_rt)))

                        _addr_type = validate_ip_address(st_rt)
                        if _addr_type != "ipv4":
                            continue

                        if st_rt in ospf_rib_json:
                            st_found = True
                            found_routes.append(st_rt)

                            if fib and next_hop:
                                if type(next_hop) is not list:
                                    next_hop = [next_hop]

                                for mnh in range(0, len(ospf_rib_json[st_rt])):
                                    if ("fib" in ospf_rib_json[st_rt][mnh]
                                        ["nexthops"][0]):
                                        found_hops.append([
                                            rib_r["ip"]
                                            for rib_r in ospf_rib_json[st_rt]
                                            [mnh]["nexthops"]
                                        ])

                                if found_hops[0]:
                                    missing_list_of_nexthops = set(
                                        found_hops[0]).difference(next_hop)
                                    additional_nexthops_in_required_nhs = set(
                                        next_hop).difference(found_hops[0])

                                    if additional_nexthops_in_required_nhs:
                                        logger.info(
                                            "Nexthop "
                                            "%s is not active for route %s in "
                                            "RIB of router %s\n",
                                            additional_nexthops_in_required_nhs,
                                            st_rt,
                                            dut,
                                        )
                                        errormsg = (
                                            "Nexthop {} is not active"
                                            " for route {} in RIB of router"
                                            " {}\n".format(
                                                additional_nexthops_in_required_nhs,
                                                st_rt,
                                                dut,
                                            ))
                                        return errormsg
                                    else:
                                        nh_found = True

                            elif next_hop and fib is None:
                                if type(next_hop) is not list:
                                    next_hop = [next_hop]
                                found_hops = [
                                    rib_r["ip"] for rib_r in
                                    ospf_rib_json[st_rt]["nexthops"]
                                ]

                                if found_hops:
                                    missing_list_of_nexthops = set(
                                        found_hops).difference(next_hop)
                                    additional_nexthops_in_required_nhs = set(
                                        next_hop).difference(found_hops)

                                    if additional_nexthops_in_required_nhs:
                                        logger.info(
                                            "Missing nexthop %s for route"
                                            " %s in RIB of router %s\n",
                                            additional_nexthops_in_required_nhs,
                                            st_rt,
                                            dut,
                                        )
                                        errormsg = (
                                            "Nexthop {} is Missing for "
                                            "route {} in RIB of router {}\n".
                                            format(
                                                additional_nexthops_in_required_nhs,
                                                st_rt,
                                                dut,
                                            ))
                                        return errormsg
                                    else:
                                        nh_found = True
                            if _rtype:
                                if "routeType" not in ospf_rib_json[st_rt]:
                                    errormsg = (
                                        "[DUT: {}]: routeType missing"
                                        " for route {} in OSPF RIB \n".format(
                                            dut, st_rt))
                                    return errormsg
                                elif _rtype != ospf_rib_json[st_rt][
                                        "routeType"]:
                                    errormsg = (
                                        "[DUT: {}]: routeType mismatch"
                                        " for route {} in OSPF RIB \n".format(
                                            dut, st_rt))
                                    return errormsg
                                else:
                                    logger.info("[DUT: {}]: Found routeType {}"
                                                " for route {}".format(
                                                    dut, _rtype, st_rt))
                            if tag:
                                if "tag" not in ospf_rib_json[st_rt]:
                                    errormsg = ("[DUT: {}]: tag is not"
                                                " present for"
                                                " route {} in RIB \n".format(
                                                    dut, st_rt))
                                    return errormsg

                                if _tag != ospf_rib_json[st_rt]["tag"]:
                                    errormsg = ("[DUT: {}]: tag value {}"
                                                " is not matched for"
                                                " route {} in RIB \n".format(
                                                    dut,
                                                    _tag,
                                                    st_rt,
                                                ))
                                    return errormsg

                            if metric is not None:
                                if "type2cost" not in ospf_rib_json[st_rt]:
                                    errormsg = ("[DUT: {}]: metric is"
                                                " not present for"
                                                " route {} in RIB \n".format(
                                                    dut, st_rt))
                                    return errormsg

                                if metric != ospf_rib_json[st_rt]["type2cost"]:
                                    errormsg = ("[DUT: {}]: metric value "
                                                "{} is not matched for "
                                                "route {} in RIB \n".format(
                                                    dut,
                                                    metric,
                                                    st_rt,
                                                ))
                                    return errormsg

                        else:
                            missing_routes.append(st_rt)

                if nh_found:
                    logger.info("[DUT: {}]: Found next_hop {} for all OSPF"
                                " routes in RIB".format(router, next_hop))

                if len(missing_routes) > 0:
                    errormsg = "[DUT: {}]: Missing route in RIB, " "routes: {}".format(
                        dut, missing_routes)
                    return errormsg

                if found_routes:
                    logger.info(
                        "[DUT: %s]: Verified routes in RIB, found"
                        " routes are: %s\n",
                        dut,
                        found_routes,
                    )
                    result = True

    logger.info("Exiting lib API: verify_ospf_rib()")
    return result
Пример #2
0
def verify_stale_routes_list(tgen, addr_type, dut, input_dict):
    """
    This API is use verify Stale routes on refering the network with next hop value
    Parameters
    ----------
    * `tgen`: topogen object
    * `dut`: input dut router name
    * `addr_type` : ip type ipv4/ipv6
    * `input_dict` : input dict, has details of static routes
    Usage
    -----
    dut = 'r1'
    input_dict = {
                "r3": {
                    "static_routes": [

                        {
                            "network": [NETWORK1_1[addr_type]],
                            "no_of_ip": 2,
                            "vrf": "RED"
                        }
                    ]
                }
            }

    result = verify_stale_routes_list(tgen, addr_type, dut, input_dict)
    Returns
    -------
    errormsg(str) or True
    """
    logger.debug("Entering lib API: verify_stale_routes_list()")
    router_list = tgen.routers()
    additional_nexthops_in_required_nhs = []
    list1 = []
    list2 = []
    found_hops = []
    for routerInput in input_dict.keys():
        for router, rnode in router_list.items():
            if router != dut:
                continue
            # Verifying RIB routes
            command = "show bgp"
            # Static routes
            sleep(2)
            logger.info('Checking router {} BGP RIB:'.format(dut))
            if 'static_routes' in input_dict[routerInput]:
                static_routes = input_dict[routerInput]["static_routes"]
                for static_route in static_routes:
                    found_routes = []
                    missing_routes = []
                    st_found = False
                    nh_found = False
                    vrf = static_route.setdefault("vrf", None)
                    community = static_route.setdefault("community", None)
                    largeCommunity = \
                        static_route.setdefault("largeCommunity", None)
                    if vrf:
                        cmd = "{} vrf {} {}".\
                            format(command, vrf, addr_type)
                        if community:
                            cmd = "{} community {}".\
                                format(cmd, community)
                        if largeCommunity:
                            cmd = "{} large-community {}".\
                                format(cmd, largeCommunity)
                    else:
                        cmd = "{} {}".\
                            format(command, addr_type)
                    cmd = "{} json".format(cmd)
                    rib_routes_json = run_frr_cmd(rnode, cmd, isjson=True)
                    # Verifying output dictionary rib_routes_json is not empty
                    if bool(rib_routes_json) == False:
                        errormsg = "[DUT: {}]: No route found in rib of router". \
                            format(router)
                        return errormsg
                    elif "warning" in rib_routes_json:
                        errormsg = "[DUT: {}]: {}". \
                            format(router, rib_routes_json["warning"])
                        return errormsg
                    network = static_route["network"]
                    if "no_of_ip" in static_route:
                        no_of_ip = static_route["no_of_ip"]
                    else:
                        no_of_ip = 1
                    # Generating IPs for verification
                    ip_list = generate_ips(network, no_of_ip)

                    for st_rt in ip_list:
                        st_rt = str(ipaddress.ip_network(st_rt))
                        _addr_type = validate_ip_address(st_rt)
                        if _addr_type != addr_type:
                            continue
                        if st_rt in rib_routes_json["routes"]:
                            st_found = True

                            found_routes.append(st_rt)
                            for mnh in range(0, len(rib_routes_json[
                                'routes'][st_rt])):
                                found_hops.append([rib_r[
                                        "ip"] for rib_r in rib_routes_json[
                                            'routes'][st_rt][
                                                mnh]["nexthops"]])
                            return found_hops
                        else:
                            return 'error  msg - no hops found'