diff --git a/vppcfg/config/prefixlist.py b/vppcfg/config/prefixlist.py index eff8ad9..b7cc16c 100644 --- a/vppcfg/config/prefixlist.py +++ b/vppcfg/config/prefixlist.py @@ -36,6 +36,20 @@ def get_by_name(yaml, plname): return None, None +def get_network_list(yaml, plname): + """Returns a list of 0 or more ip_network elements, that represent the members + in a prefixlist of given name. Return the empty list if the prefixlist doesn't + exist""" + ret = [] + plname, pl = get_by_name(yaml, plname) + if not pl: + return ret + for m in pl["members"]: + ipn = ipaddress.ip_network(m, strict=False) + ret.append(ipn) + return ret + + def count(yaml, plname): """Return the number of IPv4 and IPv6 entries in the prefixlist. Returns 0, 0 if it doesn't exist""" diff --git a/vppcfg/config/test_prefixlist.py b/vppcfg/config/test_prefixlist.py index 5b1eab0..fc41a2b 100644 --- a/vppcfg/config/test_prefixlist.py +++ b/vppcfg/config/test_prefixlist.py @@ -75,3 +75,12 @@ class TestACLMethods(unittest.TestCase): self.assertFalse(prefixlist.is_empty(self.cfg, "trusted")) self.assertTrue(prefixlist.is_empty(self.cfg, "empty")) self.assertTrue(prefixlist.is_empty(self.cfg, "pl-noexist")) + + def test_get_network_list(self): + l = prefixlist.get_network_list(self.cfg, "trusted") + self.assertIsInstance(l, list) + self.assertEquals(4, len(l)) + + l = prefixlist.get_network_list(self.cfg, "pl-notexist") + self.assertIsInstance(l, list) + self.assertEquals(0, len(l))