Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_as_number(self):
"""Test the as_number helper function."""
self.assertEqual(napalm.base.helpers.as_number("64001"), 64001)
self.assertEqual(napalm.base.helpers.as_number("1.0"), 65536)
self.assertEqual(napalm.base.helpers.as_number("1.100"), 65636)
self.assertEqual(napalm.base.helpers.as_number("1.65535"), 131071)
self.assertEqual(napalm.base.helpers.as_number("65535.65535"), 4294967295)
self.assertEqual(napalm.base.helpers.as_number(64001), 64001)
if match:
bgp_summary_dict.update(match.groupdict(1))
# Some post regex cleanup and validation
vrf = bgp_summary_dict["vrf"]
if vrf.lower() == "default":
bgp_summary_dict["vrf"] = "global"
afi = bgp_summary_dict["afi"]
afi = afi.split()[0].lower()
if afi not in allowed_afi:
raise ValueError("AFI ({}) is invalid and not supported.".format(afi))
bgp_summary_dict["afi"] = afi
local_as = bgp_summary_dict["local_as"]
local_as = helpers.as_number(local_as)
match = re.search(IPV4_ADDR_REGEX, bgp_summary_dict["router_id"])
if not match:
raise ValueError(
"BGP router_id ({}) is not valid".format(bgp_summary_dict["router_id"])
)
vrf = bgp_summary_dict["vrf"]
bgp_return_dict = {vrf: {"router_id": bgp_summary_dict["router_id"], "peers": {}}}
# Extract and process the tabular data
tabular_divider = r"^Neighbor\s+.*PfxRcd$"
tabular_data = re.split(tabular_divider, bgp_summary, flags=re.M)
if len(tabular_data) != 2:
msg = "Unexpected data processing BGP summary information:\n\n{}".format(
bgp_summary
if len(commsplit) == 2:
for line in commsplit[1].split("\n")[1:]:
# RT:65004:22
matchcommun = RE_BGP_COMMUN.match(line)
if matchcommun:
extcomm.append(matchcommun.group(1))
else:
# we've reached end of the extended community section
break
bothcomm = stdcomm + extcomm
bgp_attr = {
"as_path": search_re_dict["aspath"]["result"].strip(),
"remote_address": search_re_dict["bgpfrom"]["result"],
"local_preference": int(search_re_dict["bgplp"]["result"]),
"communities": bothcomm,
"local_as": helpers.as_number(bgpas),
}
if bgpras:
bgp_attr["remote_as"] = helpers.as_number(bgpras)
else:
bgp_attr["remote_as"] = 0 # 0? , locally sourced
return bgp_attr
"output_messages": napalm.base.helpers.as_number(
bgp_neigh["msg_total_out"]
)
if bgp_neigh["msg_total_out"]
else 0,
"input_updates": napalm.base.helpers.as_number(
bgp_neigh["msg_update_in"]
)
if bgp_neigh["msg_update_in"]
else 0,
"output_updates": napalm.base.helpers.as_number(
bgp_neigh["msg_update_out"]
)
if bgp_neigh["msg_update_out"]
else 0,
"messages_queued_out": napalm.base.helpers.as_number(neigh["out_q"]),
"connection_state": bgp_neigh["bgp_state"],
"previous_connection_state": "",
"last_event": "",
"suppress_4byte_as": (
bgp_neigh["four_byte_as"] != "advertised and received"
if bgp_neigh["four_byte_as"]
else False
),
"local_as_prepend": False,
"holdtime": napalm.base.helpers.as_number(bgp_neigh["holdtime"])
if bgp_neigh["holdtime"]
else 0,
"configured_holdtime": 0,
"keepalive": napalm.base.helpers.as_number(bgp_neigh["keepalive"])
if bgp_neigh["keepalive"]
else 0,
"up": neigh["up"] != "never",
"local_as": napalm.base.helpers.as_number(neigh["local_as"]),
"remote_as": napalm.base.helpers.as_number(neigh["remote_as"]),
"router_id": napalm.base.helpers.ip(bgp_neigh["router_id"])
if bgp_neigh["router_id"]
else "",
"local_address": napalm.base.helpers.ip(bgp_neigh["local_address"])
if bgp_neigh["local_address"]
else "",
"local_address_configured": False,
"local_port": napalm.base.helpers.as_number(bgp_neigh["local_port"])
if bgp_neigh["local_port"]
else 0,
"routing_table": bgp_neigh["vrf"] if bgp_neigh["vrf"] else "global",
"remote_address": napalm.base.helpers.ip(bgp_neigh["neighbor"]),
"remote_port": napalm.base.helpers.as_number(bgp_neigh["remote_port"])
if bgp_neigh["remote_port"]
else 0,
"multihop": False,
"multipath": False,
"remove_private_as": False,
"import_policy": "",
"export_policy": "",
"input_messages": napalm.base.helpers.as_number(
bgp_neigh["msg_total_in"]
)
if bgp_neigh["msg_total_in"]
else 0,
"output_messages": napalm.base.helpers.as_number(
bgp_neigh["msg_total_out"]
)
if bgp_neigh["msg_total_out"]
else "",
"local_address_configured": False,
"local_port": napalm.base.helpers.as_number(bgp_neigh["local_port"])
if bgp_neigh["local_port"]
else 0,
"routing_table": bgp_neigh["vrf"] if bgp_neigh["vrf"] else "global",
"remote_address": napalm.base.helpers.ip(bgp_neigh["neighbor"]),
"remote_port": napalm.base.helpers.as_number(bgp_neigh["remote_port"])
if bgp_neigh["remote_port"]
else 0,
"multihop": False,
"multipath": False,
"remove_private_as": False,
"import_policy": "",
"export_policy": "",
"input_messages": napalm.base.helpers.as_number(
bgp_neigh["msg_total_in"]
)
if bgp_neigh["msg_total_in"]
else 0,
"output_messages": napalm.base.helpers.as_number(
bgp_neigh["msg_total_out"]
)
if bgp_neigh["msg_total_out"]
else 0,
"input_updates": napalm.base.helpers.as_number(
bgp_neigh["msg_update_in"]
)
if bgp_neigh["msg_update_in"]
else 0,
"output_updates": napalm.base.helpers.as_number(
bgp_neigh["msg_update_out"]
peer_fwd_rti = neighbor_details.pop("peer_fwd_rti", "")
instance_name = "global" if instance == "master" else instance
if instance_name not in bgp_neighbor_data:
bgp_neighbor_data[instance_name] = {}
if "router_id" not in bgp_neighbor_data[instance_name]:
# we only need to set this once
bgp_neighbor_data[instance_name]["router_id"] = str(
neighbor_details.get("local_id", "")
)
peer = {
key: self._parse_value(value)
for key, value in neighbor_details.items()
if key in keys
}
peer["local_as"] = napalm.base.helpers.as_number(peer["local_as"])
peer["remote_as"] = napalm.base.helpers.as_number(peer["remote_as"])
peer["address_family"] = self._parse_route_stats(
neighbor_details, instance
)
if "peers" not in bgp_neighbor_data[instance_name]:
bgp_neighbor_data[instance_name]["peers"] = {}
bgp_neighbor_data[instance_name]["peers"][peer_ip] = peer
if not uptime_table_items:
uptime_table_items = _get_uptime_table(instance)
for neighbor, uptime in uptime_table_items:
normalized_neighbor = napalm.base.helpers.ip(neighbor)
if (
normalized_neighbor
not in bgp_neighbor_data[instance_name]["peers"]
):
bgp_neighbor_data[instance_name]["peers"][
normalized_neighbor
sent_prefixes = -1
uptime = -1
# get description
try:
description = py23_compat.text_type(neighbor_entry["description"])
except KeyError:
description = ""
# check the remote router_id looks like an ipv4 address
remote_id = napalm.base.helpers.ip(neighbor_entry["remote_id"], version=4)
if remote_addr not in bgp_neighbor_data["global"]["peers"]:
bgp_neighbor_data["global"]["peers"][remote_addr] = {
"local_as": napalm.base.helpers.as_number(entry["local_as"]),
"remote_as": napalm.base.helpers.as_number(entry["remote_as"]),
"remote_id": remote_id,
"is_up": is_up,
"is_enabled": is_enabled,
"description": description,
"uptime": uptime,
"address_family": {
afi: {
"received_prefixes": received_prefixes,
"accepted_prefixes": accepted_prefixes,
"sent_prefixes": sent_prefixes,
}
},
}
else:
# found previous data for matching remote_addr, but for different afi
existing = bgp_neighbor_data["global"]["peers"][remote_addr]
# under a certain routing instance
peer_fwd_rti = neighbor_details.pop("peer_fwd_rti", "")
instance_name = "global" if instance == "master" else instance
if instance_name not in bgp_neighbor_data:
bgp_neighbor_data[instance_name] = {}
if "router_id" not in bgp_neighbor_data[instance_name]:
# we only need to set this once
bgp_neighbor_data[instance_name]["router_id"] = str(
neighbor_details.get("local_id", "")
)
peer = {
key: self._parse_value(value)
for key, value in neighbor_details.items()
if key in keys
}
peer["local_as"] = napalm.base.helpers.as_number(peer["local_as"])
peer["remote_as"] = napalm.base.helpers.as_number(peer["remote_as"])
peer["address_family"] = self._parse_route_stats(
neighbor_details, instance
)
if "peers" not in bgp_neighbor_data[instance_name]:
bgp_neighbor_data[instance_name]["peers"] = {}
bgp_neighbor_data[instance_name]["peers"][peer_ip] = peer
if not uptime_table_items:
uptime_table_items = _get_uptime_table(instance)
for neighbor, uptime in uptime_table_items:
normalized_neighbor = napalm.base.helpers.ip(neighbor)
if (
normalized_neighbor
not in bgp_neighbor_data[instance_name]["peers"]
):
bgp_neighbor_data[instance_name]["peers"][