diff --git a/napalm_nxos/nxos.py b/napalm_nxos/nxos.py index 50658bd..dda8b18 100644 --- a/napalm_nxos/nxos.py +++ b/napalm_nxos/nxos.py @@ -23,7 +23,7 @@ from requests.exceptions import ConnectionError # import third party lib -from netaddr import IPAddress +from netaddr import IPAddress, IPNetwork from netaddr.core import AddrFormatError from pynxos.device import Device as NXOSDevice @@ -706,34 +706,22 @@ def get_interfaces_ip(self): for interface in ipv6_interf_table_vrf: interface_name = py23_compat.text_type(interface.get('intf-name', '')) - address = napalm_base.helpers.ip(interface.get('addr', '').split('/')[0]) - prefix = interface.get('prefix', '').split('/')[-1] - if prefix: - prefix = int(interface.get('prefix', '').split('/')[-1]) - else: - prefix = 128 - if interface_name not in interfaces_ip.keys(): - interfaces_ip[interface_name] = {} - if 'ipv6' not in interfaces_ip[interface_name].keys(): - interfaces_ip[interface_name]['ipv6'] = {} - if address not in interfaces_ip[interface_name].get('ipv6'): - interfaces_ip[interface_name]['ipv6'][address] = {} - interfaces_ip[interface_name]['ipv6'][address].update({ - 'prefix_length': prefix - }) - secondary_addresses = interface.get('TABLE_sec_addr', {}).get('ROW_sec_addr', []) - if type(secondary_addresses) is dict: - secondary_addresses = [secondary_addresses] - for secondary_address in secondary_addresses: - sec_prefix = secondary_address.get('sec-prefix', '').split('/') - secondary_address_ip = napalm_base.helpers.ip(sec_prefix[0]) - secondary_address_prefix = int(sec_prefix[-1]) + addresses = interface.get('addr', []) + if type(addresses) is not list: + addresses = [addresses] + + for address in addresses: + network = IPNetwork(address) + host = napalm_base.helpers.ip(network.ip) + prefix = network.prefixlen + if interface_name not in interfaces_ip.keys(): + interfaces_ip[interface_name] = {} if 'ipv6' not in interfaces_ip[interface_name].keys(): interfaces_ip[interface_name]['ipv6'] = {} - if secondary_address_ip not in interfaces_ip[interface_name].get('ipv6'): - interfaces_ip[interface_name]['ipv6'][secondary_address_ip] = {} - interfaces_ip[interface_name]['ipv6'][secondary_address_ip].update({ - 'prefix_length': secondary_address_prefix + if host not in interfaces_ip[interface_name].get('ipv6'): + interfaces_ip[interface_name]['ipv6'][host] = {} + interfaces_ip[interface_name]['ipv6'][host].update({ + 'prefix_length': prefix }) return interfaces_ip diff --git a/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/expected_result.json b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/expected_result.json new file mode 100644 index 0000000..8bc36c7 --- /dev/null +++ b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/expected_result.json @@ -0,0 +1,12 @@ +{ + "Ethernet1/1": { + "ipv6": { + "2001:db8:800:200c::2": { + "prefix_length": 64 + }, + "2001:db8:800:200d::2": { + "prefix_length": 64 + } + } + } +} diff --git a/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ip_interface.json b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ip_interface.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ip_interface.json @@ -0,0 +1 @@ +{} diff --git a/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ipv6_interface.json b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ipv6_interface.json new file mode 100644 index 0000000..bc00f1f --- /dev/null +++ b/test/unit/mocked_data/test_get_interfaces_ip/multiple_ipv6/show_ipv6_interface.json @@ -0,0 +1,63 @@ +{ + "TABLE_vrf": { + "ROW_vrf": { + "vrf-name-out": "default" + } + }, + "TABLE_intf": { + "ROW_intf": { + "intf-name": "Ethernet1/1", + "proto-state": "up", + "link-state": "up", + "admin-state": "up", + "addr": [ + "2001:db8:800:200c::2/64", + "2001:db8:800:200d::2/64" + ], + "prefix": "2001:db8:800:200c::/64", + "linklocal-addr": "fe80::2ec2:60ff:fe7d:5725", + "linklocal-configured": "FALSE", + "mrouting-enabled": "disabled", + "mgroup-locally-joined": "TRUE", + "TABLE_maddr": { + "ROW_maddr": [ + { + "m-addr": "ff02::1:ff00:2" + }, + { + "m-addr": "ff02::2" + }, + { + "m-addr": "ff02::1" + }, + { + "m-addr": "ff02::1:ff00:2" + }, + { + "m-addr": "ff02::1:ff7d:5725" + }, + { + "m-addr": "ff02::1:ff00:0" + } + ] + }, + "mtu": "1500", + "urpf-mode": "none", + "ipv6-lstype": "none", + "stats-last-reset": "never", + "upkt-fwd": "0", + "upkt-orig": "64", + "upkt-consumed": "66", + "ubyte-fwd": "0", + "ubyte-orig": "5760", + "ubyte-consumed": "4720", + "mpkt-fwd": "0", + "mpkt-orig": "114", + "mpkt-consumed": "929", + "mbyte-fwd": "0", + "mbyte-orig": "13532", + "mbyte-consumed": "93536" + } + + } +}