Skip to content

Commit d0694de

Browse files
committed
add peer as support in numbered mode + tests
1 parent aecbc16 commit d0694de

5 files changed

Lines changed: 114 additions & 6 deletions

File tree

cosmo/clients/queries/connected_devices.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ query {
1111
name
1212
device {
1313
name
14+
custom_fields
1415
__typename
1516
primary_ip4 {
1617
__typename

cosmo/netbox_types.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ def getISISIdentifier(self) -> str | None | Never:
289289
)
290290
return str(sys_id)
291291

292+
def getAssignedDeviceASN(self) -> int | None:
293+
asn: Any | None = self.getCustomFields().get("ASN")
294+
if asn:
295+
return int(asn)
296+
else:
297+
return None
298+
292299

293300
class DeviceTypeType(AbstractNetboxType):
294301
def getBasePath(self):

cosmo/routerbgpcpevisitor.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def processNumberedBGP(
5050
v6_neighbors = set()
5151

5252
t_cpe = cpe["device"]
53+
asn_constraint = {"any_as": True}
54+
assigned_asn = t_cpe.getAssignedDeviceASN()
55+
if assigned_asn:
56+
asn_constraint = {"peer_as": assigned_asn}
57+
5358
for item in iter(t_cpe):
5459
other_ipa = CpeRouterIPVisitor(other_ip_networks).accept(item)
5560
if not other_ipa:
@@ -58,9 +63,9 @@ def processNumberedBGP(
5863
v4_neighbors.add(str(other_ipa.ip))
5964
elif type(other_ipa) is IPv6Interface:
6065
v6_neighbors.add(str(other_ipa.ip))
66+
6167
if v4_neighbors:
62-
groups[f"{base_group_name}_V4"] = {
63-
"any_as": True,
68+
groups[f"{base_group_name}_V4"] = asn_constraint | {
6469
"local_address": str(own_ipv4_address.ip),
6570
"neighbors": list(map(lambda n: {"peer": n}, v4_neighbors)),
6671
"family": {
@@ -71,8 +76,7 @@ def processNumberedBGP(
7176
},
7277
}
7378
if v6_neighbors:
74-
groups[f"{base_group_name}_V6"] = {
75-
"any_as": True,
79+
groups[f"{base_group_name}_V6"] = asn_constraint | {
7680
"local_address": str(own_ipv6_address.ip),
7781
"neighbors": list(map(lambda n: {"peer": n}, v6_neighbors)),
7882
"family": {

cosmo/tests/test_case_bgpcpe.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,93 @@ device_list:
44
slug: S9600-72XC
55
id: '17799'
66
interfaces:
7+
- custom_fields:
8+
bpdufilter: false
9+
inner_tag: null
10+
outer_tag: null
11+
storm_control__broadcast: null
12+
storm_control__multicast: null
13+
storm_control__unknown_unicast: null
14+
__typename: InterfaceType
15+
description: ''
16+
enabled: true
17+
id: '191980'
18+
ip_addresses: [ ]
19+
lag: null
20+
mac_address: null
21+
mode: null
22+
mtu: null
23+
name: ifp-0/1/3
24+
tagged_vlans: [ ]
25+
tags:
26+
- name: speed:1g
27+
slug: speed1g
28+
__typename: TagType
29+
type: A_25GBASE_X_SFP28
30+
untagged_vlan: null
31+
vrf: null
32+
parent: null
33+
connected_endpoints:
34+
- name: "xe-0/1/2.3"
35+
__typename: InterfaceType
36+
device:
37+
__typename: DeviceType
38+
name: 'CPE4000'
39+
custom_fields:
40+
ASN: 65086
41+
primary_ip4: null
42+
interfaces:
43+
- ip_addresses: [ ]
44+
id: ''
45+
name: 'xe-0/1/2.0'
46+
__typename: InterfaceType
47+
- ip_addresses:
48+
- address: 10.129.6.12/29
49+
__typename: IPAddressType
50+
- address: 2a0e:b941:3::21/122
51+
__typename: IPAddressType
52+
id: ''
53+
name: 'xe-0/1/2.3'
54+
__typename: InterfaceType
55+
- ip_addresses: []
56+
id: ''
57+
name: 'mgmt0'
58+
__typename: InterfaceType
59+
- custom_fields:
60+
bpdufilter: false
61+
inner_tag: null
62+
storm_control__broadcast: null
63+
storm_control__multicast: null
64+
storm_control__unknown_unicast: null
65+
__typename: InterfaceType
66+
description: ''
67+
enabled: true
68+
id: '191990'
69+
ip_addresses:
70+
- address: 10.129.6.11/29
71+
__typename: IPAddressType
72+
lag: null
73+
mac_address: null
74+
mode: ACCESS
75+
mtu: 1500
76+
name: ifp-0/1/3.3
77+
tagged_vlans: []
78+
tags:
79+
- name: bgp:cpe
80+
slug: bgpcpe
81+
__typename: TagType
82+
type: VIRTUAL
83+
untagged_vlan:
84+
__typename: VLANType
85+
id: '1003'
86+
name: TEST-VLAN3
87+
vid: 3
88+
vrf: null
89+
parent:
90+
__typename: InterfaceType
91+
id: '191980'
92+
name: "ifp-0/1/3"
93+
mtu: null
794
- custom_fields:
895
bpdufilter: false
996
inner_tag: null

cosmo/tests/test_serializer.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,17 +641,20 @@ def test_router_case_local_bgpcpe():
641641
assert 3 in d["interfaces"]["ifp-0/1/2"]["units"]
642642
assert 4 in d["interfaces"]["ifp-0/1/2"]["units"]
643643
assert 5 in d["interfaces"]["ifp-0/1/2"]["units"]
644+
assert "ifp-0/1/3" in d["interfaces"]
645+
assert 3 in d["interfaces"]["ifp-0/1/3"]["units"]
644646
assert "lo-0/0/0" in d["interfaces"]
645-
assert len(d["interfaces"]) == 2
647+
assert len(d["interfaces"]) == 3
646648

647649
assert "protocols" in d["routing_instances"]["default"]
648650
assert "bgp" in d["routing_instances"]["default"]["protocols"]
649651

650652
groups_default = d["routing_instances"]["default"]["protocols"]["bgp"]["groups"]
651-
assert len(groups_default) == 1
653+
assert len(groups_default) == 2
652654
assert (
653655
"CUST_cl390287" in groups_default
654656
) # parent interface has tobago line attached
657+
assert groups_default["CUST_cl390287"]["any_as"] == True
655658
assert groups_default["CUST_cl390287"]["neighbors"][0]["interface"] == "ifp-0/1/2.3"
656659
assert groups_default["CUST_cl390287"]["family"]["ipv4_unicast"]["policy"][
657660
"export"
@@ -666,6 +669,12 @@ def test_router_case_local_bgpcpe():
666669
"import_list"
667670
] == ["2a0e:b941:2:42::/64", "2a0e:b941:2::/122"]
668671

672+
assert (
673+
"CPE_ifp-0-1-3-3_V4" in groups_default
674+
) # no tobago line attached, so no new naming
675+
assert groups_default["CPE_ifp-0-1-3-3_V4"]["peer_as"] == 65086
676+
assert groups_default["CPE_ifp-0-1-3-3_V4"]["neighbors"][0]["peer"] == "10.129.6.12"
677+
669678
groups_L3VPN = d["routing_instances"]["L3VPN"]["protocols"]["bgp"]["groups"]
670679

671680
assert "CPE_ifp-0-1-2-4" in groups_L3VPN # sub interface using legacy naming tag

0 commit comments

Comments
 (0)