'Create a subnet for the given network
within the cidr block configured for tenant networks.'

def _create_subnet(self, network, subnets_client=None, routers_client=None, namestart='subnet-smoke', **kwargs):

    if (not subnets_client):
        subnets_client = self.subnets_client
    if (not routers_client):
        routers_client = self.routers_client
    def cidr_in_use(cidr, tenant_id):
        'Check    cidr    existence\n\n                                                :returns:    True    if    subnet    with    cidr    already    exist    in    tenant\n                                                                        False    else\n                                                '
        cidr_in_use = self.os_admin.subnets_client.list_subnets(tenant_id=tenant_id, cidr=cidr)['subnets']
        return (len(cidr_in_use) != 0)
    ip_version = kwargs.pop('ip_version', 4)
    if (ip_version == 6):
        tenant_cidr = netaddr.IPNetwork(CONF.network.project_network_v6_cidr)
        num_bits = CONF.network.project_network_v6_mask_bits
    else:
        tenant_cidr = netaddr.IPNetwork(CONF.network.project_network_cidr)
        num_bits = CONF.network.project_network_mask_bits
    result = None
    str_cidr = None
    for subnet_cidr in tenant_cidr.subnet(num_bits):
        str_cidr = str(subnet_cidr)
        if cidr_in_use(str_cidr, tenant_id=network['tenant_id']):
            continue
        subnet = dict(name=data_utils.rand_name(namestart), network_id=network['id'], tenant_id=network['tenant_id'], cidr=str_cidr, ip_version=ip_version, **kwargs)
        try:
            result = subnets_client.create_subnet(**subnet)
            break
        except lib_exc.Conflict as e:
            is_overlapping_cidr = ('overlaps    with    another    subnet' in str(e))
            if (not is_overlapping_cidr):
                raise
    self.assertIsNotNone(result, 'Unable    to    allocate    tenant    network')
    subnet = result['subnet']
    self.assertEqual(subnet['cidr'], str_cidr)
    self.addCleanup(test_utils.call_and_ignore_notfound_exc, subnets_client.delete_subnet, subnet['id'])
    return subnet
