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 """Path utilities library for nodes in the topology."""
16 from resources.libraries.python.topology import Topology
19 class NodePath(object):
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()
62 def append_node(self, node):
63 """Append node to the path.
65 :param node: Node to append to the path.
68 self._nodes.append(node)
70 def append_nodes(self, *nodes):
71 """Append nodes to the path.
73 :param nodes: Nodes to append to the path.
76 .. note:: Node order does matter.
79 self.append_node(node)
88 def compute_path(self, always_same_link=True):
89 """Compute path for added nodes.
91 :param always_same_link: If True use always same link between two nodes
92 in path. If False use different link (if available) between two
93 nodes if one link was used before.
94 :type always_same_link: bool
96 .. note:: First add at least two nodes to the topology.
100 raise RuntimeError('Not enough nodes to compute path')
102 for idx in range(0, len(nodes) - 1):
105 node2 = nodes[idx + 1]
106 links = topo.get_active_connecting_links(node1, node2)
108 raise RuntimeError('No link between {0} and {1}'.format(
109 node1['host'], node2['host']))
112 l_set = set(links).intersection(self._links)
114 l_set = set(links).difference(self._links)
117 'No free link between {0} and {1}, all links already '
118 'used'.format(node1['host'], node2['host']))
125 self._links.append(link)
126 interface1 = topo.get_interface_by_link_name(node1, link)
127 interface2 = topo.get_interface_by_link_name(node2, link)
128 self._path.append((interface1, node1))
129 self._path.append((interface2, node2))
131 self._path_iter.extend(self._path)
132 self._path_iter.reverse()
134 def next_interface(self):
135 """Path interface iterator.
137 :return: Interface and node or None if not next interface.
138 :rtype: tuple (str, dict)
140 .. note:: Call compute_path before.
142 if not self._path_iter:
145 return self._path_iter.pop()
147 def first_interface(self):
148 """Return first interface on the path.
150 :return: Interface and node.
151 :rtype: tuple (str, dict)
153 .. note:: Call compute_path before.
156 raise RuntimeError('No path for topology')
159 def last_interface(self):
160 """Return last interface on the path.
162 :return: Interface and node.
163 :rtype: tuple (str, dict)
165 .. note:: Call compute_path before.
168 raise RuntimeError('No path for topology')
169 return self._path[-1]
171 def first_ingress_interface(self):
172 """Return first ingress interface on the path.
174 :return: Interface and node.
175 :rtype: tuple (str, dict)
177 .. note:: Call compute_path before.
180 raise RuntimeError('No path for topology')
183 def last_egress_interface(self):
184 """Return last egress interface on the path.
186 :return: Interface and node.
187 :rtype: tuple (str, dict)
189 .. note:: Call compute_path before.
192 raise RuntimeError('No path for topology')
193 return self._path[-2]