Skip to content

Commit

Permalink
Merge pull request #38 from wobcom/fix/abort-on-invalid-loopbackls
Browse files Browse the repository at this point in the history
Added serialization error for invalid loopbacks
  • Loading branch information
johannwagner authored Jan 13, 2025
2 parents 9844cb0 + 8736136 commit 247805d
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 64 deletions.
13 changes: 9 additions & 4 deletions cosmo/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def _get_vrf_rib(self, routes, vrf):
rib[table]["static"][route["prefix"]["prefix"]] = r
return rib

def _get_unit(self, iface):
def _get_unit(self, iface, is_loopback=False):
unit_stub = {}
name = iface['name'].split(".")[1]

Expand All @@ -190,6 +190,10 @@ def _get_unit(self, iface):
if not iface["vrf"] and not ipa.is_global and not is_mgmt and not self.cfg.allow_private_ips:
raise InterfaceSerializationError(f"Private IP {ipa} used on interface {iface['name']} in default VRF for device {self.device['name']}. Did you forget to configure a VRF?")

# We only want /32 on loopback interfaces.
if is_loopback and not ipa.network.prefixlen == ipa.max_prefixlen:
raise InterfaceSerializationError(f"IP {ipa} is not a valid loopback IP address.")

if ipa.version == 4:
ipv4Family[ipa.network].add_ip(ipa, is_secondary)
elif ipa.version == 6:
Expand Down Expand Up @@ -473,14 +477,15 @@ def serialize(self):
self.device["interfaces"], interface["name"]
)
if len(sub_interfaces) > 0:
is_loopback = interface_stub.get("type") == "loopback"
is_parent_loopback = interface_stub.get("type") == "loopback"
is_parent_virtual = interface_stub.get("type") == "virtual"
for si in sub_interfaces:
if not si['enabled']:
continue

name, unit = self._get_unit(si)
name, unit = self._get_unit(si, is_loopback=is_parent_loopback)
sub_num = int(name or '0')
if not is_loopback and sub_num != 0 and not unit.get("vlan", None):
if not is_parent_loopback and not is_parent_virtual and sub_num != 0 and not unit.get("vlan", None):
warnings.warn(f"Sub interface {si['name']} does not have a access VLAN configured, skipping...")
continue

