Java library to find if 2 CIDR's overlap

Asked by Ritesh Shetty

Assuming Quantum doesnt have a mechanism to query and see if a new subnet CIDR that i want to create doesnt overlaps with the existing subnet CIDR's. I want to do this before creating a subnet, i want to find if an overlapping subnet cidr exists.

If above is true then..

I know this is not directly related to the Quantum work, I need a Java library or Algorithm to find if 2 CIDR's overlap.

Any help is greatly appreciated

Question information

Language:
English Edit question
Status:
Answered
For:
neutron Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
yong sheng gong (gongysh) said :
#1

this is python one quantum is using:
    def _validate_subnet_cidr(self, context, network, new_subnet_cidr):
        """Validate the CIDR for a subnet.

        Verifies the specified CIDR does not overlap with the ones defined
        for the other subnets specified for this network, or with any other
        CIDR if overlapping IPs are disabled.
        """
        new_subnet_ipset = netaddr.IPSet([new_subnet_cidr])
        if cfg.CONF.allow_overlapping_ips:
            subnet_list = network.subnets
        else:
            subnet_list = self._get_all_subnets(context)
        for subnet in subnet_list:
            if (netaddr.IPSet([subnet.cidr]) & new_subnet_ipset):
                # don't give out details of the overlapping subnet
                err_msg = (_("Requested subnet with cidr: %(cidr)s for "
                             "network: %(network_id)s overlaps with another "
                             "subnet") %
                           {'cidr': new_subnet_cidr,
                            'network_id': network.id})
                LOG.error(_("Validation for CIDR: %(new_cidr)s failed - "
                            "overlaps with subnet %(subnet_id)s "
                            "(CIDR: %(cidr)s)"),
                          {'new_cidr': new_subnet_cidr,
                           'subnet_id': subnet.id,
                           'cidr': subnet.cidr})
                raise q_exc.InvalidInput(error_message=err_msg)

Can you help with this problem?

Provide an answer of your own, or ask Ritesh Shetty for more information if necessary.

To post a message you must log in.