aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries
diff options
context:
space:
mode:
authorselias <samelias@cisco.com>2016-10-11 17:17:36 +0200
committerPeter Mikus <pmikus@cisco.com>2016-10-20 06:51:53 +0000
commit99519a54811a70b4ff2579baf46294507a8adfcb (patch)
treed00fb11b10ff62d40857d0ca658df2a6e4f1bfe1 /resources/libraries
parenta61e7de7f4e4491df5d25c59750178c0fe06e6a7 (diff)
Add topology method for generic handling of interface references
- add "convert_interface_reference" to topology.py This is a convenience method, mainly intended for Honeycomb tests which often require conversions between interface key, name and sw_if_index. Change-Id: I427736111f2a1dc07a581c9ccc25e181065bb6fd Signed-off-by: selias <samelias@cisco.com>
Diffstat (limited to 'resources/libraries')
-rw-r--r--resources/libraries/python/topology.py79
1 files changed, 78 insertions, 1 deletions
diff --git a/resources/libraries/python/topology.py b/resources/libraries/python/topology.py
index 02f326a1df..0b40967ef2 100644
--- a/resources/libraries/python/topology.py
+++ b/resources/libraries/python/topology.py
@@ -349,7 +349,7 @@ class Topology(object):
:type node: dict
:type iface_key: str
:return: Interface name or None if not found.
- :rtype: int
+ :rtype: str
"""
try:
return node['interfaces'][iface_key].get('name')
@@ -357,6 +357,83 @@ class Topology(object):
return None
@staticmethod
+ def convert_interface_reference_to_key(node, interface):
+ """Takes interface reference in any format
+ (name, link name, interface key or sw_if_index)
+ and converts to interface key using Topology methods.
+
+ :param node: Node in topology.
+ :param interface: Name, sw_if_index, link name or key of an interface
+ on the node.
+ Valid formats are: sw_if_index, key, name.
+ :type node: dict
+ :type interface: str or int
+
+ :return: Interface key.
+ :rtype: str
+
+ :raises TypeError: If provided with invalid arguments.
+ :raises RuntimeError: If the interface does not exist in topology.
+ """
+
+ if isinstance(interface, int):
+ key = Topology.get_interface_by_sw_index(node, interface)
+ if key is None:
+ raise RuntimeError("Interface with sw_if_index={0} does not "
+ "exist in topology.".format(interface))
+ elif interface in Topology.get_node_interfaces(node):
+ key = interface
+ elif interface in Topology.get_links({"dut": node}):
+ key = Topology.get_interface_by_link_name(node, interface)
+ elif isinstance(interface, basestring):
+ key = Topology.get_interface_by_name(node, interface)
+ if key is None:
+ raise RuntimeError("Interface with key, name or link name "
+ "\"{0}\" does not exist in topology."
+ .format(interface))
+ else:
+ raise TypeError("Type of interface argument must be integer"
+ " or string.")
+ return key
+
+ @staticmethod
+ def convert_interface_reference(node, interface, wanted_format):
+ """Takes interface reference in any format
+ (name, link name, topology key or sw_if_index) and returns
+ its equivalent in the desired format.
+
+ :param node: Node in topology.
+ :param interface: Name, sw_if_index, link name or key of an interface
+ on the node.
+ :param wanted_format: Format of return value wanted.
+ Valid options are: sw_if_index, key, name.
+ :type node: dict
+ :type interface: str or int
+ :type wanted_format: str
+
+ :return: Interface name, interface key or sw_if_index.
+ :rtype: str or int
+
+ :raises TypeError, ValueError: If provided with invalid arguments.
+ :raises RuntimeError: If the interface does not exist in topology.
+ """
+
+ key = Topology.convert_interface_reference_to_key(node, interface)
+
+ conversions = {
+ "key": lambda x, y: y,
+ "name": Topology.get_interface_name,
+ "sw_if_index": Topology.get_interface_sw_index
+ }
+
+ try:
+ return conversions[wanted_format](node, key)
+ except KeyError:
+ raise ValueError("Unrecognized return value wanted: {0}."
+ "Valid options are key, name, sw_if_index"
+ .format(wanted_format))
+
+ @staticmethod
def get_interface_numa_node(node, iface_key):
"""Get interface numa node.