Beispiel #1
0
def test_bgp_route_str():
    bgp_route = BgpRoute(
        network="A",
        asPath=[1, 2],
        communities=[1, 2, 3],
        localPreference=4,
        metric=5,
        nextHopIp="2.2.2.2",
        originatorIp="1.1.1.1",
        originType="egp",
        protocol="bgp",
        sourceProtocol="connected",
        tag=23,
        weight=42,
    )
    lines = bgp_route._repr_html_lines()
    assert lines == [
        "Network: A",
        "AS Path: [1, 2]",
        "Communities: [1, 2, 3]",
        "Local Preference: 4",
        "Metric: 5",
        "Next Hop IP: 2.2.2.2",
        "Originator IP: 1.1.1.1",
        "Origin Type: egp",
        "Protocol: bgp",
        "Source Protocol: connected",
        "Tag: 23",
        "Weight: 42",
    ]
Beispiel #2
0
def testBgpRouteSerialization():
    network = "0.0.0.0/0"
    asPath = [1, 2, 3]
    communities = [4, 5, 6]
    localPreference = 1
    metric = 2
    originType = "egp"
    originatorIp = "1.1.1.1"
    protocol = "bgp"

    bgpRoute = BgpRoute(
        network=network,
        asPath=asPath,
        communities=communities,
        localPreference=localPreference,
        metric=metric,
        originatorIp=originatorIp,
        originType=originType,
        protocol=protocol,
    )

    dct = bgpRoute.dict()

    assert dct["class"] == "org.batfish.datamodel.BgpRoute"
    assert dct["network"] == network
    assert dct["asPath"] == asPath
    assert dct["communities"] == communities
    assert dct["localPreference"] == localPreference
    assert dct["metric"] == metric
    assert dct["originatorIp"] == originatorIp
    assert dct["originType"] == originType
    assert dct["protocol"] == protocol
Beispiel #3
0
def testBgpRouteDeserialization():
    network = "0.0.0.0/0"
    asPath = [1, 2, 3]
    communities = [4, 5, 6]
    localPreference = 1
    metric = 2
    originType = "egp"
    originatorIp = "1.1.1.1"
    protocol = "bgp"

    dct = {
        "network": network,
        "asPath": asPath,
        "communities": communities,
        "localPreference": localPreference,
        "metric": metric,
        "originatorIp": originatorIp,
        "originType": originType,
        "protocol": protocol,
    }
    bgpRoute = BgpRoute.from_dict(dct)
    assert bgpRoute.network == network
    assert bgpRoute.communities == communities
    assert bgpRoute.localPreference == localPreference
    assert bgpRoute.metric == metric
    assert bgpRoute.originType == originType
    assert bgpRoute.originatorIp == originatorIp
    assert bgpRoute.protocol == protocol
Beispiel #4
0
def testBgpRouteSerialization():
    network = "0.0.0.0/0"
    asPath = [1, 2, 3]
    communities = [4, 5, 6]
    localPreference = 1
    metric = 2
    nextHopIp = "2.2.2.2"
    originType = "egp"
    originatorIp = "1.1.1.1"
    protocol = "bgp"
    srcProtocol = "connected"
    tag = 23
    weight = 42

    bgpRoute = BgpRoute(
        network=network,
        asPath=asPath,
        communities=communities,
        localPreference=localPreference,
        metric=metric,
        nextHopIp=nextHopIp,
        originatorIp=originatorIp,
        originType=originType,
        protocol=protocol,
        sourceProtocol=srcProtocol,
        tag=tag,
        weight=weight,
    )

    dct = bgpRoute.dict()

    assert dct["class"] == "org.batfish.datamodel.questions.BgpRoute"
    assert dct["network"] == network
    assert dct["asPath"] == asPath
    assert dct["communities"] == communities
    assert dct["localPreference"] == localPreference
    assert dct["metric"] == metric
    assert dct["nextHopIp"] == nextHopIp
    assert dct["originatorIp"] == originatorIp
    assert dct["originType"] == originType
    assert dct["protocol"] == protocol
    assert dct["srcProtocol"] == srcProtocol
    assert dct["tag"] == tag
    assert dct["weight"] == weight
Beispiel #5
0
def rowToBgpRoute(row):
    asPath = parseAsPath(row['AS_Path'])
    communities = [communityStrToInt(comm) for comm in row['Communities']]
    originType = originProtocolToOriginType(row['Origin_Protocol'])
    return BgpRoute(communities=communities,
                    asPath=asPath,
                    localPreference=row['Local_Pref'],
                    network=row['Network'],
                    protocol=row['Protocol'],
                    metric=row['Metric'],
                    originatorIp='0.0.0.0',
                    originType=originType)
