diff --git a/ccutils/ccparser/CiscoIosParser.py b/ccutils/ccparser/CiscoIosParser.py index c118984..824e5b2 100644 --- a/ccutils/ccparser/CiscoIosParser.py +++ b/ccutils/ccparser/CiscoIosParser.py @@ -641,5 +641,126 @@ def section_unprocessed_lines(self, parent, check_patterns): unprocessed_lines.append(child) return unprocessed_lines + @property + @functools.lru_cache() + def all_ipv4_physical_addresses(self): + """ + Get all physical IPv4 addresses of device config. + + Returns: + list: List IPv4 Addresses + + Example:: + + [ + "192.168.100.2", + "192.168.101.2" + ] + + Returns empty list (``[]``) if absent + + """ + addresses = [] + for interface_line in self.interface_lines: + if "l3" in interface_line.flags: + addresses.extend([x["ip_address"] for x in interface_line.ip_addresses]) + return addresses + + @functools.lru_cache() + def vrf_ipv4_physical_addresses(self, vrf="global"): + """ + Get all physical IPv4 addresses in particular VRF. By default returns all IP addresses from global routing table + + Args: + vrf: Name of the VRF (default is `"global"`) + + Returns: + list: List IPv4 Addresses + + Example:: + + [ + "192.168.100.2", + "192.168.101.2" + ] + + Returns empty list (``[]``) if absent + + """ + addresses = [] + # Filter L3 Interfaces + interfaces = [x for x in self.interface_lines if "l3" in x.flags] + # Filter VRF interfaces + if vrf == "global": + interfaces = [x for x in interfaces if x.vrf is None] + else: + interfaces = [x for x in interfaces if x.vrf == vrf] + for interface in interfaces: + addresses.extend([x["ip_address"] for x in interface.ip_addresses]) + return addresses + + @property + @functools.lru_cache() + def all_ipv4_standby_addresses(self): + """ + + Returns: + list: List IPv4 Addresses + + Example:: + + [ + "192.168.100.1", + "192.168.101.1" + ] + + Returns empty list (``[]``) if absent + + """ + addresses = [] + # Filter L3 interfaces + interfaces = [x for x in self.interface_lines if "l3" in x.flags] + # Filter Stadnby Interfaces + interfaces = [x for x in interfaces if x.standby is not None] + for interface in interfaces: + for group in interface.standby["groups"].values(): + addresses.extend([x["ip_address"] for x in group["ip_addresses"]]) + return addresses + + @functools.lru_cache() + def vrf_ipv4_standby_addresses(self, vrf="global"): + """ + + Args: + vrf: Name of the VRF (default is `"global"`) + + Returns: + list: List IPv4 Addresses + + Example:: + + [ + "192.168.100.1", + "192.168.101.1" + ] + + Returns empty list (``[]``) if absent + + """ + addresses = [] + # Filter L3 interfaces + interfaces = [x for x in self.interface_lines if "l3" in x.flags] + # Filter Stadnby Interfaces + interfaces = [x for x in interfaces if x.standby is not None] + # Filter VRF Interfaces + if vrf == "global": + interfaces = [x for x in interfaces if x.vrf is None] + else: + interfaces = [x for x in interfaces if x.vrf == vrf] + + for interface in interfaces: + for group in interface.standby["groups"].values(): + addresses.extend([x["ip_address"] for x in group["ip_addresses"]]) + return addresses diff --git a/docs/conf.py b/docs/conf.py index b4d7cc6..efcfa7f 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -26,9 +26,9 @@ add_module_names = False # The short X.Y version -version = '0.2.11' +version = '0.2.12' # The full version, including alpha/beta/rc tags -release = '0.2.11-beta' +release = '0.2.12-beta' # -- General configuration --------------------------------------------------- diff --git a/setup.py b/setup.py index ff091b5..97e173e 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='ccutils', - version='0.2.11', + version='0.2.12', packages=find_packages(exclude=["test", "examples"]), url='https://github.org/mihudec/ccutils', license='', diff --git a/tests/resources/cisco_ios_L3_interfaces_address_test.txt b/tests/resources/cisco_ios_L3_interfaces_address_test.txt new file mode 100644 index 0000000..34c75cd --- /dev/null +++ b/tests/resources/cisco_ios_L3_interfaces_address_test.txt @@ -0,0 +1,9 @@ +interface Vlan100 + vrf forwarding TEST + ip address 192.168.100.2 255.255.255.0 + standby 100 ip 192.168.100.1 + standby 100 version 2 +interface Vlan101 + vrf forwarding TEST + ip address 192.168.101.2 255.255.255.0 + standby 101 ip 192.168.101.1 diff --git a/tests/results/cisco_ios_L3_interfaces_address_test.json b/tests/results/cisco_ios_L3_interfaces_address_test.json new file mode 100644 index 0000000..5bdd9eb --- /dev/null +++ b/tests/results/cisco_ios_L3_interfaces_address_test.json @@ -0,0 +1,18 @@ +{ + "all_ipv4_physical_addresses": [ + "192.168.100.2", + "192.168.101.2" + ], + "vrf_ipv4_physical_addresses": [ + "192.168.100.2", + "192.168.101.2" + ], + "all_ipv4_standby_addresses": [ + "192.168.100.1", + "192.168.101.1" + ], + "vrf_ipv4_standby_addresses": [ + "192.168.100.1", + "192.168.101.1" + ] +} \ No newline at end of file diff --git a/tests/test_CiscoIosParser.py b/tests/test_CiscoIosParser.py index 5278f09..d14dbbe 100644 --- a/tests/test_CiscoIosParser.py +++ b/tests/test_CiscoIosParser.py @@ -27,3 +27,43 @@ def test_vrfs(self): self.assertEqual(want, have) + def test_all_ipv4_physical_addresses(self): + test_file_base = "cisco_ios_L3_interfaces_address_test" + with self.subTest(msg=test_file_base): + config = self.get_config(test_file_name=test_file_base) + want = self.get_results(results_file_name=test_file_base)["all_ipv4_physical_addresses"] + have = config.all_ipv4_physical_addresses + jprint(have) + + self.assertEqual(want, have) + + def test_vrf_ipv4_physical_addresses(self): + test_file_base = "cisco_ios_L3_interfaces_address_test" + with self.subTest(msg=test_file_base): + config = self.get_config(test_file_name=test_file_base) + want = self.get_results(results_file_name=test_file_base)["vrf_ipv4_physical_addresses"] + have = config.vrf_ipv4_physical_addresses(vrf="TEST") + jprint(have) + + self.assertEqual(want, have) + + def test_all_ipv4_standby_addresses(self): + test_file_base = "cisco_ios_L3_interfaces_address_test" + with self.subTest(msg=test_file_base): + config = self.get_config(test_file_name=test_file_base) + want = self.get_results(results_file_name=test_file_base)["all_ipv4_standby_addresses"] + have = config.all_ipv4_standby_addresses + jprint(have) + + self.assertEqual(want, have) + + def test_vrf_ipv4_standby_addresses(self): + test_file_base = "cisco_ios_L3_interfaces_address_test" + with self.subTest(msg=test_file_base): + config = self.get_config(test_file_name=test_file_base) + want = self.get_results(results_file_name=test_file_base)["vrf_ipv4_standby_addresses"] + have = config.vrf_ipv4_standby_addresses(vrf="TEST") + jprint(have) + + self.assertEqual(want, have) +