'Remove networks from down DHCP agents if admin state is up.
Reschedule them if configured so.'

def remove_networks_from_down_agents(self):

    agent_dead_limit = self.agent_dead_limit_seconds()
    self.wait_down_agents('DHCP', agent_dead_limit)
    cutoff = self.get_cutoff_time(agent_dead_limit)
    context = ncontext.get_admin_context()
    try:
        down_bindings = context.session.query(ndab_model.NetworkDhcpAgentBinding).join(agent_model.Agent).filter((agent_model.Agent.heartbeat_timestamp < cutoff), agent_model.Agent.admin_state_up)
        dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP)
        dead_bindings = [b for b in self._filter_bindings(context, down_bindings)]
        agents = self.get_agents_db(context, {'agent_type': [constants.AGENT_TYPE_DHCP]})
        if (not agents):
            return
        active_agents = [agent for agent in agents if self.is_eligible_agent(context, True, agent)]
        if (not active_agents):
            LOG.warning(_LW('No    DHCP    agents    available,    skipping    rescheduling'))
            return
        for binding in dead_bindings:
            LOG.warning(_LW('Removing    network    %(network)s    from    agent    %(agent)s    because    the    agent    did    not    report    to    the    server    in    the    last    %(dead_time)s    seconds.'), {'network': binding.network_id, 'agent': binding.dhcp_agent_id, 'dead_time': agent_dead_limit})
            saved_binding = {'net': binding.network_id, 'agent': binding.dhcp_agent_id}
            try:
                self.remove_network_from_dhcp_agent(context, binding.dhcp_agent_id, binding.network_id, notify=False)
            except dhcpagentscheduler.NetworkNotHostedByDhcpAgent:
                LOG.debug('Network    %(net)s    already    removed    from    DHCP    agent    %(agent)s', saved_binding)
            except Exception:
                LOG.exception(_LE('Unexpected    exception    occurred    while    removing    network    %(net)s    from    agent    %(agent)s'), saved_binding)
            if cfg.CONF.network_auto_schedule:
                self._schedule_network(context, saved_binding['net'], dhcp_notifier)
    except Exception:
        LOG.exception(_LE('Exception    encountered    during    network    rescheduling'))
