Ejemplo n.º 1
0
def iter_missing_networks_from_tree(tree):
    """iter_missing_networks wrapper for network trees."""
    #    base = Address(0, 0)
    base = ipaddr.IPNetwork('::/0', version=6)
    children = tree.listChildren(include=['ipv6 network'])
    children = [c.address for c in children]
    return iter_missing_networks(base, children)
Ejemplo n.º 2
0
    def _loaded(self, data=None):
        """Called for an existing network being loaded.

        Creates self.address from storage.
        """
        super(Network, self)._loaded(data)
        self.address = ipaddr.IPNetwork(data['netstring'])
Ejemplo n.º 3
0
def address_from_string(address, mask=True, validate=True):
    """Return an Address object matching an address string.

    The address string must be an ipv6 address.

    If an Address object is passed in it is returned untouched.
    """
    try:
        ret = ipaddr.IPNetwork(address,
                               version=6,
                               strict=validate,
                               mask_address=mask)
    except ipaddr.AddressValueError, e:
        raise errors.InvalidNetworkAddress(str(e))
Ejemplo n.º 4
0
def find_network_parent(network_tree, address):
    """Find the nearest (direct) existing parent of this network.
        
        Starts from the network tree and searches through the existing
        networks until the smallest possible parent is found.
        """
    address = ipaddr.IPNetwork(address, version=6)

    parent = network_tree
    while True:
        prev_parent = parent
        for net in parent.listChildren(include=[Network.class_name]):
            # Check if net.address is a supernet of address (but not the exact same size).
            if address in net.address and address != net.address:
                parent = net
                break
        # Found our nearest parent.
        if parent is prev_parent:
            break
    return parent
Ejemplo n.º 5
0
    def _created(self, user):
        """Perform setup for a newly created network.

        This includes several steps. Make sure we match the protocol of
        the network tree we have been created in.
        Find our real parent, since we likely have been created as a child
        of the network tree, and relocate to it.
        Find any children (subnets) of ourselves and relocate to be children
        of us.
        """
        super(Network, self)._created(user)
        network_tree = self.getParent('network tree')
        if network_tree.protocol != 'ipv6':
            raise errors.SiptrackError(
                'network type doesn\'t match network tree protocol')

        # If we were passed a string, convert it, otherwise assume
        # it's an Address object already.
        self.address = ipaddr.IPNetwork(self.address,
                                        version=6,
                                        mask_address=True)
        self.storageAction('write_data', {
            'name': 'netstring',
            'value': str(self.address)
        })

        # Be really sure that this network is in the correct place.
        parent = find_network_parent(network_tree, self.address)
        if parent.oid != self.parent.oid:
            raise errors.SiptrackError('invalid network location')

        # Make sure an identical network doesn't exist here.
        for network in parent.listChildren(include=['ipv6 network']):
            if self.oid == network.oid:
                continue
            if self.address == network.address:
                raise errors.SiptrackError('network already exists %s' %
                                           (self.address))

        self._collectChildren()
Ejemplo n.º 6
0
 def addressFromString(self, address):
     return ipaddr.IPNetwork(address)