CSIT-425: HC Test: NSH-SFC test suite
[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
16 from ipaddress import IPv4Network, ip_address
17
18 from resources.libraries.python.ssh import SSH
19 from resources.libraries.python.constants import Constants
20 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
21 from resources.libraries.python.topology import Topology
22
23
24 class IPUtil(object):
25     """Common IP utilities"""
26
27     @staticmethod
28     def vpp_ip_probe(node, interface, addr, if_type="key"):
29         """Run ip probe on VPP node.
30
31         :param node: VPP node.
32         :param interface: Interface key or name.
33         :param addr: IPv4/IPv6 address.
34         :param if_type: Interface type
35         :type node: dict
36         :type interface: str
37         :type addr: str
38         :type if_type: str
39         :raises ValueError: If the if_type is unknown.
40         :raises Exception: If vpp probe fails.
41         """
42         ssh = SSH()
43         ssh.connect(node)
44
45         if if_type == "key":
46             iface_name = Topology.get_interface_name(node, interface)
47         elif if_type == "name":
48             iface_name = interface
49         else:
50             raise ValueError("if_type unknown: {0}".format(if_type))
51
52         cmd = "{c}".format(c=Constants.VAT_BIN_NAME)
53         cmd_input = 'exec ip probe {dev} {ip}'.format(dev=iface_name, ip=addr)
54         (ret_code, _, _) = ssh.exec_command_sudo(cmd, cmd_input)
55         if int(ret_code) != 0:
56             raise Exception('VPP ip probe {dev} {ip} failed on {h}'.format(
57                 dev=iface_name, ip=addr, h=node['host']))
58
59     @staticmethod
60     def ip_addresses_should_be_equal(ip1, ip2):
61         """Fails if the given IP addresses are unequal.
62
63         :param ip1: IPv4 or IPv6 address.
64         :param ip2: IPv4 or IPv6 address.
65         :type ip1: str
66         :type ip2: str
67         """
68
69         addr1 = ip_address(unicode(ip1))
70         addr2 = ip_address(unicode(ip2))
71
72         if addr1 != addr2:
73             raise AssertionError('IP addresses are not equal: {0} != {1}'.
74                                  format(ip1, ip2))
75
76     @staticmethod
77     def setup_network_namespace(node, namespace_name, interface_name,
78                                 ip_address, prefix):
79         """Setup namespace on given node and attach interface and IP to
80         this namespace. Applicable also on TG node.
81
82         :param node: Node to set namespace on.
83         :param namespace_name: Namespace name.
84         :param interface_name: Interface name.
85         :param ip_address: IP address of namespace's interface.
86         :param prefix: IP address prefix length.
87         :type node: dict
88         :type namespace_name: str
89         :type vhost_if: str
90         :type ip_address: str
91         :type prefix: int
92         """
93         cmd = ('ip netns add {0}'.format(namespace_name))
94         exec_cmd_no_error(node, cmd, sudo=True)
95
96         cmd = ('ip link set dev {0} up netns {1}'.format(interface_name,
97                                                          namespace_name))
98         exec_cmd_no_error(node, cmd, sudo=True)
99
100         cmd = ('ip netns exec {0} ip addr add {1}/{2} dev {3}'.format(
101             namespace_name, ip_address, prefix, interface_name))
102         exec_cmd_no_error(node, cmd, sudo=True)
103
104     @staticmethod
105     def linux_enable_forwarding(node, ip_ver='ipv4'):
106         """Enable forwarding on a Linux node, e.g. VM.
107
108         :param node: Node to enable forwarding on.
109         :param ip_ver: IP version, 'ipv4' or 'ipv6'.
110         :type node: dict
111         :type ip_ver: str
112         """
113         cmd = 'sysctl -w net.{0}.ip_forward=1'.format(ip_ver)
114         exec_cmd_no_error(node, cmd, sudo=True)
115
116     @staticmethod
117     def set_linux_interface_ip(node, interface, ip, prefix, namespace=None):
118         """Set IP address to interface in linux.
119
120         :param node: Node where to execute command.
121         :param interface: Interface in namespace.
122         :param ip: IP to be set on interface.
123         :param prefix: IP prefix.
124         :param namespace: Execute command in namespace. Optional
125         :type node: dict
126         :type interface: str
127         :type ip: str
128         :type prefix: int
129         :type namespace: str
130         :raises RuntimeError: IP could not be set.
131         """
132         if namespace is not None:
133             cmd = 'ip netns exec {} ip addr add {}/{} dev {}'.format(
134                 namespace, ip, prefix, interface)
135         else:
136             cmd = 'ip addr add {}/{} dev {}'.format(ip, prefix, interface)
137         (rc, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
138         if rc != 0:
139             raise RuntimeError(
140                 'Could not set IP for interface, reason:{}'.format(stderr))
141
142     @staticmethod
143     def set_linux_interface_route(node, interface, route, namespace=None):
144         """Set route via interface in linux.
145
146         :param node: Node where to execute command.
147         :param interface: Interface in namespace.
148         :param route: Route to be added via interface.
149         :param namespace: Execute command in namespace. Optional parameter.
150         :type node: dict
151         :type interface: str
152         :type route: str
153         :type namespace: str
154         """
155         if namespace is not None:
156             cmd = 'ip netns exec {} ip route add {} dev {}'.format(
157                 namespace, route, interface)
158         else:
159             cmd = 'ip route add {} dev {}'.format(route, interface)
160         exec_cmd_no_error(node, cmd, sudo=True)
161
162
163 def convert_ipv4_netmask_prefix(network):
164     """Convert network mask to equivalent network prefix length or vice versa.
165
166     Example: mask 255.255.0.0 -> prefix length 16
167     :param network: Network mask or network prefix length.
168     :type network: str or int
169     :return: Network mask or network prefix length.
170     :rtype: str or int
171     """
172     temp_address = "0.0.0.0"
173     net = IPv4Network(u"{0}/{1}".format(temp_address, network), False)
174
175     if isinstance(network, int) and (0 < network < 33):
176         return str(net.netmask)
177     elif isinstance(network, basestring):
178         return int(net.prefixlen)
179     else:
180         raise Exception("Value {0} is not a valid ipv4 netmask or network"
181                         " prefix length".format(network))