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 """Common IP utilities library."""
15 from ipaddress import IPv4Network
17 from resources.libraries.python.ssh import SSH
18 from resources.libraries.python.constants import Constants
22 """Common IP utilities"""
25 def vpp_ip_probe(node, interface, addr):
26 """Run ip probe on VPP node.
28 :param node: VPP node.
29 :param interface: Interface name.
30 :param addr: IPv4/IPv6 address.
38 cmd = "{c}".format(c=Constants.VAT_BIN_NAME)
39 cmd_input = 'exec ip probe {dev} {ip}'.format(dev=interface, ip=addr)
40 (ret_code, _, _) = ssh.exec_command_sudo(cmd, cmd_input)
41 if int(ret_code) != 0:
42 raise Exception('VPP ip probe {dev} {ip} failed on {h}'.format(
43 dev=interface, ip=addr, h=node['host']))
46 def convert_ipv4_netmask_prefix(network):
47 """Convert network mask to equivalent network prefix length or vice versa.
49 Example: mask 255.255.0.0 -> prefix length 16
50 :param network: Network mask or network prefix length.
51 :type network: str or int
52 :return: Network mask or network prefix length.
55 temp_address = "0.0.0.0"
56 net = IPv4Network(u"{0}/{1}".format(temp_address, network), False)
58 if isinstance(network, int) and (0 < network < 33):
60 elif isinstance(network, basestring):
63 raise Exception("Value {0} is not a valid ipv4 netmask or network"
64 " prefix length".format(network))