summaryrefslogtreecommitdiffstats
path: root/lib/python
diff options
context:
space:
mode:
authorFeng Pan <fpan@redhat.com>2016-04-15 11:43:34 -0400
committerFeng Pan <fpan@redhat.com>2016-04-21 09:37:20 -0400
commit3d56d3211e2ddee0825e79174cfdfbcda70cd20b (patch)
treed67789ed8ec95857711f57b297f0048106cc37ff /lib/python
parenta412ab4c4a4835bb4a9f4bf0f2ef8c7e965f1aaf (diff)
Adds python IP utility library
Changes include: - IP utility library in python 3 that supports both IPv4 and IPv6 address generation. This library currently includes a single function of generating IP ranges or single IP for a given CIDR. More functionality will be added at a later time to support features such as IP address calculation. - Updated common-function.sh to use python library to generate IP ranges. All existing bash functions are preserved, so any callers will get identical IP ranges as before. - Add dependency to python3 for opnfv-apex-common package. - Add python dependency to build.sh No change is made to interface related functions. Change-Id: Idc6998754f9f3c7a3868ec5b5768f3bb5f78cd90 Signed-off-by: Feng Pan <fpan@redhat.com>
Diffstat (limited to 'lib/python')
-rw-r--r--lib/python/apex/__init__.py9
-rw-r--r--lib/python/apex/ip_utils.py54
2 files changed, 63 insertions, 0 deletions
diff --git a/lib/python/apex/__init__.py b/lib/python/apex/__init__.py
new file mode 100644
index 00000000..0c0ae6c6
--- /dev/null
+++ b/lib/python/apex/__init__.py
@@ -0,0 +1,9 @@
+##############################################################################
+# Copyright (c) 2016 Feng Pan (fpan@redhat.com) and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
diff --git a/lib/python/apex/ip_utils.py b/lib/python/apex/ip_utils.py
new file mode 100644
index 00000000..680ce7e0
--- /dev/null
+++ b/lib/python/apex/ip_utils.py
@@ -0,0 +1,54 @@
+
+##############################################################################
+# Copyright (c) 2016 Feng Pan (fpan@redhat.com) and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Apache License, Version 2.0
+# which accompanies this distribution, and is available at
+# http://www.apache.org/licenses/LICENSE-2.0
+##############################################################################
+
+
+import ipaddress
+
+
+def generate_ip_range(args):
+ """
+ Generate IP range in string format for given CIDR.
+ This function works for both IPv4 and IPv6.
+
+ args is expected to contain the following members:
+ CIDR: any valid CIDR representation.
+ start_position: starting index, default to first address in subnet (1)
+ end_position: ending index, default to last address in subnet (-1)
+
+ Returns IP range in string format. A single IP is returned if start and end IPs are identical.
+ """
+ cidr = ipaddress.ip_network(args.CIDR)
+ (start_index, end_index) = (args.start_position, args.end_position)
+ if cidr[start_index] == cidr[end_index]:
+ return str(cidr[start_index])
+ else:
+ return ','.join(sorted([str(cidr[start_index]), str(cidr[end_index])]))
+
+
+def main():
+ import argparse
+ import sys
+
+ parser = argparse.ArgumentParser()
+ subparsers = parser.add_subparsers()
+
+ parser_gen_ip_range = subparsers.add_parser('generate_ip_range', help='Generate IP Range given CIDR')
+ parser_gen_ip_range.add_argument('CIDR', help='Network in CIDR notation')
+ parser_gen_ip_range.add_argument('start_position', type=int, help='Starting index')
+ parser_gen_ip_range.add_argument('end_position', type=int, help='Ending index')
+ parser_gen_ip_range.set_defaults(func=generate_ip_range)
+
+ args = parser.parse_args(sys.argv[1:])
+ print(args.func(args))
+
+
+if __name__ == '__main__':
+ main()
+