Add optional args to traffic script arg parser
[csit.git] / resources / libraries / python / IPv6Util.py
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:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
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.
13
14 """IPv6 utilities library."""
15
16 import re
17
18 from resources.libraries.python.ssh import SSH
19
20
21 class IPv6Util(object):
22     """IPv6 utilities"""
23
24     @staticmethod
25     def ipv6_ping(src_node, dst_addr, count=3, data_size=56, timeout=1):
26         """IPv6 ping.
27
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)
33         :type src_node: dict
34         :type dst_addr: str
35         :type count: int
36         :type data_size: int
37         :type timeout: int
38         :return: Number of lost packets.
39         :rtype: int
40         """
41         ssh = SSH()
42         ssh.connect(src_node)
43
44         cmd = "ping6 -c {c} -s {s} -W {W} {dst}".format(c=count, s=data_size,
45                                                         W=timeout,
46                                                         dst=dst_addr)
47         (ret_code, stdout, _) = ssh.exec_command(cmd)
48
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)
53
54         return packet_lost
55
56     @staticmethod
57     def ipv6_ping_port(nodes_ip, src_node, dst_node, port, cnt=3,
58                        size=56, timeout=1):
59         """Send IPv6 ping to the node port.
60
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)
68         :type nodes_ip: dict
69         :type src_node: dict
70         :type dst_node: dict
71         :type port: str
72         :type cnt: int
73         :type size: int
74         :type timeout: int
75         :return: Number of lost packets.
76         :rtype: int
77         """
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)
80
81     @staticmethod
82     def get_node_port_ipv6_address(node, interface, nodes_addr):
83         """Return IPv6 address of the node port.
84
85         :param node: Node in the topology.
86         :param interface: Interface name of the node.
87         :param nodes_addr: Nodes IPv6 addresses.
88         :type node: dict
89         :type interface: str
90         :type nodes_addr: dict
91         :return: IPv6 address string.
92         :rtype: str
93         """
94         for net in nodes_addr.values():
95             for port in net['ports'].values():
96                 host = port.get('node')
97                 dev = port.get('if')
98                 if host == node['host'] and dev == interface:
99                     ip = port.get('addr')
100                     if ip is not None:
101                         return ip
102                     else:
103                         raise Exception(
104                             'Node {n} port {p} IPv6 address is not set'.format(
105                                 n=node['host'], p=interface))
106
107         raise Exception('Node {n} port {p} IPv6 address not found.'.format(
108             n=node['host'], p=interface))