Add get_by_lcp_name() plus tests. Correct behavior of sub-ints without explicit encap: they are exact-match
This commit is contained in:
		@@ -63,6 +63,22 @@ def get_parent_by_name(yaml, ifname):
 | 
				
			|||||||
    return None,None
 | 
					    return None,None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def get_by_lcp_name(yaml, lcpname):
 | 
				
			||||||
 | 
					    """ Returns the interface or sub-interface by a given lcp name, or None,None if it does not exist """
 | 
				
			||||||
 | 
					    if not 'interfaces' in yaml:
 | 
				
			||||||
 | 
					        return None,None
 | 
				
			||||||
 | 
					    for ifname, iface in yaml['interfaces'].items():
 | 
				
			||||||
 | 
					        if 'lcp' in iface and iface['lcp'] == lcpname:
 | 
				
			||||||
 | 
					            return ifname, iface
 | 
				
			||||||
 | 
					        if not 'sub-interfaces' in iface:
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        for subid, sub_iface in yaml['interfaces'][ifname]['sub-interfaces'].items():
 | 
				
			||||||
 | 
					            sub_ifname = "%s.%d" % (ifname, subid)
 | 
				
			||||||
 | 
					            if 'lcp' in sub_iface and sub_iface['lcp'] == lcpname:
 | 
				
			||||||
 | 
					                return sub_ifname, sub_iface
 | 
				
			||||||
 | 
					    return None,None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def get_by_name(yaml, ifname):
 | 
					def get_by_name(yaml, ifname):
 | 
				
			||||||
    """ Returns the interface or sub-interface by a given name, or None,None if it does not exist """
 | 
					    """ Returns the interface or sub-interface by a given name, or None,None if it does not exist """
 | 
				
			||||||
    if '.' in ifname:
 | 
					    if '.' in ifname:
 | 
				
			||||||
@@ -217,6 +233,7 @@ def get_encapsulation(yaml, ifname):
 | 
				
			|||||||
    exact_match = False
 | 
					    exact_match = False
 | 
				
			||||||
    if not 'encapsulation' in iface:
 | 
					    if not 'encapsulation' in iface:
 | 
				
			||||||
        dot1q = int(subid)
 | 
					        dot1q = int(subid)
 | 
				
			||||||
 | 
					        exact_match = True
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        if 'dot1q' in iface['encapsulation']:
 | 
					        if 'dot1q' in iface['encapsulation']:
 | 
				
			||||||
            dot1q = iface['encapsulation']['dot1q']
 | 
					            dot1q = iface['encapsulation']['dot1q']
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -136,6 +136,16 @@ class TestInterfaceMethods(unittest.TestCase):
 | 
				
			|||||||
        self.assertTrue(interface.is_l3(self.cfg, "GigabitEthernet1/0/0"))
 | 
					        self.assertTrue(interface.is_l3(self.cfg, "GigabitEthernet1/0/0"))
 | 
				
			||||||
        self.assertFalse(interface.is_l3(self.cfg, "GigabitEthernet3/0/0"))
 | 
					        self.assertFalse(interface.is_l3(self.cfg, "GigabitEthernet3/0/0"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_get_by_lcp_name(self):
 | 
				
			||||||
 | 
					        ifname, iface = interface.get_by_lcp_name(self.cfg, "notexist")
 | 
				
			||||||
 | 
					        self.assertIsNone(ifname)
 | 
				
			||||||
 | 
					        self.assertIsNone(iface)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        ifname, iface = interface.get_by_lcp_name(self.cfg, "e1.100.100")
 | 
				
			||||||
 | 
					        self.assertEqual(ifname, "GigabitEthernet1/0/1.102")
 | 
				
			||||||
 | 
					        ifname, iface = interface.get_by_lcp_name(self.cfg, "e2")
 | 
				
			||||||
 | 
					        self.assertEqual(ifname, "GigabitEthernet2/0/0")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_get_by_name(self):
 | 
					    def test_get_by_name(self):
 | 
				
			||||||
        ifname, iface = interface.get_by_name(self.cfg, "GigabitEthernet1/0/1.201")
 | 
					        ifname, iface = interface.get_by_name(self.cfg, "GigabitEthernet1/0/1.201")
 | 
				
			||||||
        self.assertEqual(ifname, "GigabitEthernet1/0/1.201")
 | 
					        self.assertEqual(ifname, "GigabitEthernet1/0/1.201")
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ interfaces:
 | 
				
			|||||||
        description: "This sub-int is has the same encap as 102"
 | 
					        description: "This sub-int is has the same encap as 102"
 | 
				
			||||||
        encapsulation:
 | 
					        encapsulation:
 | 
				
			||||||
          dot1q: 102
 | 
					          dot1q: 102
 | 
				
			||||||
 | 
					          exact-match: True
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  GigabitEthernet1/0/1:
 | 
					  GigabitEthernet1/0/1:
 | 
				
			||||||
    mtu: 9216
 | 
					    mtu: 9216
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,7 +3,7 @@ test:
 | 
				
			|||||||
  errors:
 | 
					  errors:
 | 
				
			||||||
    expected:
 | 
					    expected:
 | 
				
			||||||
     - "sub-interface .*.100 does not have unique encapsulation"
 | 
					     - "sub-interface .*.100 does not have unique encapsulation"
 | 
				
			||||||
     - "sub-interface .*.101 does not have unique encapsulation"
 | 
					     - "sub-interface .*.102 does not have unique encapsulation"
 | 
				
			||||||
    count: 2
 | 
					    count: 2
 | 
				
			||||||
---
 | 
					---
 | 
				
			||||||
interfaces:
 | 
					interfaces:
 | 
				
			||||||
@@ -12,7 +12,11 @@ interfaces:
 | 
				
			|||||||
      100:
 | 
					      100:
 | 
				
			||||||
        description: "VLAN 100"
 | 
					        description: "VLAN 100"
 | 
				
			||||||
      101:
 | 
					      101:
 | 
				
			||||||
        description: "Another VLAN 100"
 | 
					        description: "Another VLAN 100, but without exact-match"
 | 
				
			||||||
        encapsulation:
 | 
					        encapsulation:
 | 
				
			||||||
          dot1q: 100
 | 
					          dot1q: 100
 | 
				
			||||||
 | 
					      102:
 | 
				
			||||||
 | 
					        description: "Another VLAN 100, but without exact-match"
 | 
				
			||||||
 | 
					        encapsulation:
 | 
				
			||||||
 | 
					          dot1q: 100
 | 
				
			||||||
 | 
					          exact-match: True
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user