1# -----------------------------------------------------------------------------
2# Copyright (c) 2008 by David P. D. Moss. All rights reserved.
3#
4# Released under the BSD license. See the LICENSE file for details.
5# -----------------------------------------------------------------------------
6from netaddr.ip import IPNetwork, cidr_exclude, cidr_merge
7
8
9class SubnetSplitter(object):
10 """
11 A handy utility class that takes a single (large) subnet and allows
12 smaller subnet within its range to be extracted by CIDR prefix. Any
13 leaving address space is available for subsequent extractions until
14 all space is exhausted.
15 """
16
17 def __init__(self, base_cidr):
18 """
19 Constructor.
20
21 :param base_cidr: an IPv4 or IPv6 address with a CIDR prefix.
22 (see IPNetwork.__init__ for full details).
23 """
24 self._subnets = set([IPNetwork(base_cidr)])
25
26 def extract_subnet(self, prefix, count=None):
27 """Extract 1 or more subnets of size specified by CIDR prefix."""
28 for cidr in self.available_subnets():
29 subnets = list(cidr.subnet(prefix, count=count))
30 if not subnets:
31 continue
32 self.remove_subnet(cidr)
33 self._subnets = self._subnets.union(set(cidr_exclude(cidr, cidr_merge(subnets)[0])))
34 return subnets
35 return []
36
37 def available_subnets(self):
38 """Returns a list of the currently available subnets."""
39 return sorted(self._subnets, key=lambda x: x.prefixlen, reverse=True)
40
41 def remove_subnet(self, ip_network):
42 """Remove a specified IPNetwork from available address space."""
43 self._subnets.remove(ip_network)