summaryrefslogtreecommitdiffstats
path: root/netaddr/contrib/subnet_splitter.py
blob: 875b1131ede3447f3a451ee1ade7621582bc559b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#-----------------------------------------------------------------------------
#   Copyright (c) 2008 by David P. D. Moss. All rights reserved.
#
#   Released under the BSD license. See the LICENSE file for details.
#-----------------------------------------------------------------------------
from netaddr.ip import IPNetwork, cidr_exclude, cidr_merge


class SubnetSplitter(object):
    """
    A handy utility class that takes a single (large) subnet and allows
    smaller subnet within its range to be extracted by CIDR prefix. Any
    leaving address space is available for subsequent extractions until
    all space is exhausted.
    """
    def __init__(self, base_cidr):
        """
        Constructor.

        :param base_cidr: an IPv4 or IPv6 address with a CIDR prefix.
            (see IPNetwork.__init__ for full details).
        """
        self._subnets = set([IPNetwork(base_cidr)])

    def extract_subnet(self, prefix, count=None):
        """Extract 1 or more subnets of size specified by CIDR prefix."""
        for cidr in self.available_subnets():
            subnets = list(cidr.subnet(prefix, count=count))
            if not subnets:
                continue
            self.remove_subnet(cidr)
            self._subnets = self._subnets.union(
                set(
                    cidr_exclude(cidr, cidr_merge(subnets)[0])
                )
            )
            return subnets
        return []

    def available_subnets(self):
        """Returns a list of the currently available subnets."""
        return sorted(self._subnets, key=lambda x: x.prefixlen, reverse=True)

    def remove_subnet(self, ip_network):
        """Remove a specified IPNetwork from available address space."""
        self._subnets.remove(ip_network)