1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 """Defines nodes and topology structure."""
16 from collections import Counter
20 from robot.api import logger
21 from robot.libraries.BuiltIn import BuiltIn
22 from robot.api.deco import keyword
24 __all__ = ["DICT__nodes", 'Topology']
27 def load_topo_from_yaml():
28 """Load topology from file defined in "${TOPOLOGY_PATH}" variable.
30 :return: Nodes from loaded topology.
32 topo_path = BuiltIn().get_variable_value("${TOPOLOGY_PATH}")
34 with open(topo_path) as work_file:
35 return load(work_file.read())['nodes']
38 # pylint: disable=invalid-name
39 class NodeType(object):
40 """Defines node types used in topology dictionaries."""
41 # Device Under Test (this node has VPP running on it)
43 # Traffic Generator (this node has traffic generator on it)
45 # Virtual Machine (this node running on DUT node)
49 class NodeSubTypeTG(object):
50 """Defines node sub-type TG - traffic generator."""
51 # T-Rex traffic generator
58 DICT__nodes = load_topo_from_yaml()
61 class Topology(object):
62 """Topology data manipulation and extraction methods.
64 Defines methods used for manipulation and extraction of data from
67 "Active topology" contains initially data from the topology file and can be
68 extended with additional data from the DUTs like internal interface indexes
69 or names. Additional data which can be filled to the active topology are
70 - additional internal representation (index, name, ...)
71 - operational data (dynamic ports)
73 To access the port data it is recommended to use a port key because the key
74 does not rely on the data retrieved from nodes, this allows to call most of
75 the methods without having filled active topology with internal nodes data.
79 def add_new_port(node, ptype):
80 """Add new port to the node to active topology.
82 :param node: Node to add new port on.
83 :param ptype: Port type, used as key prefix.
86 :return: Port key or None
87 :rtype: string or None
91 for i in range(1, max_ports):
92 if node['interfaces'].get(str(ptype) + str(i)) is None:
93 iface = str(ptype) + str(i)
94 node['interfaces'][iface] = dict()
99 def remove_all_ports(node, ptype):
100 """Remove all ports with ptype as prefix.
102 :param node: Node to remove ports on.
103 :param: ptype: Port type, used as key prefix.
108 for if_key in list(node['interfaces']):
109 if if_key.startswith(str(ptype)):
110 node['interfaces'].pop(if_key)
113 def update_interface_sw_if_index(node, iface_key, sw_if_index):
114 """Update sw_if_index on the interface from the node.
116 :param node: Node to update sw_if_index on.
117 :param iface_key: Topology key of the interface.
118 :param sw_if_index: Internal index to store.
121 :type sw_if_index: int
123 node['interfaces'][iface_key]['vpp_sw_index'] = int(sw_if_index)
126 def update_interface_mac_address(node, iface_key, mac_address):
127 """Update mac_address on the interface from the node.
129 :param node: Node to update MAC on.
130 :param iface_key: Topology key of the interface.
131 :param mac_address: MAC address.
134 :type mac_address: str
136 node['interfaces'][iface_key]['mac_address'] = str(mac_address)
139 def update_interface_vhost_socket(node, iface_key, vhost_socket):
140 """Update vhost socket name on the interface from the node.
142 :param node: Node to update socket name on.
143 :param iface_key: Topology key of the interface.
144 :param vhost_socket: Path to named socket on node.
147 :type vhost_socket: str
149 node['interfaces'][iface_key]['vhost_socket'] = str(vhost_socket)
152 def get_node_by_hostname(nodes, hostname):
153 """Get node from nodes of the topology by hostname.
155 :param nodes: Nodes of the test topology.
156 :param hostname: Host name.
159 :return: Node dictionary or None if not found.
161 for node in nodes.values():
162 if node['host'] == hostname:
168 def get_links(nodes):
169 """Get list of links(networks) in the topology.
171 :param nodes: Nodes of the test topology.
173 :return: Links in the topology.
178 for node in nodes.values():
179 for interface in node['interfaces'].values():
180 link = interface.get('link')
182 if link not in links:
188 def _get_interface_by_key_value(node, key, value):
189 """Return node interface key from topology file
190 according to key and value.
192 :param node: The node dictionary.
193 :param key: Key by which to select the interface.
194 :param value: Value that should be found using the key.
198 :return: Interface key from topology file
201 interfaces = node['interfaces']
203 for if_key, if_val in interfaces.iteritems():
204 k_val = if_val.get(key)
205 if k_val is not None:
212 def get_interface_by_name(node, iface_name):
213 """Return interface key based on name from DUT/TG.
215 This method returns interface key based on interface name
216 retrieved from the DUT, or TG.
218 :param node: The node topology dictionary.
219 :param iface_name: Interface name (string form).
221 :type iface_name: string
222 :return: Interface key.
225 return Topology._get_interface_by_key_value(node, "name", iface_name)
228 def get_interface_by_link_name(node, link_name):
229 """Return interface key of link on node.
231 This method returns the interface name associated with a given link
234 :param node: The node topology dictionary.
235 :param link_name: Name of the link that a interface is connected to.
237 :type link_name: string
238 :return: Interface key of the interface connected to the given link.
241 return Topology._get_interface_by_key_value(node, "link", link_name)
243 def get_interfaces_by_link_names(self, node, link_names):
244 """Return dictionary of dictionaries {"interfaceN", interface name}.
246 This method returns the interface names associated with given links
249 :param node: The node topology directory.
250 :param link_names: List of names of the link that a interface is
253 :type link_names: list
254 :return: Dictionary of interface names that are connected to the given
259 interface_key_tpl = "interface{}"
261 for link_name in link_names:
262 interface = self.get_interface_by_link_name(node, link_name)
263 interface_name = self.get_interface_name(node, interface)
264 interface_key = interface_key_tpl.format(str(interface_number))
265 retval[interface_key] = interface_name
266 interface_number += 1
270 def get_interface_by_sw_index(node, sw_index):
271 """Return interface name of link on node.
273 This method returns the interface name associated with a software
274 interface index assigned to the interface by vpp for a given node.
276 :param node: The node topology dictionary.
277 :param sw_index: Sw_index of the link that a interface is connected to.
280 :return: Interface name of the interface connected to the given link.
283 return Topology._get_interface_by_key_value(node, "vpp_sw_index",
287 def get_interface_sw_index(node, iface_key):
288 """Get VPP sw_if_index for the interface using interface key.
290 :param node: Node to get interface sw_if_index on.
291 :param iface_key: Interface key from topology file, or sw_index.
293 :type iface_key: str/int
294 :return: Return sw_if_index or None if not found.
297 if isinstance(iface_key, basestring):
298 return node['interfaces'][iface_key].get('vpp_sw_index')
299 #FIXME: use only iface_key, do not use integer
301 return int(iface_key)
302 except (KeyError, ValueError):
306 def get_interface_sw_index_by_name(node, iface_name):
307 """Get VPP sw_if_index for the interface using interface name.
309 :param node: Node to get interface sw_if_index on.
310 :param iface_name: Interface name.
312 :type iface_name: str
313 :return: Return sw_if_index or None if not found.
314 :raises TypeError: If provided interface name is not a string.
317 if isinstance(iface_name, basestring):
318 iface_key = Topology.get_interface_by_name(node, iface_name)
319 return node['interfaces'][iface_key].get('vpp_sw_index')
321 raise TypeError("Interface name must be a string.")
322 except (KeyError, ValueError):
326 def get_interface_mtu(node, iface_key):
327 """Get interface MTU.
329 Returns physical layer MTU (max. size of Ethernet frame).
330 :param node: Node to get interface MTU on.
331 :param iface_key: Interface key from topology file.
334 :return: MTU or None if not found.
338 return node['interfaces'][iface_key].get('mtu')
343 def get_interface_name(node, iface_key):
344 """Get interface name (retrieved from DUT/TG).
346 Returns name in string format, retrieved from the node.
347 :param node: Node to get interface name on.
348 :param iface_key: Interface key from topology file.
351 :return: Interface name or None if not found.
355 return node['interfaces'][iface_key].get('name')
360 def get_interface_numa_node(node, iface_key):
361 """Get interface numa node.
363 Returns physical relation to numa node, numa_id.
365 :param node: Node to get numa id on.
366 :param iface_key: Interface key from topology file.
369 :return: numa node id, None if not available.
373 return node['interfaces'][iface_key].get('numa_node')
378 def get_interfaces_numa_node(node, *iface_keys):
379 """Get numa node on which are located most of the interfaces.
381 Return numa node with highest count of interfaces provided as arguments.
382 Return 0 if the interface does not have numa_node information available.
383 If all interfaces have unknown location (-1), then return 0.
384 If most of interfaces have unknown location (-1), but there are
385 some interfaces with known location, then return the second most
386 location of the provided interfaces.
388 :param node: Node from DICT__nodes.
389 :param iface_keys: Interface keys for lookup.
391 :type iface_keys: strings
394 for if_key in iface_keys:
396 numa_list.append(node['interfaces'][if_key].get('numa_node'))
400 numa_cnt_mc = Counter(numa_list).most_common()
402 if len(numa_cnt_mc) > 0 and numa_cnt_mc[0][0] != -1:
403 return numa_cnt_mc[0][0]
404 elif len(numa_cnt_mc) > 1 and numa_cnt_mc[0][0] == -1:
405 return numa_cnt_mc[1][0]
410 def get_interface_mac(node, iface_key):
411 """Get MAC address for the interface.
413 :param node: Node to get interface mac on.
414 :param iface_key: Interface key from topology file.
417 :return: Return MAC or None if not found.
420 return node['interfaces'][iface_key].get('mac_address')
425 def get_adjacent_node_and_interface(nodes_info, node, iface_key):
426 """Get node and interface adjacent to specified interface
429 :param nodes_info: Dictionary containing information on all nodes
431 :param node: Node that contains specified interface.
432 :param iface_key: Interface key from topology file.
433 :type nodes_info: dict
436 :return: Return (node, interface_key) tuple or None if not found.
440 # get link name where the interface belongs to
441 for if_key, if_val in node['interfaces'].iteritems():
444 if if_key == iface_key:
445 link_name = if_val['link']
448 if link_name is None:
452 for node_data in nodes_info.values():
454 if node_data['host'] == node['host']:
457 in node_data['interfaces'].iteritems():
458 if 'link' not in if_val:
460 if if_val['link'] == link_name:
461 return node_data, if_key
464 def get_interface_pci_addr(node, iface_key):
465 """Get interface PCI address.
467 :param node: Node to get interface PCI address on.
468 :param iface_key: Interface key from topology file.
471 :return: Return PCI address or None if not found.
474 return node['interfaces'][iface_key].get('pci_address')
479 def get_interface_driver(node, iface_key):
480 """Get interface driver.
482 :param node: Node to get interface driver on.
483 :param iface_key: Interface key from topology file.
486 :return: Return interface driver or None if not found.
489 return node['interfaces'][iface_key].get('driver')
494 def get_node_interfaces(node):
495 """Get all node interfaces.
497 :param node: Node to get list of interfaces from.
499 :return: Return list of keys of all interfaces.
502 return node['interfaces'].keys()
505 def get_node_link_mac(node, link_name):
506 """Return interface mac address by link name.
508 :param node: Node to get interface sw_index on.
509 :param link_name: Link name.
512 :return: MAC address string.
515 for port in node['interfaces'].values():
516 if port.get('link') == link_name:
517 return port.get('mac_address')
521 def _get_node_active_link_names(node, filter_list=None):
522 """Return list of link names that are other than mgmt links.
524 :param node: Node topology dictionary.
525 :param filter_list: Link filter criteria.
527 :type filter_list: list of strings
528 :return: List of strings that represent link names occupied by the node.
531 interfaces = node['interfaces']
533 for interface in interfaces.values():
534 if 'link' in interface:
535 if (filter_list is not None) and ('model' in interface):
536 for filt in filter_list:
537 if filt == interface['model']:
538 link_names.append(interface['link'])
539 elif (filter_list is not None) and ('model' not in interface):
540 logger.trace("Cannot apply filter on interface: {}"
541 .format(str(interface)))
543 link_names.append(interface['link'])
544 if len(link_names) == 0:
548 @keyword('Get active links connecting "${node1}" and "${node2}"')
549 def get_active_connecting_links(self, node1, node2,
550 filter_list_node1=None,
551 filter_list_node2=None):
552 """Return list of link names that connect together node1 and node2.
554 :param node1: Node topology dictionary.
555 :param node2: Node topology dictionary.
556 :param filter_list_node1: Link filter criteria for node1.
557 :param filter_list_node2: Link filter criteria for node2.
560 :type filter_list_node1: list of strings
561 :type filter_list_node2: list of strings
562 :return: List of strings that represent connecting link names.
566 logger.trace("node1: {}".format(str(node1)))
567 logger.trace("node2: {}".format(str(node2)))
568 node1_links = self._get_node_active_link_names(
570 filter_list=filter_list_node1)
571 node2_links = self._get_node_active_link_names(
573 filter_list=filter_list_node2)
575 connecting_links = None
576 if node1_links is None:
577 logger.error("Unable to find active links for node1")
578 elif node2_links is None:
579 logger.error("Unable to find active links for node2")
581 connecting_links = list(set(node1_links).intersection(node2_links))
583 return connecting_links
585 @keyword('Get first active connecting link between node "${node1}" and '
587 def get_first_active_connecting_link(self, node1, node2):
590 :param node1: Connected node.
591 :param node2: Connected node.
594 :return: Name of link connecting the two nodes together.
596 :raises: RuntimeError
598 connecting_links = self.get_active_connecting_links(node1, node2)
599 if len(connecting_links) == 0:
600 raise RuntimeError("No links connecting the nodes were found")
602 return connecting_links[0]
604 @keyword('Get egress interfaces name on "${node1}" for link with '
606 def get_egress_interfaces_name_for_nodes(self, node1, node2):
607 """Get egress interfaces on node1 for link with node2.
609 :param node1: First node, node to get egress interface on.
610 :param node2: Second node.
613 :return: Egress interfaces.
617 links = self.get_active_connecting_links(node1, node2)
619 raise RuntimeError('No link between nodes')
620 for interface in node1['interfaces'].values():
621 link = interface.get('link')
626 name = interface.get('name')
629 interfaces.append(name)
632 @keyword('Get first egress interface name on "${node1}" for link with '
634 def get_first_egress_interface_for_nodes(self, node1, node2):
635 """Get first egress interface on node1 for link with node2.
637 :param node1: First node, node to get egress interface name on.
638 :param node2: Second node.
641 :return: Egress interface name.
644 interfaces = self.get_egress_interfaces_name_for_nodes(node1, node2)
646 raise RuntimeError('No egress interface for nodes')
649 @keyword('Get link data useful in circular topology test from tg "${tgen}"'
650 ' dut1 "${dut1}" dut2 "${dut2}"')
651 def get_links_dict_from_nodes(self, tgen, dut1, dut2):
652 """Return link combinations used in tests in circular topology.
654 For the time being it returns links from the Node path:
656 The naming convention until changed to something more general is
658 DUT1_DUT2_LINK: link name between DUT! and DUT2
659 DUT1_TG_LINK: link name between DUT1 and TG
660 DUT2_TG_LINK: link name between DUT2 and TG
661 TG_TRAFFIC_LINKS: list of link names that generated traffic is sent
663 DUT1_BD_LINKS: list of link names that will be connected by the bridge
665 DUT2_BD_LINKS: list of link names that will be connected by the bridge
668 :param tgen: Traffic generator node data.
669 :param dut1: DUT1 node data.
670 :param dut2: DUT2 node data.
674 :return: Dictionary of possible link combinations.
677 # TODO: replace with generic function.
678 dut1_dut2_link = self.get_first_active_connecting_link(dut1, dut2)
679 dut1_tg_link = self.get_first_active_connecting_link(dut1, tgen)
680 dut2_tg_link = self.get_first_active_connecting_link(dut2, tgen)
681 tg_traffic_links = [dut1_tg_link, dut2_tg_link]
682 dut1_bd_links = [dut1_dut2_link, dut1_tg_link]
683 dut2_bd_links = [dut1_dut2_link, dut2_tg_link]
684 topology_links = {'DUT1_DUT2_LINK': dut1_dut2_link,
685 'DUT1_TG_LINK': dut1_tg_link,
686 'DUT2_TG_LINK': dut2_tg_link,
687 'TG_TRAFFIC_LINKS': tg_traffic_links,
688 'DUT1_BD_LINKS': dut1_bd_links,
689 'DUT2_BD_LINKS': dut2_bd_links}
690 return topology_links
693 def is_tg_node(node):
694 """Find out whether the node is TG.
696 :param node: Node to examine.
698 :return: True if node is type of TG, otherwise False.
701 return node['type'] == NodeType.TG
704 def get_node_hostname(node):
705 """Return host (hostname/ip address) of the node.
707 :param node: Node created from topology.
709 :return: Hostname or IP address.
715 def set_interface_numa_node(node, iface_key, numa_node_id):
716 """Set interface numa_node location.
718 :param node: Node to set numa_node on.
719 :param iface_key: Interface key from topology file.
722 :return: Return iface_key or None if not found.
725 node['interfaces'][iface_key]['numa_node'] = numa_node_id