Use interface key instead of interface name.
[csit.git] / resources / libraries / python / IPUtil.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 """Common IP utilities library."""
15 from ipaddress import IPv4Network
16
17 from resources.libraries.python.ssh import SSH
18 from resources.libraries.python.constants import Constants
19
20 from resources.libraries.python.topology import Topology
21
22 class IPUtil(object):
23     """Common IP utilities"""
24
25     @staticmethod
26     def vpp_ip_probe(node, interface, addr, if_type="key"):
27         """Run ip probe on VPP node.
28
29         :param node: VPP node.
30         :param interface: Interface key or name.
31         :param addr: IPv4/IPv6 address.
32         :param if_type: Interface type
33         :type node: dict
34         :type interface: str
35         :type addr: str
36         :type if_type: str
37         :raises ValueError: If the if_type is unknown.
38         :raises Exception: If vpp probe fails.
39         """
40         ssh = SSH()
41         ssh.connect(node)
42
43         if if_type == "key":
44             iface_name = Topology.get_interface_name(node, interface)
45         elif if_type == "name":
46             iface_name = interface
47         else:
48             raise ValueError("if_type unknown: {}".format(if_type))
49
50         cmd = "{c}".format(c=Constants.VAT_BIN_NAME)
51         cmd_input = 'exec ip probe {dev} {ip}'.format(dev=iface_name, ip=addr)
52         (ret_code, _, _) = ssh.exec_command_sudo(cmd, cmd_input)
53         if int(ret_code) != 0:
54             raise Exception('VPP ip probe {dev} {ip} failed on {h}'.format(
55                 dev=iface_name, ip=addr, h=node['host']))
56
57
58 def convert_ipv4_netmask_prefix(network):
59     """Convert network mask to equivalent network prefix length or vice versa.
60
61     Example: mask 255.255.0.0 -> prefix length 16
62     :param network: Network mask or network prefix length.
63     :type network: str or int
64     :return: Network mask or network prefix length.
65     :rtype: str or int
66     """
67     temp_address = "0.0.0.0"
68     net = IPv4Network(u"{0}/{1}".format(temp_address, network), False)
69
70     if isinstance(network, int) and (0 < network < 33):
71         return str(net.netmask)
72     elif isinstance(network, basestring):
73         return int(net.prefixlen)
74     else:
75         raise Exception("Value {0} is not a valid ipv4 netmask or network"
76                         " prefix length".format(network))