97629c0cbba74c7c78c80cc2f21711fe4bd99bee
[integration/test.git] / csit / libraries / SFC / SfcUtils.py
1 import ipaddr
2
3 __author__ = "Jose Luis Franco Arza"
4 __copyright__ = "Copyright(c) 2016, Ericsson."
5 __license__ = "New-style BSD"
6 __email__ = "jose.luis.franco.arza@ericsson.com"
7
8
9 def get_network_from_cidr(cidr):
10     '''
11     Returns the subnetwork part from a given subnet in CIDR format,
12     like 192.168.1.0/24. Returning 192.168.1.0.
13     '''
14     o = ipaddr.IPv4Network(cidr)
15     return str(o.network)
16
17
18 def get_mask_from_cidr(cidr):
19     '''
20     Returns a subnet mask from a given subnet in CIDR format,
21     like 192.168.1.0/24. Returning 255.255.255.0.
22     '''
23     o = ipaddr.IPv4Network(cidr)
24     return str(o.netmask)
25
26
27 def get_ip_address_first_octets(ip, n_octets):
28     '''
29     Given an IP address, this function returns the number
30     of octets determined as argument. If 4 are specified, then the output
31     is the whole IP
32     '''
33
34     return ".".join(ip.split(".")[:int(n_octets)])