Skip to content

Commit

Permalink
Update get_bgp_config to clarify return values (#1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
bewing committed Mar 28, 2023
1 parent 8b46f08 commit 8ecc4de
Show file tree
Hide file tree
Showing 22 changed files with 495 additions and 95 deletions.
3 changes: 2 additions & 1 deletion napalm/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ def get_bgp_config(
:param neighbor: Returns the configuration of a specific BGP neighbor.
Main dictionary keys represent the group name and the values represent a dictionary having
the keys below. Neighbors which aren't members of a group will be stored in a key named "_":
the keys below. A default group named "_" will contain information regarding global
settings and any neighbors that are not members of a group.
* type (string)
* description (string)
Expand Down
2 changes: 2 additions & 0 deletions napalm/eos/eos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,8 @@ def parse_options(options, default_value=False):
bgp_config[group_name] = default_group_dict(local_as)
bgp_config[group_name].update(parse_options(options, default_value))

bgp_config["_"] = default_group_dict(local_as)

for peer, peer_details in bgp_neighbors.items():
peer_group = peer_details.pop("__group", None)
if not peer_group:
Expand Down
37 changes: 19 additions & 18 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,9 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
r" update-source (\w+)", neighbor_config
)
local_as = napalm.base.helpers.regex_find_txt(
r"local-as (\d+)", neighbor_config, default=0
r"local-as (\d+)",
neighbor_config,
default=bgp_asn,
)
password = napalm.base.helpers.regex_find_txt(
r"password (?:[0-9] )?([^\']+\')", neighbor_config
Expand Down Expand Up @@ -1565,29 +1567,28 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
"route_reflector_client": route_reflector_client,
}

bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": bgp_asn,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("_", {}),
}
# Get the peer-group level config for each group
for group_name in bgp_group_neighbors.keys():
# If a group is passed in params, only continue on that group
if group:
if group_name != group:
continue
# Default no group
if group_name == "_":
bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": 0,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("_", {}),
}
continue
neighbor_config = napalm.base.helpers.netutils_parse_objects(
group_name, bgp_config_list
Expand All @@ -1609,7 +1610,7 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
r" description ([^\']+)\'", neighbor_config
)
local_as = napalm.base.helpers.regex_find_txt(
r"local-as (\d+)", neighbor_config, default=0
r"local-as (\d+)", neighbor_config, default=bgp_asn
)
import_policy = napalm.base.helpers.regex_find_txt(
r"route-map ([^\s]+) in", neighbor_config
Expand Down
47 changes: 29 additions & 18 deletions napalm/iosxr/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,15 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
<InstanceName>default</InstanceName></Naming></Instance></BGP></Configuration></Get>"
result_tree = ETREE.fromstring(self.device.make_rpc_call(rpc_command))

bgp_asn = napalm.base.helpers.convert(
int,
napalm.base.helpers.find_txt(
result_tree,
"Get/Configuration/BGP/Instance[1]/InstanceAS/FourByteAS/Naming/AS",
),
0,
)

if not group:
neighbor = ""

Expand All @@ -1010,7 +1019,9 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
int, napalm.base.helpers.find_txt(bgp_neighbor, "RemoteAS/AS_YY"), 0
)
local_as = napalm.base.helpers.convert(
int, napalm.base.helpers.find_txt(bgp_neighbor, "LocalAS/AS_YY"), 0
int,
napalm.base.helpers.find_txt(bgp_neighbor, "LocalAS/AS_YY"),
bgp_asn,
)
af_table = napalm.base.helpers.find_txt(
bgp_neighbor, "NeighborAFTable/NeighborAF/Naming/AFName"
Expand Down Expand Up @@ -1102,7 +1113,7 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
int, napalm.base.helpers.find_txt(bgp_group, "RemoteAS/AS_YY"), 0
)
local_as = napalm.base.helpers.convert(
int, napalm.base.helpers.find_txt(bgp_group, "LocalAS/AS_YY"), 0
int, napalm.base.helpers.find_txt(bgp_group, "LocalAS/AS_YY"), bgp_asn
)
multihop_ttl = napalm.base.helpers.convert(
int,
Expand Down Expand Up @@ -1164,22 +1175,22 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
}
if group and group == group_name:
break
if "" in bgp_group_neighbors.keys():
bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": 0,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("", {}),
}

bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": bgp_asn,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("", {}),
}

return bgp_config

Expand Down
46 changes: 28 additions & 18 deletions napalm/iosxr_netconf/iosxr_netconf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,16 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
if not group:
neighbor = ""

bgp_asn = napalm.base.helpers.convert(
int,
self._find_txt(
result_tree,
".//bgpc:bgp/bgpc:instance/bgpc:instance-as/bgpc:four-byte-as/bgpc:as",
default=0,
namespaces=C.NS,
),
)

bgp_group_neighbors = {}
bgp_neighbor_xpath = ".//bgpc:bgp/bgpc:instance/bgpc:instance-as/\
bgpc:four-byte-as/bgpc:default-vrf/bgpc:bgp-entity/bgpc:neighbors/bgpc:neighbor"
Expand Down Expand Up @@ -1430,7 +1440,7 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
),
0,
)
local_as = local_as_x * 65536 + local_as_y
local_as = (local_as_x * 65536 + local_as_y) or bgp_asn
af_table = self._find_txt(
bgp_neighbor,
"./bgpc:neighbor-afs/bgpc:neighbor-af/bgpc:af-name",
Expand Down Expand Up @@ -1597,7 +1607,7 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
),
0,
)
local_as = local_as_x * 65536 + local_as_y
local_as = (local_as_x * 65536 + local_as_y) or bgp_asn
multihop_ttl = napalm.base.helpers.convert(
int,
self._find_txt(
Expand Down Expand Up @@ -1679,22 +1689,22 @@ def build_prefix_limit(af_table, limit, prefix_percent, prefix_timeout):
}
if group and group == group_name:
break
if "" in bgp_group_neighbors.keys():
bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": 0,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("", {}),
}

bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": bgp_asn,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": bgp_group_neighbors.get("", {}),
}

return bgp_config

Expand Down
24 changes: 24 additions & 0 deletions napalm/junos/junos.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,30 @@ def build_prefix_limit(**args):

bgp_config = {}

routing_options = junos_views.junos_routing_config_table(self.device)
routing_options.get(options=self.junos_config_options)

bgp_asn = int(
routing_options.xml.find(
"./routing-options/autonomous-system/as-number"
).text
)

bgp_config["_"] = {
"apply_groups": [],
"description": "",
"local_as": bgp_asn,
"type": "",
"import_policy": "",
"export_policy": "",
"local_address": "",
"multipath": False,
"multihop_ttl": 0,
"remote_as": 0,
"remove_private_as": False,
"prefix_limit": {},
"neighbors": {},
}
if group:
bgp = junos_views.junos_bgp_config_group_table(self.device)
bgp.get(group=group, options=self.junos_config_options)
Expand Down
9 changes: 9 additions & 0 deletions napalm/junos/utils/junos_views.yml
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,15 @@ junos_bgp_config_peers_view:
inet6_flow_teardown_timeout_prefix_limit: {family/inet6/flow/prefix-limit/teardown/idle-timeout/timeout: int}
inet6_flow_novalidate_prefix_limit: {family/inet6/flow/prefix-limit/no-validate: unicode}

junos_routing_config_table:
get: "routing-options/autonomous-system"
view: junos_routing_config_view

junos_routing_config_view:
fields:
local_system_as: autonomous-system


####
#### BGP Neighbors and Routing Tables Stats
####
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"_": {
"type": "",
"multipath": false,
"apply_groups": [],
"remove_private_as": false,
"multihop_ttl": 0,
"remote_as": 0,
"local_address": "",
"local_as": 64496,
"description": "",
"import_policy": "",
"export_policy": "",
"prefix_limit": {},
"neighbors": {}
},
"IPv6-PEERS-GROUP-NAME": {
"type": "",
"multipath": false,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{
"_": {
"type": "",
"multipath": false,
"apply_groups": [],
"remove_private_as": false,
"multihop_ttl": 0,
"remote_as": 0,
"local_address": "",
"local_as": 4266524237,
"description": "",
"import_policy": "",
"export_policy": "",
"prefix_limit": {},
"neighbors": {}
},
"IPv4-PEERS-GROUP-NAME": {
"type": "",
"multipath": false,
Expand Down

0 comments on commit 8ecc4de

Please sign in to comment.