Add get_lcps() to enumerate all LCP names in the system
This commit is contained in:
		@@ -23,3 +23,20 @@ interfaces:
 | 
				
			|||||||
  GigabitEthernet2/0/0:
 | 
					  GigabitEthernet2/0/0:
 | 
				
			||||||
    description: "This interface has no sub-ints"
 | 
					    description: "This interface has no sub-ints"
 | 
				
			||||||
    lcp: "e2"
 | 
					    lcp: "e2"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  GigabitEthernet2/0/1:
 | 
				
			||||||
 | 
					    description: "This LCP also on gi2/0/2"
 | 
				
			||||||
 | 
					    lcp: "twice"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  GigabitEthernet2/0/2:
 | 
				
			||||||
 | 
					    description: "This LCP also on gi2/0/1"
 | 
				
			||||||
 | 
					    lcp: "twice"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  GigabitEthernet2/0/3:
 | 
				
			||||||
 | 
					    description: "This LCP also on loop0"
 | 
				
			||||||
 | 
					    lcp: "thrice"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					loopbacks:
 | 
				
			||||||
 | 
					  loop0:
 | 
				
			||||||
 | 
					    description: "This LCP also on gi2/0/3"
 | 
				
			||||||
 | 
					    lcp: "thrice"
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,23 +13,32 @@
 | 
				
			|||||||
#
 | 
					#
 | 
				
			||||||
import logging
 | 
					import logging
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def is_unique(yaml, lcpname):
 | 
					def get_lcps(yaml):
 | 
				
			||||||
    """ Returns True if there is at most one occurence of the LCP name in the entire config."""
 | 
					    """ Returns a list of all LCPs configured in the system, or an empty list if there are none. """
 | 
				
			||||||
    ncount=0
 | 
					
 | 
				
			||||||
 | 
					    ret = []
 | 
				
			||||||
    if 'interfaces' in yaml:
 | 
					    if 'interfaces' in yaml:
 | 
				
			||||||
        for ifname, iface in yaml['interfaces'].items():
 | 
					        for ifname, iface in yaml['interfaces'].items():
 | 
				
			||||||
            if 'lcp' in iface and iface['lcp'] == lcpname:
 | 
					            if 'lcp' in iface:
 | 
				
			||||||
                ncount = ncount + 1
 | 
					                ret.append(iface['lcp'])
 | 
				
			||||||
            if 'sub-interfaces' in iface:
 | 
					            if 'sub-interfaces' in iface:
 | 
				
			||||||
                for sub_ifname, sub_iface in iface['sub-interfaces'].items():
 | 
					                for sub_ifname, sub_iface in iface['sub-interfaces'].items():
 | 
				
			||||||
                    if 'lcp' in sub_iface and sub_iface['lcp'] == lcpname:
 | 
					                    if 'lcp' in sub_iface:
 | 
				
			||||||
                        ncount = ncount + 1
 | 
					                        ret.append(sub_iface['lcp'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if 'loopbacks' in yaml:
 | 
					    if 'loopbacks' in yaml:
 | 
				
			||||||
        for ifname, iface in yaml['loopbacks'].items():
 | 
					        for ifname, iface in yaml['loopbacks'].items():
 | 
				
			||||||
            if 'lcp' in iface and iface['lcp'] == lcpname:
 | 
					            if 'lcp' in iface:
 | 
				
			||||||
                ncount = ncount + 1
 | 
					                ret.append(iface['lcp'])
 | 
				
			||||||
    if 'bridgedomains' in yaml:
 | 
					    if 'bridgedomains' in yaml:
 | 
				
			||||||
        for ifname, iface in yaml['bridgedomains'].items():
 | 
					        for ifname, iface in yaml['bridgedomains'].items():
 | 
				
			||||||
            if 'lcp' in iface and iface['lcp'] == lcpname:
 | 
					            if 'lcp' in iface:
 | 
				
			||||||
                ncount = ncount + 1
 | 
					                ret.append(iface['lcp'])
 | 
				
			||||||
    return ncount < 2
 | 
					
 | 
				
			||||||
 | 
					    return ret
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def is_unique(yaml, lcpname):
 | 
				
			||||||
 | 
					    """ Returns True if there is at most one occurence of the LCP name in the entire config."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    lcps = get_lcps(yaml)
 | 
				
			||||||
 | 
					    return lcps.count(lcpname) < 2
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -8,12 +8,19 @@ class TestLCPMethods(unittest.TestCase):
 | 
				
			|||||||
        with open("unittest/test_lcp.yaml", "r") as f:
 | 
					        with open("unittest/test_lcp.yaml", "r") as f:
 | 
				
			||||||
            self.cfg = yaml.load(f, Loader = yaml.FullLoader)
 | 
					            self.cfg = yaml.load(f, Loader = yaml.FullLoader)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_enumerators(self):
 | 
				
			||||||
 | 
					        lcps = lcp.get_lcps(self.cfg)
 | 
				
			||||||
 | 
					        self.assertIn("e1", lcps)
 | 
				
			||||||
 | 
					        self.assertIn("foo", lcps)
 | 
				
			||||||
 | 
					        self.assertIn("e2", lcps)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_lcp(self):
 | 
					    def test_lcp(self):
 | 
				
			||||||
        self.assertTrue(lcp.is_unique(self.cfg, "e1"))
 | 
					        self.assertTrue(lcp.is_unique(self.cfg, "e1"))
 | 
				
			||||||
        self.assertTrue(lcp.is_unique(self.cfg, "foo"))
 | 
					        self.assertTrue(lcp.is_unique(self.cfg, "foo"))
 | 
				
			||||||
 | 
					        self.assertTrue(lcp.is_unique(self.cfg, "notexist"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        ## TODO(pim) - ensure that is_unique also takes synthesized LCPs into account
 | 
					        self.assertFalse(lcp.is_unique(self.cfg, "twice"))
 | 
				
			||||||
        ## self.assertFalse(lcp.is_unique(self.cfg, "e1.1000"))
 | 
					        self.assertFalse(lcp.is_unique(self.cfg, "thrice"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_qinx(self):
 | 
					    def test_qinx(self):
 | 
				
			||||||
        qinx_ifname, qinx_iface = interface.get_by_name(self.cfg, "GigabitEthernet1/0/1.201")
 | 
					        qinx_ifname, qinx_iface = interface.get_by_name(self.cfg, "GigabitEthernet1/0/1.201")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user