diff --git a/config/interface.py b/config/interface.py index d055ee3..86c3b1e 100644 --- a/config/interface.py +++ b/config/interface.py @@ -63,6 +63,22 @@ def get_parent_by_name(yaml, ifname): 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): """ Returns the interface or sub-interface by a given name, or None,None if it does not exist """ if '.' in ifname: @@ -217,6 +233,7 @@ def get_encapsulation(yaml, ifname): exact_match = False if not 'encapsulation' in iface: dot1q = int(subid) + exact_match = True else: if 'dot1q' in iface['encapsulation']: dot1q = iface['encapsulation']['dot1q'] diff --git a/config/test_interface.py b/config/test_interface.py index 5b91125..acf1f40 100644 --- a/config/test_interface.py +++ b/config/test_interface.py @@ -136,6 +136,16 @@ class TestInterfaceMethods(unittest.TestCase): self.assertTrue(interface.is_l3(self.cfg, "GigabitEthernet1/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): ifname, iface = interface.get_by_name(self.cfg, "GigabitEthernet1/0/1.201") self.assertEqual(ifname, "GigabitEthernet1/0/1.201") diff --git a/unittest/test_interface.yaml b/unittest/test_interface.yaml index 72542b7..0af3681 100644 --- a/unittest/test_interface.yaml +++ b/unittest/test_interface.yaml @@ -17,6 +17,7 @@ interfaces: description: "This sub-int is has the same encap as 102" encapsulation: dot1q: 102 + exact-match: True GigabitEthernet1/0/1: mtu: 9216 diff --git a/unittest/yaml/error-subinterface1.yaml b/unittest/yaml/error-subinterface1.yaml index 4335ef0..750b719 100644 --- a/unittest/yaml/error-subinterface1.yaml +++ b/unittest/yaml/error-subinterface1.yaml @@ -3,7 +3,7 @@ test: errors: expected: - "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 --- interfaces: @@ -12,7 +12,11 @@ interfaces: 100: description: "VLAN 100" 101: - description: "Another VLAN 100" + description: "Another VLAN 100, but without exact-match" encapsulation: dot1q: 100 - + 102: + description: "Another VLAN 100, but without exact-match" + encapsulation: + dot1q: 100 + exact-match: True