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."""
18 from robot.api import logger
19 from robot.libraries.BuiltIn import BuiltIn
20 from robot.api.deco import keyword
22 __all__ = ["DICT__nodes", 'Topology']
25 def load_topo_from_yaml():
26 """Load topology from file defined in "${TOPOLOGY_PATH}" variable.
28 :return: Nodes from loaded topology.
30 topo_path = BuiltIn().get_variable_value("${TOPOLOGY_PATH}")
32 with open(topo_path) as work_file:
33 return load(work_file.read())['nodes']
36 class NodeType(object):
37 """Defines node types used in topology dictionaries."""
38 # Device Under Test (this node has VPP running on it)
40 # Traffic Generator (this node has traffic generator on it)
42 # Virtual Machine (this node running on DUT node)
46 class NodeSubTypeTG(object):
47 # T-Rex traffic generator
54 DICT__nodes = load_topo_from_yaml()
57 class Topology(object):
58 """Topology data manipulation and extraction methods.
60 Defines methods used for manipulation and extraction of data from
63 "Active topology" contains initially data from the topology file and can be
64 extended with additional data from the DUTs like internal interface indexes
65 or names. Additional data which can be filled to the active topology are
66 - additional internal representation (index, name, ...)
67 - operational data (dynamic ports)
69 To access the port data it is recommended to use a port key because the key
70 does not rely on the data retrieved from nodes, this allows to call most of
71 the methods without having filled active topology with internal nodes data.
75 def add_new_port(node, ptype):
76 """Add new port to the node to active topology.
78 :param node: Node to add new port on.
79 :param ptype: Port type, used as key prefix.
82 :return: Port key or None
83 :rtype: string or None
87 for i in range(1, max_ports):
88 if node['interfaces'].get(str(ptype) + str(i)) is None:
89 iface = str(ptype) + str(i)
90 node['interfaces'][iface] = dict()
95 def remove_all_ports(node, ptype):
96 """Remove all ports with ptype as prefix.
98 :param node: Node to remove ports on.
99 :param: ptype: Port type, used as key prefix.
104 for if_key in list(node['interfaces']):
105 if if_key.startswith(str(ptype)):
106 node['interfaces'].pop(if_key)
109 def update_interface_sw_if_index(node, iface_key, sw_if_index):
110 """Update sw_if_index on the interface from the node.
112 :param node: Node to update sw_if_index on.
113 :param iface_key: Topology key of the interface.
114 :param sw_if_index: Internal index to store.
117 :type sw_if_index: int
119 node['interfaces'][iface_key]['vpp_sw_index'] = int(sw_if_index)
122 def update_interface_mac_address(node, iface_key, mac_address):
123 """Update mac_address on the interface from the node.
125 :param node: Node to update MAC on.
126 :param iface_key: Topology key of the interface.
127 :param mac_address: MAC address.
130 :type mac_address: str
132 node['interfaces'][iface_key]['mac_address'] = str(mac_address)
135 def update_interface_vhost_socket(node, iface_key, vhost_socket):
136 """Update vhost socket name on the interface from the node.
138 :param node: Node to update socket name on.
139 :param iface_key: Topology key of the interface.
140 :param vhost_socket: Path to named socket on node.
143 :type vhost_socket: str
145 node['interfaces'][iface_key]['vhost_socket'] = str(vhost_socket)
148 def get_node_by_hostname(nodes, hostname):
149 """Get node from nodes of the topology by hostname.
151 :param nodes: Nodes of the test topology.
152 :param hostname: Host name.
155 :return: Node dictionary or None if not found.
157 for node in nodes.values():
158 if node['host'] == hostname:
164 def get_links(nodes):
165 """Get list of links(networks) in the topology.
167 :param nodes: Nodes of the test topology.
169 :return: Links in the topology.
174 for node in nodes.values():
175 for interface in node['interfaces'].values():
176 link = interface.get('link')
178 if link not in links:
184 def _get_interface_by_key_value(node, key, value):
185 """Return node interface key from topology file
186 according to key and value.
188 :param node: The node dictionary.
189 :param key: Key by which to select the interface.
190 :param value: Value that should be found using the key.
194 :return: Interface key from topology file
197 interfaces = node['interfaces']
199 for if_key, if_val in interfaces.iteritems():
200 k_val = if_val.get(key)
201 if k_val is not None:
208 def get_interface_by_name(node, iface_name):
209 """Return interface key based on name from DUT/TG.
211 This method returns interface key based on interface name
212 retrieved from the DUT, or TG.
214 :param node: The node topology dictionary.
215 :param iface_name: Interface name (string form).
217 :type iface_name: string
218 :return: Interface key.
221 return Topology._get_interface_by_key_value(node, "name", iface_name)
224 def get_interface_by_link_name(node, link_name):
225 """Return interface key of link on node.
227 This method returns the interface name associated with a given link
230 :param node: The node topology dictionary.
231 :param link_name: Name of the link that a interface is connected to.
233 :type link_name: string
234 :return: Interface key of the interface connected to the given link.
237 return Topology._get_interface_by_key_value(node, "link", link_name)
239 def get_interfaces_by_link_names(self, node, link_names):
240 """Return dictionary of dictionaries {"interfaceN", interface name}.
242 This method returns the interface names associated with given links
245 :param node: The node topology directory.
246 :param link_names: List of names of the link that a interface is
249 :type link_names: list
250 :return: Dictionary of interface names that are connected to the given
255 interface_key_tpl = "interface{}"
257 for link_name in link_names:
258 interface = self.get_interface_by_link_name(node, link_name)
259 interface_name = self.get_interface_name(node, interface)
260 interface_key = interface_key_tpl.format(str(interface_number))
261 retval[interface_key] = interface_name
262 interface_number += 1
266 def get_interface_by_sw_index(node, sw_index):
267 """Return interface name of link on node.
269 This method returns the interface name associated with a software
270 interface index assigned to the interface by vpp for a given node.
272 :param node: The node topology dictionary.
273 :param sw_index: Sw_index of the link that a interface is connected to.
276 :return: Interface name of the interface connected to the given link.
279 return Topology._get_interface_by_key_value(node, "vpp_sw_index", sw_index)
282 def get_interface_sw_index(node, iface_key):
283 """Get VPP sw_if_index for the interface.
285 :param node: Node to get interface sw_if_index on.
286 :param iface_key: Interface key from topology file, or sw_index.
288 :type iface_key: str/int
289 :return: Return sw_if_index or None if not found.
292 if isinstance(iface_key, basestring):
293 return node['interfaces'][iface_key].get('vpp_sw_index')
294 #FIXME: use only iface_key, do not use integer
296 return int(iface_key)
297 except (KeyError, ValueError):
301 def get_interface_mtu(node, iface_key):
302 """Get interface MTU.
304 Returns physical layer MTU (max. size of Ethernet frame).
305 :param node: Node to get interface MTU on.
306 :param iface_key: Interface key from topology file.
309 :return: MTU or None if not found.
313 return node['interfaces'][iface_key].get('mtu')
318 def get_interface_name(node, iface_key):
319 """Get interface name (retrieved from DUT/TG).
321 Returns name in string format, retrieved from the node.
322 :param node: Node to get interface name on.
323 :param iface_key: Interface key from topology file.
326 :return: Interface name or None if not found.
330 return node['interfaces'][iface_key].get('name')
335 def get_interface_mac(node, iface_key):
336 """Get MAC address for the interface.
338 :param node: Node to get interface mac on.
339 :param iface_key: Interface key from topology file.
342 :return: Return MAC or None if not found.
345 return node['interfaces'][iface_key].get('mac_address')
350 def get_adjacent_node_and_interface(nodes_info, node, iface_key):
351 """Get node and interface adjacent to specified interface
354 :param nodes_info: Dictionary containing information on all nodes
356 :param node: Node that contains specified interface.
357 :param iface_key: Interface key from topology file.
358 :type nodes_info: dict
361 :return: Return (node, interface_key) tuple or None if not found.
365 # get link name where the interface belongs to
366 for if_key, if_val in node['interfaces'].iteritems():
369 if if_key == iface_key:
370 link_name = if_val['link']
373 if link_name is None:
377 for node_data in nodes_info.values():
379 if node_data['host'] == node['host']:
382 in node_data['interfaces'].iteritems():
383 if 'link' not in if_val:
385 if if_val['link'] == link_name:
386 return node_data, if_key
389 def get_interface_pci_addr(node, iface_key):
390 """Get interface PCI address.
392 :param node: Node to get interface PCI address on.
393 :param iface_key: Interface key from topology file.
396 :return: Return PCI address or None if not found.
399 return node['interfaces'][iface_key].get('pci_address')
404 def get_interface_driver(node, iface_key):
405 """Get interface driver.
407 :param node: Node to get interface driver on.
408 :param iface_key: Interface key from topology file.
411 :return: Return interface driver or None if not found.
414 return node['interfaces'][iface_key].get('driver')
419 def get_node_link_mac(node, link_name):
420 """Return interface mac address by link name.
422 :param node: Node to get interface sw_index on.
423 :param link_name: Link name.
426 :return: MAC address string.
429 for port in node['interfaces'].values():
430 if port.get('link') == link_name:
431 return port.get('mac_address')
435 def _get_node_active_link_names(node, filter_list=None):
436 """Return list of link names that are other than mgmt links.
438 :param node: Node topology dictionary.
439 :param filter_list: Link filter criteria.
441 :type filter_list: list of strings
442 :return: List of strings that represent link names occupied by the node.
445 interfaces = node['interfaces']
447 for interface in interfaces.values():
448 if 'link' in interface:
449 if (filter_list is not None) and ('model' in interface):
450 for filt in filter_list:
451 if filt == interface['model']:
452 link_names.append(interface['link'])
453 elif (filter_list is not None) and ('model' not in interface):
454 logger.trace("Cannot apply filter on interface: {}" \
455 .format(str(interface)))
457 link_names.append(interface['link'])
458 if len(link_names) == 0:
462 @keyword('Get active links connecting "${node1}" and "${node2}"')
463 def get_active_connecting_links(self, node1, node2,
464 filter_list_node1=None,
465 filter_list_node2=None):
466 """Return list of link names that connect together node1 and node2.
468 :param node1: Node topology dictionary.
469 :param node2: Node topology dictionary.
470 :param filter_list_node1: Link filter criteria for node1.
471 :param filter_list_node2: Link filter criteria for node2.
474 :type filter_list1: list of strings
475 :type filter_list2: list of strings
476 :return: List of strings that represent connecting link names.
480 logger.trace("node1: {}".format(str(node1)))
481 logger.trace("node2: {}".format(str(node2)))
482 node1_links = self._get_node_active_link_names(
484 filter_list=filter_list_node1)
485 node2_links = self._get_node_active_link_names(
487 filter_list=filter_list_node2)
489 connecting_links = None
490 if node1_links is None:
491 logger.error("Unable to find active links for node1")
492 elif node2_links is None:
493 logger.error("Unable to find active links for node2")
495 connecting_links = list(set(node1_links).intersection(node2_links))
497 return connecting_links
499 @keyword('Get first active connecting link between node "${node1}" and '
501 def get_first_active_connecting_link(self, node1, node2):
504 :param node1: Connected node.
505 :param node2: Connected node.
508 :return: Name of link connecting the two nodes together.
510 :raises: RuntimeError
512 connecting_links = self.get_active_connecting_links(node1, node2)
513 if len(connecting_links) == 0:
514 raise RuntimeError("No links connecting the nodes were found")
516 return connecting_links[0]
518 @keyword('Get egress interfaces name on "${node1}" for link with "${node2}"')
519 def get_egress_interfaces_name_for_nodes(self, node1, node2):
520 """Get egress interfaces on node1 for link with node2.
522 :param node1: First node, node to get egress interface on.
523 :param node2: Second node.
526 :return: Egress interfaces.
530 links = self.get_active_connecting_links(node1, node2)
532 raise RuntimeError('No link between nodes')
533 for interface in node1['interfaces'].values():
534 link = interface.get('link')
539 name = interface.get('name')
542 interfaces.append(name)
545 @keyword('Get first egress interface name on "${node1}" for link with '
547 def get_first_egress_interface_for_nodes(self, node1, node2):
548 """Get first egress interface on node1 for link with node2.
550 :param node1: First node, node to get egress interface name on.
551 :param node2: Second node.
554 :return: Egress interface name.
557 interfaces = self.get_egress_interfaces_name_for_nodes(node1, node2)
559 raise RuntimeError('No egress interface for nodes')
562 @keyword('Get link data useful in circular topology test from tg "${tgen}"'
563 ' dut1 "${dut1}" dut2 "${dut2}"')
564 def get_links_dict_from_nodes(self, tgen, dut1, dut2):
565 """Return link combinations used in tests in circular topology.
567 For the time being it returns links from the Node path:
569 The naming convention until changed to something more general is
571 DUT1_DUT2_LINK: link name between DUT! and DUT2
572 DUT1_TG_LINK: link name between DUT1 and TG
573 DUT2_TG_LINK: link name between DUT2 and TG
574 TG_TRAFFIC_LINKS: list of link names that generated traffic is sent
576 DUT1_BD_LINKS: list of link names that will be connected by the bridge
578 DUT2_BD_LINKS: list of link names that will be connected by the bridge
581 :param tgen: Traffic generator node data.
582 :param dut1: DUT1 node data.
583 :param dut2: DUT2 node data.
587 :return: Dictionary of possible link combinations.
590 # TODO: replace with generic function.
591 dut1_dut2_link = self.get_first_active_connecting_link(dut1, dut2)
592 dut1_tg_link = self.get_first_active_connecting_link(dut1, tgen)
593 dut2_tg_link = self.get_first_active_connecting_link(dut2, tgen)
594 tg_traffic_links = [dut1_tg_link, dut2_tg_link]
595 dut1_bd_links = [dut1_dut2_link, dut1_tg_link]
596 dut2_bd_links = [dut1_dut2_link, dut2_tg_link]
597 topology_links = {'DUT1_DUT2_LINK': dut1_dut2_link,
598 'DUT1_TG_LINK': dut1_tg_link,
599 'DUT2_TG_LINK': dut2_tg_link,
600 'TG_TRAFFIC_LINKS': tg_traffic_links,
601 'DUT1_BD_LINKS': dut1_bd_links,
602 'DUT2_BD_LINKS': dut2_bd_links}
603 return topology_links
606 def is_tg_node(node):
607 """Find out whether the node is TG.
609 :param node: Node to examine.
611 :return: True if node is type of TG, otherwise False.
614 return node['type'] == NodeType.TG
617 def get_node_hostname(node):
618 """Return host (hostname/ip address) of the node.
620 :param node: Node created from topology.
622 :return: Hostname or IP address.