'Validate that the tenant can use the requested networks.
Return the number of instances than can be successfully allocated
with the requested network configuration.'

def validate_networks(self, context, requested_networks, num_instances):

    LOG.debug('validate_networks()    for    %s', requested_networks)
    neutron = get_client(context)
    ports_needed_per_instance = self._ports_needed_per_instance(context, neutron, requested_networks)
    if ports_needed_per_instance:
        quotas = neutron.show_quota(context.project_id)['quota']
        if (quotas.get('port', (-1)) == (-1)):
            return num_instances
        params = dict(tenant_id=context.project_id, fields=['id'])
        ports = neutron.list_ports(**params)['ports']
        free_ports = (quotas.get('port') - len(ports))
        if (free_ports < 0):
            msg = (_('The    number    of    defined    ports:    %(ports)d    is    over    the    limit:    %(quota)d') % {'ports': len(ports), 'quota': quotas.get('port')})
            raise exception.PortLimitExceeded(msg)
        ports_needed = (ports_needed_per_instance * num_instances)
        if (free_ports >= ports_needed):
            return num_instances
        else:
            return (free_ports // ports_needed_per_instance)
    return num_instances