Expand Down
4 changes: 2 additions & 2 deletions cosmo/tests/test_case_1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: []
l2vpn_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
4 changes: 2 additions & 2 deletions cosmo/tests/test_case_2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: []
l2vpn_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
4 changes: 2 additions & 2 deletions cosmo/tests/test_case_bgpcpe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ device_list:
enabled: true
id: '192047'
ip_addresses:
- address: 45.139.136.10/24
- address: 45.139.136.10/32
lag: null
mac_address: null
mode: null
Expand Down Expand Up @@ -155,4 +155,4 @@ vrf_list:
rd: null
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
4 changes: 2 additions & 2 deletions cosmo/tests/test_case_fec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
28 changes: 4 additions & 24 deletions cosmo/tests/test_case_ips.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,6 @@ device_list:
slug: mx204
id: '1746'
interfaces:
- custom_fields:
bpdufilter: false
bpduguard: false
inner_tag: null
ipv6_ra: false
outer_tag: null
description: ''
enabled: true
id: '3646'
ip_addresses: [ ]
lag: null
mac_address: null
mode: null
mtu: null
name: lo-0/0/0
tagged_vlans: [ ]
tags: [ ]
type: LOOPBACK
untagged_vlan: null
vrf: null
- custom_fields:
inner_tag: null
outer_tag: null
Expand All @@ -40,7 +20,7 @@ device_list:
mac_address: 94:BF:41:41:41:F3
mode: null
mtu: null
name: lo-0/0/0.14
name: ifp-0/0/2.0
tagged_vlans: [ ]
tags: [ ]
type: A_100GBASE_X_QSFP28
Expand All @@ -63,7 +43,7 @@ device_list:
mac_address: 94:BF:41:41:41:F4
mode: null
mtu: null
name: lo-0/0/0.16
name: ifp-0/0/3.0
tagged_vlans: [ ]
tags: [ ]
type: A_100GBASE_X_QSFP28
Expand All @@ -75,11 +55,11 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
2 changes: 1 addition & 1 deletion cosmo/tests/test_case_l2x_err_template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ l2vpn_list: []
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
6 changes: 3 additions & 3 deletions cosmo/tests/test_case_l3vpn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ device_list:
enabled: true
id: '192047'
ip_addresses:
- address: 45.139.136.10/24
- address: 45.139.136.10/32
lag: null
mac_address: null
mode: null
Expand Down Expand Up @@ -121,9 +121,9 @@ loopback_interface_list:
- name: "lo0.0"
vrf: null
ip_addresses:
- address: 45.139.136.10/24
- address: 45.139.136.10/32
family:
value: 4
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
4 changes: 2 additions & 2 deletions cosmo/tests/test_case_lag.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: [ ]
l2vpn_list: [ ]
vrf_list: [ ]
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
2 changes: 1 addition & 1 deletion cosmo/tests/test_case_local_l2x.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ l2vpn_list:
vrf_list: []
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
8 changes: 4 additions & 4 deletions cosmo/tests/test_case_mpls_evpn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.11/24
address: 45.139.136.11/32
serial: CT702
staticroute_set: []
- device_type:
Expand Down Expand Up @@ -184,7 +184,7 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: ''
staticroute_set: []
l2vpn_list:
Expand All @@ -211,6 +211,6 @@ l2vpn_list:
vrf_list: []
loopbacks:
TEST0001:
ipv4: "45.139.136.11/24"
ipv4: "45.139.136.11/32"
TEST0002:
ipv4: "45.139.136.10/24"
ipv4: "45.139.136.10/32"
2 changes: 1 addition & 1 deletion cosmo/tests/test_case_no_manuf_slug.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ device_list:
raw: ACME
raw: coyote-0-2-1-rc1
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: []
l2vpn_list: []
Expand Down
2 changes: 1 addition & 1 deletion cosmo/tests/test_case_vendor_unknown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ device_list:
slug: ACME
slug: coyote-0-2-1-rc1
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: 4242
staticroute_set: []
l2vpn_list: []
Expand Down
6 changes: 3 additions & 3 deletions cosmo/tests/test_case_vpws.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ device_list:
slug: juniper
slug: junos-21-4r3-s5-4
primary_ip4:
address: 45.139.136.10/24
address: 45.139.136.10/32
serial: ''
staticroute_set: []
l2vpn_list:
Expand All @@ -132,6 +132,6 @@ l2vpn_list:
vrf_list: []
loopbacks:
TEST0002:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
TEST0001:
ipv4: 45.139.136.11/24
ipv4: 45.139.136.11/32
2 changes: 1 addition & 1 deletion cosmo/tests/test_case_vrf_staticroute.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,4 @@ vrf_list:
rd: null
loopbacks:
TEST0001:
ipv4: 45.139.136.10/24
ipv4: 45.139.136.10/32
23 changes: 12 additions & 11 deletions cosmo/tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,21 @@ def test_router_vrf_rib():
def test_router_ips():
[sd] = get_router_sd_from_path("./test_case_ips.yaml")

assert 'lo-0/0/0' in sd['interfaces']
assert 14 in sd['interfaces']['lo-0/0/0']['units']
assert 16 in sd['interfaces']['lo-0/0/0']['units']
assert 'ifp-0/0/2' in sd['interfaces']
assert 'ifp-0/0/3' in sd['interfaces']
assert 0 in sd['interfaces']['ifp-0/0/2']['units']
assert 0 in sd['interfaces']['ifp-0/0/3']['units']

unit_14 = sd['interfaces']['lo-0/0/0']['units'][14]
unit_16 = sd['interfaces']['lo-0/0/0']['units'][16]
unit_v4 = sd['interfaces']['ifp-0/0/2']['units'][0]
unit_v6 = sd['interfaces']['ifp-0/0/3']['units'][0]

assert unit_14['families']['inet']['address']['45.139.138.1/29'] == {}
assert unit_14['families']['inet']['address']['45.139.138.8/29'] == {"primary": True}
assert unit_14['families']['inet']['address']['45.139.138.9/29'] == {"secondary": True}
assert unit_v4['families']['inet']['address']['45.139.138.1/29'] == {}
assert unit_v4['families']['inet']['address']['45.139.138.8/29'] == {"primary": True}
assert unit_v4['families']['inet']['address']['45.139.138.9/29'] == {"secondary": True}

assert unit_16['families']['inet6']['address']['2a0e:b941:2::/122'] == {}
assert unit_16['families']['inet6']['address']['2a0e:b941:2::40/122'] == {"primary": True}
assert unit_16['families']['inet6']['address']['2a0e:b941:2::41/122'] == {"secondary": True}
assert unit_v6['families']['inet6']['address']['2a0e:b941:2::/122'] == {}
assert unit_v6['families']['inet6']['address']['2a0e:b941:2::40/122'] == {"primary": True}
assert unit_v6['families']['inet6']['address']['2a0e:b941:2::41/122'] == {"secondary": True}


def test_router_case_mpls_evpn():
Expand Down

0 comments on commit 247805d

Please sign in to comment.