Add 'state' field to interfaces and sub-interfaces

Assert that children cannot be 'up' of their parent is 'down'. Add tests. Update user-guide.
This commit is contained in:
Pim van Pelt
2022-04-05 11:06:33 +00:00
parent 65de792e35
commit b461ef49bb
10 changed files with 59 additions and 2 deletions

View File

@ -396,6 +396,17 @@ def get_mtu(yaml, ifname):
return 1500
def get_admin_state(yaml, ifname):
""" Return True if the interface admin state should be 'up'. Return False
if it does not exist, or if it's set to 'down'. """
ifname, iface = get_by_name(yaml, ifname)
if not iface:
return False
if not 'state' in iface:
return True
return iface['state'] == 'up'
def validate_interfaces(yaml):
result = True
msgs = []
@ -410,6 +421,8 @@ def validate_interfaces(yaml):
if ifname.startswith("BondEthernet") and (None,None) == bondethernet.get_by_name(yaml, ifname):
msgs.append("interface %s does not exist in bondethernets" % ifname)
result = False
if not 'state' in iface:
iface['state'] = 'up'
iface_mtu = get_mtu(yaml, ifname)
iface_lcp = get_lcp(yaml, ifname)
@ -473,6 +486,12 @@ def validate_interfaces(yaml):
result = False
continue
if not 'state' in sub_iface:
sub_iface['state'] = 'up'
if sub_iface['state'] == 'up' and iface['state'] == 'down':
msgs.append("sub-interface %s cannot be up if parent %s is down" % (sub_ifname, ifname))
result = False
sub_mtu = get_mtu(yaml, sub_ifname)
if sub_mtu > iface_mtu:
msgs.append("sub-interface %s has MTU %d higher than parent %s MTU %d" % (sub_ifname, sub_iface['mtu'], ifname, iface_mtu))

View File

@ -51,6 +51,7 @@ interface:
addresses: list(ip_interface(),min=1,max=6,required=False)
sub-interfaces: map(include('sub-interface'),key=int(min=1,max=4294967295),required=False)
l2xc: str(required=False)
state: enum('up', 'down', required=False)
---
sub-interface:
description: str(exclude='\'"',len=64,required=False)
@ -59,6 +60,7 @@ sub-interface:
addresses: list(ip_interface(),required=False)
encapsulation: include('encapsulation',required=False)
l2xc: str(required=False)
state: enum('up', 'down', required=False)
---
encapsulation:
dot1q: int(min=1,max=4095,required=False)

View File

@ -201,3 +201,10 @@ class TestInterfaceMethods(unittest.TestCase):
def test_is_phy(self):
self.assertTrue(interface.is_phy(self.cfg, "GigabitEthernet1/0/0"))
self.assertFalse(interface.is_phy(self.cfg, "GigabitEthernet1/0/0.100"))
def test_get_admin_state(self):
self.assertFalse(interface.get_admin_state(self.cfg, "notexist"))
self.assertFalse(interface.get_admin_state(self.cfg, "GigabitEthernet2/0/0"))
self.assertTrue(interface.get_admin_state(self.cfg, "GigabitEthernet1/0/0"))
self.assertTrue(interface.get_admin_state(self.cfg, "GigabitEthernet1/0/0.101"))
self.assertFalse(interface.get_admin_state(self.cfg, "GigabitEthernet1/0/0.102"))