Beispiel #6
0
def _parse_json_with_schema(schema, json_object):
    # type: (str, Any) -> Any
    """Process JSON object according to its schema."""
    if json_object is None:
        # Honor null/None values
        return None

    # See if it's an iterable and we need to process it
    if _is_iterable_schema(schema):
        if not isinstance(json_object, list):
            raise ValueError(
                "Got non-list value for list/set schema {schema}. Value: {value}"
                .format(schema=schema, value=json_object))
        base_schema = _get_base_schema(schema)
        return ListWrapper([
            _parse_json_with_schema(base_schema, element)
            for element in json_object
        ])

    # Handle "primitive" schemas
    if schema == "AclTrace":
        return AclTrace.from_dict(json_object)
    if schema == "FileLines":
        return FileLines.from_dict(json_object)
    if schema == "Flow":
        return Flow.from_dict(json_object)
    if schema == "FlowTrace":
        return FlowTrace.from_dict(json_object)
    if schema == "Integer" or schema == "Long":
        return int(json_object)
    if schema == "Interface":
        return Interface.from_dict(json_object)
    if schema == "Ip":
        return str(json_object)
    if schema == "Issue":
        return Issue.from_dict(json_object)
    if schema == "Node":
        return json_object["name"]
    if schema == "BgpRoute":
        return BgpRoute.from_dict(json_object)
    if schema == "BgpRouteDiffs":
        return BgpRouteDiffs.from_dict(json_object)
    if schema == "Prefix":
        return str(json_object)
    if schema == "SelfDescribing":
        return _parse_json_with_schema(json_object["schema"],
                                       json_object.get("value"))
    if schema == "String":
        return str(json_object)
    if schema == "Trace":
        return Trace.from_dict(json_object)
    return json_object
Beispiel #7
0
def testBgpRouteDeserialization():
    network = "0.0.0.0/0"
    asPath = [1, 2, 3]
    communities = [4, 5, 6]
    localPreference = 1
    metric = 2
    nextHopIp = "2.2.2.2"
    originType = "egp"
    originatorIp = "1.1.1.1"
    protocol = "bgp"
    srcProtocol = "connected"
    tag = 23
    weight = 42

    dct = {
        "network": network,
        "asPath": asPath,
        "communities": communities,
        "localPreference": localPreference,
        "metric": metric,
        "nextHopIp": nextHopIp,
        "originatorIp": originatorIp,
        "originType": originType,
        "protocol": protocol,
        "srcProtocol": srcProtocol,
        "tag": tag,
        "weight": weight,
    }
    bgpRoute = BgpRoute.from_dict(dct)
    assert bgpRoute.network == network
    assert bgpRoute.asPath == asPath
    assert bgpRoute.communities == communities
    assert bgpRoute.localPreference == localPreference
    assert bgpRoute.metric == metric
    assert bgpRoute.nextHopIp == nextHopIp
    assert bgpRoute.originType == originType
    assert bgpRoute.originatorIp == originatorIp
    assert bgpRoute.protocol == protocol
    assert bgpRoute.sourceProtocol == srcProtocol
    assert bgpRoute.tag == tag
    assert bgpRoute.weight == weight
Beispiel #8
0
def convert_ext_bgp_to_bgp_route(bf, target_node, target_peer, ext_routes):
    """Convert BF formatted external_bgp_announcements.json to BF BgpRoute objects
        for a specified target node and peer. Only works for default VRF and numbered active BGP peers.
    """

    # Algorithm:
    # loop thru external_bgp_announcements
    # find ones that have `dstNode` == target_node and `srcIp` == target_peer
    # create BgpRoute object for each announcement

    # use `network`, `asPath`, `localPreference` as is
    # map `med` to `metric`
    # convert `communities` from list of str to list of int
    # default values for `originType:egp`, `originatorIp:0.0.0.0`, `protocol:bgp`, `sourceProtocol:None`

    bgp_routes = []
    for ext_route in ext_routes:
        if ext_route['dstNode'] == target_node and ext_route[
                'srcIp'] == target_peer:
            _communities = []
            for community in ext_route['communities']:
                _communities.append(communityStrToInt(community))

            _bgpRoute = BgpRoute(network=ext_route['network'],
                                 originatorIp='0.0.0.0',
                                 originType='egp',
                                 protocol='bgp',
                                 asPath=ext_route['asPath'],
                                 communities=_communities,
                                 localPreference=ext_route['localPreference'],
                                 metric=ext_route['med'],
                                 sourceProtocol=None)

            bgp_routes.append(_bgpRoute)

    return bgp_routes