1 # Copyright (c) 2022 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 """Path utilities library for nodes in the topology."""
16 from resources.libraries.python.topology import Topology
20 """Path utilities for nodes in the topology.
24 node1--link1-->node2--link2-->node3--link3-->node2--link4-->node1
26 | Library | resources/libraries/python/NodePath.py
29 | | [Arguments] | ${node1} | ${node2} | ${node3}
30 | | Append Node | ${nodes1}
31 | | Append Node | ${nodes2}
32 | | Append Nodes | ${nodes3} | ${nodes2}
33 | | Append Node | ${nodes1}
34 | | Compute Path | ${FALSE}
35 | | ${first_int} | ${node}= | First Interface
36 | | ${last_int} | ${node}= | Last Interface
37 | | ${first_ingress} | ${node}= | First Ingress Interface
38 | | ${last_egress} | ${node}= | Last Egress Interface
39 | | ${next} | ${node}= | Next Interface
42 >>> from NodePath import NodePath
44 >>> path.append_node(node1)
45 >>> path.append_node(node2)
46 >>> path.append_nodes(node3, node2)
47 >>> path.append_node(node1)
48 >>> path.compute_path()
49 >>> (interface, node) = path.first_interface()
50 >>> (interface, node) = path.last_interface()
51 >>> (interface, node) = path.first_ingress_interface()
52 >>> (interface, node) = path.last_egress_interface()
53 >>> (interface, node) = path.next_interface()
58 self._nodes_filter = []
63 def append_node(self, node, filter_list=None):
64 """Append node to the path.
66 :param node: Node to append to the path.
67 :param filter_list: Filter criteria list.
69 :type filter_list: list of strings
71 self._nodes_filter.append(filter_list)
72 self._nodes.append(node)
74 def append_nodes(self, *nodes, filter_list=None):
75 """Append nodes to the path.
77 :param nodes: Nodes to append to the path.
78 :param filter_list: Filter criteria list.
80 :type filter_list: list of strings
82 .. note:: Node order does matter.
85 self.append_node(node, filter_list=filter_list)
90 self._nodes_filter = []
95 def compute_path(self, always_same_link=True, topo_has_dut=True):
96 """Compute path for added nodes.
98 .. note:: First add at least two nodes to the topology.
100 :param always_same_link: If True use always same link between two nodes
101 in path. If False use different link (if available)
102 between two nodes if one link was used before.
103 :param topo_has_dut: If False we want to test back to back test on TG.
104 :type always_same_link: bool
105 :type topo_has_dut: bool
106 :raises RuntimeError: If not enough nodes for path.
109 if len(nodes) < 2 and topo_has_dut:
110 raise RuntimeError(u"Not enough nodes to compute path")
112 for idx in range(0, len(nodes) - 1):
115 n1_list = self._nodes_filter[idx]
117 node2 = nodes[idx + 1]
118 n2_list = self._nodes_filter[idx + 1]
123 links = topo.get_active_connecting_links(
124 node1, node2, filter_list_node1=n1_list,
125 filter_list_node2=n2_list
129 f"No link between {node1[u'host']} and {node2[u'host']}"
132 # Not using set operations, as we need deterministic order.
134 l_set = [link for link in links if link in self._links]
136 l_set = [link for link in links if link not in self._links]
139 f"No free link between {node1[u'host']} and "
140 f"{node2[u'host']}, all links already used"
148 self._links.append(link)
150 use_subsequent = not topo_has_dut
151 interface1 = topo.get_interface_by_link_name(node1, link)
152 interface2 = topo.get_interface_by_link_name(node2, link,
154 self._path.append((interface1, node1))
155 self._path.append((interface2, node2))
157 self._path_iter.extend(self._path)
158 self._path_iter.reverse()
160 def next_interface(self):
161 """Path interface iterator.
163 :returns: Interface and node or None if not next interface.
164 :rtype: tuple (str, dict)
166 .. note:: Call compute_path before.
168 if not self._path_iter:
170 return self._path_iter.pop()
172 def first_interface(self):
173 """Return first interface on the path.
175 :returns: Interface and node.
176 :rtype: tuple (str, dict)
178 .. note:: Call compute_path before.
181 raise RuntimeError(u"No path for topology")
184 def last_interface(self):
185 """Return last interface on the path.
187 :returns: Interface and node.
188 :rtype: tuple (str, dict)
190 .. note:: Call compute_path before.
193 raise RuntimeError(u"No path for topology")
194 return self._path[-1]
196 def first_ingress_interface(self):
197 """Return first ingress interface on the path.
199 :returns: Interface and node.
200 :rtype: tuple (str, dict)
202 .. note:: Call compute_path before.
205 raise RuntimeError(u"No path for topology")
208 def last_egress_interface(self):
209 """Return last egress interface on the path.
211 :returns: Interface and node.
212 :rtype: tuple (str, dict)
214 .. note:: Call compute_path before.
217 raise RuntimeError(u"No path for topology")
218 return self._path[-2]
220 def compute_circular_topology(
221 self, nodes, filter_list=None, nic_pfs=1,
222 always_same_link=False, topo_has_tg=True, topo_has_dut=True):
223 """Return computed circular path.
225 :param nodes: Nodes to append to the path.
226 :param filter_list: Filter criteria list.
227 :param nic_pfs: Number of PF of NIC.
228 :param always_same_link: If True use always same link between two nodes
229 in path. If False use different link (if available)
230 between two nodes if one link was used before.
231 :param topo_has_tg: If True, the topology has a TG node. If False,
232 the topology consists entirely of DUT nodes.
233 :param topo_has_dut: If True, the topology has a DUT node(s). If False,
234 the topology consists entirely of TG nodes.
236 :type filter_list: list of strings
238 :type always_same_link: bool
239 :type topo_has_tg: bool
240 :type topo_has_dut: bool
241 :returns: Topology information dictionary.
243 :raises RuntimeError: If unsupported combination of parameters.
246 t_dict[u"hosts"] = set()
248 duts = [key for key in nodes if u"DUT" in key]
249 for host in [nodes[dut][u"host"] for dut in duts]:
250 t_dict[u"hosts"].add(host)
251 t_dict[u"duts"] = duts
252 t_dict[u"duts_count"] = len(duts)
253 t_dict[u"int"] = u"pf"
255 for _ in range(0, nic_pfs // 2):
258 self.append_node(nodes[u"TG"])
260 self.append_node(nodes[u"TG"], filter_list=filter_list)
263 self.append_node(nodes[dut], filter_list=filter_list)
265 t_dict[u"hosts"].add(nodes[u"TG"][u"host"])
267 self.append_node(nodes[u"TG"])
269 self.append_node(nodes[u"TG"], filter_list=filter_list)
270 self.compute_path(always_same_link, topo_has_dut)
272 n_idx = 0 # node index
273 t_idx = 1 # TG interface index
274 d_idx = 0 # DUT interface index
277 interface, node = self.next_interface()
280 if topo_has_tg and node.get(u"type") == u"TG":
281 n_pfx = f"TG" # node prefix
282 p_pfx = f"pf{t_idx}" # physical interface prefix
283 i_pfx = f"if{t_idx}" # [backwards compatible] interface prefix
286 elif topo_has_tg and topo_has_dut:
287 # Each node has 2 interfaces, starting with 1
288 # Calculate prefixes appropriately for current
289 # path topology nomenclature:
290 # tg1_if1 -> dut1_if1 -> dut1_if2 ->
291 # [dut2_if1 -> dut2_if2 ...] -> tg1_if2
292 n_pfx = f"DUT{n_idx // 2 + 1}"
293 p_pfx = f"pf{d_idx % 2 + t_idx - 1}"
294 i_pfx = f"if{d_idx % 2 + t_idx - 1}"
297 elif not topo_has_tg and always_same_link:
298 this_host = node.get(u"host")
299 if prev_host != this_host:
300 # When moving to a new host in the path,
301 # increment the node index (n_idx) and
302 # reset DUT interface index (d_idx) to 1.
305 n_pfx = f"DUT{n_idx}"
310 raise RuntimeError(u"Unsupported combination of paramters")
312 t_dict[f"{n_pfx}"] = node
313 t_dict[f"{n_pfx}_{p_pfx}"] = [interface]
314 t_dict[f"{n_pfx}_{p_pfx}_mac"] = \
315 [Topology.get_interface_mac(node, interface)]
316 t_dict[f"{n_pfx}_{p_pfx}_vlan"] = \
317 [Topology.get_interface_vlan(node, interface)]
318 t_dict[f"{n_pfx}_{p_pfx}_pci"] = \
319 [Topology.get_interface_pci_addr(node, interface)]
320 t_dict[f"{n_pfx}_{p_pfx}_ip4_addr"] = \
321 [Topology.get_interface_ip4(node, interface)]
322 t_dict[f"{n_pfx}_{p_pfx}_ip4_prefix"] = \
323 [Topology.get_interface_ip4_prefix_length(node, interface)]
324 if f"{n_pfx}_pf_pci" not in t_dict:
325 t_dict[f"{n_pfx}_pf_pci"] = []
326 t_dict[f"{n_pfx}_pf_pci"].append(
327 Topology.get_interface_pci_addr(node, interface))
328 if f"{n_pfx}_pf_keys" not in t_dict:
329 t_dict[f"{n_pfx}_pf_keys"] = []
330 t_dict[f"{n_pfx}_pf_keys"].append(interface)
331 # Backward compatibility below
332 t_dict[f"{n_pfx.lower()}_{i_pfx}"] = interface
333 t_dict[f"{n_pfx.lower()}_{i_pfx}_mac"] = \
334 Topology.get_interface_mac(node, interface)
335 t_dict[f"{n_pfx.lower()}_{i_pfx}_pci"] = \
336 Topology.get_interface_pci_addr(node, interface)
337 t_dict[f"{n_pfx.lower()}_{i_pfx}_ip4_addr"] = \
338 Topology.get_interface_ip4(node, interface)
339 t_dict[f"{n_pfx.lower()}_{i_pfx}_ip4_prefix"] = \
340 Topology.get_interface_ip4_prefix_length(node, interface)