Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds mac, ip checks for getters #1560

Merged
merged 6 commits into from
Feb 13, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions napalm/ios/ios.py
Original file line number Diff line number Diff line change
Expand Up @@ -876,11 +876,13 @@ def get_lldp_neighbors(self):
lldp[intf_name] = []
for lldp_entry in entries:
hostname = lldp_entry["remote_system_name"]
port = lldp_entry["remote_port"]
# Match IOS behaviour of taking remote chassis ID
# When lacking a system name (in show lldp neighbors)
if not hostname:
hostname = lldp_entry["remote_chassis_id"]
lldp_dict = {"port": lldp_entry["remote_port"], "hostname": hostname}
hostname = napalm.base.helpers.mac(lldp_entry["remote_chassis_id"])
port = napalm.base.helpers.mac(port)
lldp_dict = {"port": port, "hostname": hostname}
lldp[intf_name].append(lldp_dict)

return lldp
Expand All @@ -901,6 +903,16 @@ def get_lldp_neighbors_detail(self, interface=""):
if len(lldp_entries) == 0:
return {}

# format chassis_id for consistency, if it is a mac address
for entry in lldp_entries:
if re.search(
r"^(\d|\w){4}.(\d|\w){4}.(\d|\w){4}$", entry["remote_chassis_id"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing this regex check, I'd just apply the convert function instead, e.g.,

Suggested change
r"^(\d|\w){4}.(\d|\w){4}.(\d|\w){4}$", entry["remote_chassis_id"]
entry["remote_chassis_id"] = napalm.base.helpers.conver(napalm.base.helpers.mac, entry["remote_chassis_id"], entry["remote_chassis_id"])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the quick review and all the suggestions. Just checking: I agree with your suggestion, but if the chassis id cannot be converted, an empty chassis id will be returned. This is an example value from a chassis id from one of the tests:

In [75]: id = "hmbbkku-la"

In [76]: id = napalm.base.helpers.convert(napalm.base.helpers.mac, id)

In [77]: id
Out[77]: ''

Is that OK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have addressed the comment with the latest commit. Let me know what you think @mirceaulinic

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there should be a third argument, with the default, notice in the suggestion above:

entry["remote_chassis_id"] = napalm.base.helpers.conver(napalm.base.helpers.mac, entry["remote_chassis_id"], entry["remote_chassis_id"])

When unable to convert to the normalised MAC format, it'd default to the extracted value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the help! Added.

):
entry["remote_chassis_id"] = napalm.base.helpers.mac(
entry["remote_chassis_id"]
)
print(entry["remote_chassis_id"])
mundruid marked this conversation as resolved.
Show resolved Hide resolved

# Older IOS versions don't have 'Local Intf' defined in LLDP detail.
# We need to get them from the non-detailed command
# which is in the same sequence as the detailed output
Expand Down Expand Up @@ -1203,6 +1215,7 @@ def get_interfaces_ip(self):
m = re.match(INTERNET_ADDRESS, line)
if m:
ip, prefix = m.groups()
ip = napalm.base.helpers.ip(ip)
ipv4.update({ip: {"prefix_length": int(prefix)}})
interfaces[interface_name] = {"ipv4": ipv4}

Expand All @@ -1220,10 +1233,12 @@ def get_interfaces_ip(self):
m = re.match(LINK_LOCAL_ADDRESS, line)
if m:
ip = m.group(1)
ip = napalm.base.helpers.ip(ip, 6)
ipv6.update({ip: {"prefix_length": 10}})
m = re.match(GLOBAL_ADDRESS, line)
if m:
ip, prefix = m.groups()
ip = napalm.base.helpers.ip(ip, 6)
ipv6.update({ip: {"prefix_length": int(prefix)}})

# Interface without ipv6 doesn't appears in show ipv6 interface
Expand Down Expand Up @@ -2383,7 +2398,7 @@ def get_ntp_peers(self):
ntp_stats = self.get_ntp_stats()

return {
ntp_peer.get("remote"): {}
napalm.base.helpers.ip(ntp_peer.get("remote")): {}
for ntp_peer in ntp_stats
if ntp_peer.get("remote")
}
Expand Down Expand Up @@ -3251,7 +3266,10 @@ def ping(
results_array = []
for i in range(probes_received):
results_array.append(
{"ip_address": str(destination), "rtt": 0.0}
{
"ip_address": napalm.base.helpers.ip(str(destination)),
"rtt": 0.0,
}
)
ping_dict["success"].update({"results": results_array})

Expand Down Expand Up @@ -3374,7 +3392,7 @@ def traceroute(
current_probe += 1
# If current_element contains msec record the entry for probe
elif "msec" in current_element:
ip_address = str(ip_address)
ip_address = napalm.base.helpers.ip(str(ip_address))
host_name = str(host_name)
rtt = float(current_element.replace("msec", ""))
results[current_hop]["probes"][current_probe][
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
{
"Vlan20": {
"ipv4": {
"172.29.50.3": {
"prefix_length": 27
}
},
"ipv6": {
"FE80::C009:24FF:FEAC:0": {
"prefix_length": 10
},
"2002::1": {
"prefix_length": 64
}
}
},
"Vlan41": {
"ipv4": {
"172.29.52.34": {
"prefix_length": 27
},
"192.168.81.34": {
"prefix_length": 24
}
}
},
"Loopback0": {
"ipv6": {
"FE80::C009:24FF:FEAC:0": {"prefix_length": 10},
"2001::1": {"prefix_length": 128},
"2001::2": {"prefix_length": 128}
}
}
"Vlan20": {
"ipv4": {
"172.29.50.3": {
"prefix_length": 27
}
},
"ipv6": {
"fe80::c009:24ff:feac:0": {
"prefix_length": 10
},
"2002::1": {
"prefix_length": 64
}
}
},
"Vlan41": {
"ipv4": {
"172.29.52.34": {
"prefix_length": 27
},
"192.168.81.34": {
"prefix_length": 24
}
}
},
"Loopback0": {
"ipv6": {
"fe80::c009:24ff:feac:0": {
"prefix_length": 10
},
"2001::1": {
"prefix_length": 128
},
"2001::2": {
"prefix_length": 128
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"FastEthernet0/24": [
{
"port": "Fa0/19",
"hostname": "switch1"
}
],
"FastEthernet0/17": [
{
"port": "480f.cf28.8a1b",
"hostname": "480f.cf28.8a1b"
}
]
"FastEthernet0/24": [
{
"port": "Fa0/19",
"hostname": "switch1"
}
],
"FastEthernet0/17": [
{
"port": "48:0F:CF:28:8A:1B",
"hostname": "48:0F:CF:28:8A:1B"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
{
"GigabitEthernet1": [
{
"parent_interface": "",
"remote_chassis_id": "2cc2.603e.363b",
"remote_port": "Management1",
"remote_port_description": "",
"remote_system_capab": ["bridge", "router"],
"remote_system_description": "Arista Networks EOS version 4.15.2F running on an Arista Networks vEOS",
"remote_system_enable_capab": ["bridge", "router"],
"remote_system_name": "eos-spine1.ntc.com"
},
{
"parent_interface": "",
"remote_chassis_id": "0005.8671.58c0",
"remote_port": "fxp0",
"remote_port_description": "fxp0",
"remote_system_capab": ["bridge", "router"],
"remote_system_description": "Juniper Networks, Inc. vmx internet router, kernel JUNOS 15.1F4.15, Build date: 2015-12-23 19:22:55 UTC Copyright (c) 1996-2015 Juniper Networks, Inc.",
"remote_system_enable_capab": ["bridge", "router"],
"remote_system_name": "vmx1"
}
]
"GigabitEthernet1": [
{
"parent_interface": "",
"remote_chassis_id": "2C:C2:60:3E:36:3B",
"remote_port": "Management1",
"remote_port_description": "",
"remote_system_capab": ["bridge", "router"],
"remote_system_description": "Arista Networks EOS version 4.15.2F running on an Arista Networks vEOS",
"remote_system_enable_capab": ["bridge", "router"],
"remote_system_name": "eos-spine1.ntc.com"
},
{
"parent_interface": "",
"remote_chassis_id": "00:05:86:71:58:C0",
"remote_port": "fxp0",
"remote_port_description": "fxp0",
"remote_system_capab": ["bridge", "router"],
"remote_system_description": "Juniper Networks, Inc. vmx internet router, kernel JUNOS 15.1F4.15, Build date: 2015-12-23 19:22:55 UTC Copyright (c) 1996-2015 Juniper Networks, Inc.",
"remote_system_enable_capab": ["bridge", "router"],
"remote_system_name": "vmx1"
}
]
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"FastEthernet0/24": [
{
"parent_interface": "",
"remote_chassis_id": "a40c.c3c1.3980",
"remote_port": "Fa0/19",
"remote_port_description": "FastEthernet0/19",
"remote_system_capab": ["bridge"],
"remote_system_description": "Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(1)SE2, RELEASE SOFTWARE (fc3)",
"remote_system_enable_capab": ["bridge"],
"remote_system_name": "switch1"
}
],
"FastEthernet0/17": [
{
"parent_interface": "",
"remote_chassis_id": "480f.cf28.8a1b",
"remote_port": "480f.cf28.8a1b",
"remote_port_description": "",
"remote_system_capab": [],
"remote_system_description": "",
"remote_system_enable_capab": [],
"remote_system_name": ""
}
]
"FastEthernet0/24": [
{
"parent_interface": "",
"remote_chassis_id": "A4:0C:C3:C1:39:80",
"remote_port": "Fa0/19",
"remote_port_description": "FastEthernet0/19",
"remote_system_capab": ["bridge"],
"remote_system_description": "Cisco IOS Software, C2960 Software (C2960-LANBASEK9-M), Version 15.0(1)SE2, RELEASE SOFTWARE (fc3)",
"remote_system_enable_capab": ["bridge"],
"remote_system_name": "switch1"
}
],
"FastEthernet0/17": [
{
"parent_interface": "",
"remote_chassis_id": "48:0F:CF:28:8A:1B",
"remote_port": "480f.cf28.8a1b",
"remote_port_description": "",
"remote_system_capab": [],
"remote_system_description": "",
"remote_system_enable_capab": [],
"remote_system_name": ""
}
]
}
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
{
"GigabitEthernet1": [
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "001e.bd3a.f900",
"remote_system_name": "csr9.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
},
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "001e.bd60.ee00",
"remote_system_name": "csr8.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
},
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "001e.e507.ed00",
"remote_system_name": "csr12.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
],
"GigabitEthernet2": [
{
"parent_interface": "",
"remote_port": "Gi2",
"remote_port_description": "GigabitEthernet2",
"remote_chassis_id": "001e.bd3a.f900",
"remote_system_name": "csr9.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
],
"GigabitEthernet4": [
{
"parent_interface": "",
"remote_port": "Gi4",
"remote_port_description": "GigabitEthernet4",
"remote_chassis_id": "001e.bd60.ee00",
"remote_system_name": "csr8.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
]
"GigabitEthernet1": [
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "00:1E:BD:3A:F9:00",
"remote_system_name": "csr9.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
},
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "00:1E:BD:60:EE:00",
"remote_system_name": "csr8.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
},
{
"parent_interface": "",
"remote_port": "Gi1",
"remote_port_description": "GigabitEthernet1",
"remote_chassis_id": "00:1E:E5:07:ED:00",
"remote_system_name": "csr12.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
],
"GigabitEthernet2": [
{
"parent_interface": "",
"remote_port": "Gi2",
"remote_port_description": "GigabitEthernet2",
"remote_chassis_id": "00:1E:BD:3A:F9:00",
"remote_system_name": "csr9.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
],
"GigabitEthernet4": [
{
"parent_interface": "",
"remote_port": "Gi4",
"remote_port_description": "GigabitEthernet4",
"remote_chassis_id": "00:1E:BD:60:EE:00",
"remote_system_name": "csr8.bogus.com",
"remote_system_description": "Cisco IOS Software [Denali], CSR1000V Software (X86_64_LINUX_IOSD-UNIVERSALK9-M), Version 16.3.1, RELEASE SOFTWARE (fc3)",
"remote_system_capab": ["bridge", "router"],
"remote_system_enable_capab": ["router"]
}
]
}
Loading