'Schedule non-hosted networks to the DHCP agent on the specified
host.'

def auto_schedule_networks(self, plugin, context, host):

    agents_per_network = cfg.CONF.dhcp_agents_per_network
    bindings_to_add = []
    with context.session.begin(subtransactions=True):
        fields = ['network_id', 'enable_dhcp', 'segment_id']
        subnets = plugin.get_subnets(context, fields=fields)
        net_ids = {}
        net_segment_ids = collections.defaultdict(set)
        for s in subnets:
            if s['enable_dhcp']:
                net_segment_ids[s['network_id']].add(s.get('segment_id'))
        for (network_id, segment_ids) in net_segment_ids.items():
            is_routed_network = any(segment_ids)
            net_ids[network_id] = is_routed_network
        if (not net_ids):
            LOG.debug('No    non-hosted    networks')
            return False
        query = context.session.query(agent_model.Agent)
        query = query.filter((agent_model.Agent.agent_type == constants.AGENT_TYPE_DHCP), (agent_model.Agent.host == host), (agent_model.Agent.admin_state_up == sql.true()))
        dhcp_agents = query.all()
        segment_host_mapping = network.SegmentHostMapping.get_objects(context, host=host)
        segments_on_host = {s.segment_id for s in segment_host_mapping}
        for dhcp_agent in dhcp_agents:
            if agent_utils.is_agent_down(dhcp_agent.heartbeat_timestamp):
                LOG.warning(_LW('DHCP    agent    %s    is    not    active'), dhcp_agent.id)
                continue
            for (net_id, is_routed_network) in net_ids.items():
                agents = plugin.get_dhcp_agents_hosting_networks(context, [net_id])
                segments_on_network = net_segment_ids[net_id]
                if is_routed_network:
                    if (len((segments_on_network & segments_on_host)) == 0):
                        continue
                elif (len(agents) >= agents_per_network):
                    continue
                if any(((dhcp_agent.id == agent.id) for agent in agents)):
                    continue
                net = plugin.get_network(context, net_id)
                az_hints = (net.get(az_ext.AZ_HINTS) or cfg.CONF.default_availability_zones)
                if (az_hints and (dhcp_agent['availability_zone'] not in az_hints)):
                    continue
                bindings_to_add.append((dhcp_agent, net_id))
    for (agent, net_id) in bindings_to_add:
        self.resource_filter.bind(context, [agent], net_id)
    return True
