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 """IPv6 utilities library."""
18 from resources.libraries.python.ssh import SSH
21 class IPv6Util(object):
25 def ipv6_ping(src_node, dst_addr, count=3, data_size=56, timeout=1):
28 :param src_node: Node where ping run.
29 :param dst_addr: Destination IPv6 address.
30 :param count: Number of echo requests. (Optional)
31 :param data_size: Number of the data bytes. (Optional)
32 :param timeout: Time to wait for a response, in seconds. (Optional)
38 :return: Number of lost packets.
44 cmd = "ping6 -c {c} -s {s} -W {W} {dst}".format(c=count, s=data_size,
47 (ret_code, stdout, _) = ssh.exec_command(cmd)
49 regex = re.compile(r'(\d+) packets transmitted, (\d+) received')
50 match = regex.search(stdout)
51 sent, received = match.groups()
52 packet_lost = int(sent) - int(received)
57 def ipv6_ping_port(nodes_ip, src_node, dst_node, port, cnt=3,
59 """Send IPv6 ping to the node port.
61 :param nodes_ip: Nodes IPv6 addresses.
62 :param src_node: Node where ping run.
63 :param dst_node: Destination node.
64 :param port: Port on the destination node.
65 :param cnt: Number of echo requests. (Optional)
66 :param size: Number of the data bytes. (Optional)
67 :param timeout: Time to wait for a response, in seconds. (Optional)
75 :return: Number of lost packets.
78 dst_ip = IPv6Util.get_node_port_ipv6_address(dst_node, port, nodes_ip)
79 return IPv6Util.ipv6_ping(src_node, dst_ip, cnt, size, timeout)
82 def get_node_port_ipv6_address(node, interface, nodes_addr):
83 """Return IPv6 address of the node port.
85 :param node: Node in the topology.
86 :param interface: Interface name of the node.
87 :param nodes_addr: Nodes IPv6 addresses.
90 :type nodes_addr: dict
91 :return: IPv6 address string.
94 for net in nodes_addr.values():
95 for port in net['ports'].values():
96 host = port.get('node')
98 if host == node['host'] and dev == interface:
104 'Node {n} port {p} IPv6 address is not set'.format(
105 n=node['host'], p=interface))
107 raise Exception('Node {n} port {p} IPv6 address not found.'.format(
108 n=node['host'], p=interface))