Skip to content
Merged
Changes from all 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
41 changes: 41 additions & 0 deletions py-json/LANforge/lfcli_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,47 @@ def json_delete(self, _req_url, debug_=False):
# print("----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ")
return json_response

def resolve_port_to_ipv4(self, port):
"""Resolve a LANforge port name or EID to its IPv4 address.

Returns the address as a string, or None when it cannot be resolved.
"""
if not port:
logger.error("resolve_port_to_ipv4: provided invalid port name, port: %s", port)
return None

if not isinstance(port, str):
logger.error("resolve_port_to_ipv4: port must be a string, got %s",
type(port).__name__)
return None
Comment thread
Narayana-CT marked this conversation as resolved.

# when the port is already an IP address, return it unchanged
if port.count('.') == 3:
logger.info("Port IP %s", port)
if port == '0.0.0.0':
logger.warning("Port IP is 0.0.0.0 is not a valid IP")
Comment thread
Narayana-CT marked this conversation as resolved.
return port

shelf, resource, port_name, _ = LFUtils.name_to_eid(port)
response = self.json_get('/port/{}/{}/{}?fields=ip'.format(shelf, resource, port_name))
Comment thread
Narayana-CT marked this conversation as resolved.

try:
resolved_ip = response['interface']['ip']
except (TypeError, KeyError) as e:
logger.error(
"resolve_port_to_ipv4: /port/%s/%s/%s response is not in the expected "
Comment thread
haricharan-candela marked this conversation as resolved.
"format (%s). Data received: %s", shelf, resource, port_name, e, response)
Comment thread
Narayana-CT marked this conversation as resolved.
return None

if not resolved_ip or resolved_ip == "0.0.0.0":
logger.error(
"resolve_port_to_ipv4: port '%s' resolved to %s; the port is down or has "
Comment thread
haricharan-candela marked this conversation as resolved.
"no IP assigned.", port, resolved_ip)
Comment thread
Narayana-CT marked this conversation as resolved.
return None

logger.info("Port IP %s", resolved_ip)
return resolved_ip

@staticmethod
def response_list_to_map(json_list, key, debug_=False):
reverse_map = {}
Expand Down
Loading