aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/IPUtil.py
diff options
context:
space:
mode:
authorVratko Polak <vrpolak@cisco.com>2019-07-09 12:17:09 +0200
committerDave Wallace <dwallacelf@gmail.com>2019-07-12 13:00:49 +0000
commit33fb34665214bbbd0a4b3154169b21c2da01f69b (patch)
tree9ebb70889824451cf8411875159a6fafd70b60ac /resources/libraries/python/IPUtil.py
parentccfe499e2a27f2caf234ecbb2ec948120810eab6 (diff)
PapiExecutor always verifies
Do not support returning unverified replies anymore. Basically, ".get_replies().verify_replies()" is now just ".get_replies()". This allows fairly large simplifications both at call sites and in PapiExecutor.py + Rename get_dumps to get_details. + Introduce get_reply and get_sw_if_index. + Rename variables holding get_*() value, + e.g. get_stats() value is stored to variable named "stats". + Rename "item" of subsequent loop to hint the type instead. + Rename "details" function argument to "verbose". + Process reply details in place, instead of building new list. - Except hybrid blocks which can return both list or single item. - Except human readable text building blocks. + Rename most similar names to sw_if_index. - Except "vpp_sw_index" and some function names. + Use single run_cli_cmd from PapiExecutor. + Do not chain methods over multiple lines. + Small space gain is not worth readability loss. + Include minor code and docstrings improvement. + Add some TODOs. Change-Id: Ib2110a3d2101a74d5837baab3a58dc46aafc6ce3 Signed-off-by: Vratko Polak <vrpolak@cisco.com>
Diffstat (limited to 'resources/libraries/python/IPUtil.py')
-rw-r--r--resources/libraries/python/IPUtil.py53
1 files changed, 20 insertions, 33 deletions
diff --git a/resources/libraries/python/IPUtil.py b/resources/libraries/python/IPUtil.py
index 2dafebf5f6..6a8e1a2401 100644
--- a/resources/libraries/python/IPUtil.py
+++ b/resources/libraries/python/IPUtil.py
@@ -112,34 +112,30 @@ class IPUtil(object):
"""
sw_if_index = InterfaceUtil.get_interface_index(node, interface)
- data = list()
if sw_if_index:
is_ipv6 = 1 if ip_version == 'ipv6' else 0
cmd = 'ip_address_dump'
- cmd_reply = 'ip_address_details'
args = dict(sw_if_index=sw_if_index,
is_ipv6=is_ipv6)
err_msg = 'Failed to get L2FIB dump on host {host}'.format(
host=node['host'])
with PapiExecutor(node) as papi_exec:
- papi_resp = papi_exec.add(cmd, **args).get_dump(err_msg)
-
- for item in papi_resp.reply[0]['api_reply']:
- item[cmd_reply]['ip'] = item[cmd_reply]['prefix'].split('/')[0]
- item[cmd_reply]['prefix_length'] = int(
- item[cmd_reply]['prefix'].split('/')[1])
- item[cmd_reply]['is_ipv6'] = is_ipv6
- item[cmd_reply]['netmask'] = \
+ details = papi_exec.add(cmd, **args).get_details(err_msg)
+
+ for item in details:
+ item['ip'] = item['prefix'].split('/')[0]
+ item['prefix_length'] = int(item['prefix'].split('/')[1])
+ item['is_ipv6'] = is_ipv6
+ item['netmask'] = \
str(IPv6Network(unicode('::/{pl}'.format(
- pl=item[cmd_reply]['prefix_length']))).netmask) \
+ pl=item['prefix_length']))).netmask) \
if is_ipv6 \
else str(IPv4Network(unicode('0.0.0.0/{pl}'.format(
- pl=item[cmd_reply]['prefix_length']))).netmask)
- data.append(item[cmd_reply])
+ pl=item['prefix_length']))).netmask)
- return data
+ return details
@staticmethod
def vpp_get_ip_tables(node):
@@ -193,10 +189,9 @@ class IPUtil(object):
ifc=interface)
with PapiExecutor(node) as papi_exec:
- papi_resp = papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ reply = papi_exec.add(cmd, **args).get_reply(err_msg)
- return papi_resp['vrf_id']
+ return reply['vrf_id']
@staticmethod
def vpp_ip_source_check_setup(node, if_name):
@@ -215,8 +210,7 @@ class IPUtil(object):
err_msg = 'Failed to enable source check on interface {ifc}'.format(
ifc=if_name)
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)
@staticmethod
def vpp_ip_probe(node, interface, addr):
@@ -230,7 +224,6 @@ class IPUtil(object):
:type addr: str
"""
cmd = 'ip_probe_neighbor'
- cmd_reply = 'proxy_arp_intfc_enable_disable_reply'
args = dict(
sw_if_index=InterfaceUtil.get_interface_index(node, interface),
dst=str(addr))
@@ -238,8 +231,7 @@ class IPUtil(object):
dev=interface, ip=addr, h=node['host'])
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(cmd_reply=cmd_reply, err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)
@staticmethod
def ip_addresses_should_be_equal(ip1, ip2):
@@ -416,8 +408,7 @@ class IPUtil(object):
err_msg = 'Failed to add IP address on interface {ifc}'.format(
ifc=interface)
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)
@staticmethod
def vpp_add_ip_neighbor(node, iface_key, ip_addr, mac_address):
@@ -446,8 +437,7 @@ class IPUtil(object):
err_msg = 'Failed to add IP neighbor on interface {ifc}'.format(
ifc=iface_key)
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)
@staticmethod
def union_addr(ip_addr):
@@ -600,9 +590,8 @@ class IPUtil(object):
history = False if 1 < i < kwargs.get('count', 1) else True
papi_exec.add(cmd, history=history, **args)
if i > 0 and i % Constants.PAPI_MAX_API_BULK == 0:
- papi_exec.get_replies(err_msg).verify_replies(
- err_msg=err_msg)
- papi_exec.get_replies(err_msg).verify_replies(err_msg=err_msg)
+ papi_exec.get_replies(err_msg)
+ papi_exec.get_replies(err_msg)
@staticmethod
def flush_ip_addresses(node, interface):
@@ -620,8 +609,7 @@ class IPUtil(object):
err_msg = 'Failed to flush IP address on interface {ifc}'.format(
ifc=interface)
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)
@staticmethod
def add_fib_table(node, table_id, ipv6=False):
@@ -644,5 +632,4 @@ class IPUtil(object):
err_msg = 'Failed to add FIB table on host {host}'.format(
host=node['host'])
with PapiExecutor(node) as papi_exec:
- papi_exec.add(cmd, **args).get_replies(err_msg). \
- verify_reply(err_msg=err_msg)
+ papi_exec.add(cmd, **args).get_reply(err_msg)