def test_filter_bindings(self):
        self.ctx = context.get_admin_context()
        dhcp_agt_ids = self._create_dhcp_agents()
        network_ids = sorted(self._create_test_networks(num_net=4))
        ndab_obj1 = network_obj.NetworkDhcpAgentBinding(
            self.ctx, network_id=network_ids[0], dhcp_agent_id=dhcp_agt_ids[0])
        ndab_obj1.create()
        ndab_obj2 = network_obj.NetworkDhcpAgentBinding(
            self.ctx, network_id=network_ids[1], dhcp_agent_id=dhcp_agt_ids[0])
        ndab_obj2.create()
        ndab_obj3 = network_obj.NetworkDhcpAgentBinding(
            self.ctx, network_id=network_ids[2], dhcp_agent_id=dhcp_agt_ids[1])
        ndab_obj3.create()
        ndab_obj4 = network_obj.NetworkDhcpAgentBinding(
            self.ctx, network_id=network_ids[3], dhcp_agent_id=dhcp_agt_ids[1])
        ndab_obj4.create()
        bindings_objs = sorted(network_obj.NetworkDhcpAgentBinding.get_objects(
            self.ctx),
                               key=attrgetter('network_id'))

        with mock.patch.object(self,
                               'agent_starting_up',
                               side_effect=[True, False]):
            res = [b for b in self._filter_bindings(None, bindings_objs)]
            # once per each agent id1 and id2
            self.assertEqual(2, len(res))
            res_ids = [b.network_id for b in res]
            self.assertIn(network_ids[2], res_ids)
            self.assertIn(network_ids[3], res_ids)
 def add_network_to_dhcp_agent(self, context, id, network_id):
     self._get_network(context, network_id)
     with context.session.begin(subtransactions=True):
         agent_db = self._get_agent(context, id)
         if (agent_db['agent_type'] != constants.AGENT_TYPE_DHCP or
                 not services_available(agent_db['admin_state_up'])):
             raise dhcpagentscheduler.InvalidDHCPAgent(id=id)
         dhcp_agents = self.get_dhcp_agents_hosting_networks(
             context, [network_id])
         for dhcp_agent in dhcp_agents:
             if id == dhcp_agent.id:
                 raise dhcpagentscheduler.NetworkHostedByDHCPAgent(
                     network_id=network_id, agent_id=id)
         network.NetworkDhcpAgentBinding(context, dhcp_agent_id=id,
              network_id=network_id).create()
     dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP)
     if dhcp_notifier:
         dhcp_notifier.network_added_to_agent(
             context, network_id, agent_db.host)
Exemple #3
0
 def add_network_to_dhcp_agent(self, context, id, network_id):
     self._get_network(context, network_id)
     with db_api.CONTEXT_WRITER.using(context):
         agent_db = self._get_agent(context, id)
         if (agent_db['agent_type'] != constants.AGENT_TYPE_DHCP
                 or not services_available(agent_db['admin_state_up'])):
             raise das_exc.InvalidDHCPAgent(id=id)
         dhcp_agents = self.get_dhcp_agents_hosting_networks(
             context, [network_id])
         for dhcp_agent in dhcp_agents:
             if id == dhcp_agent.id:
                 raise das_exc.NetworkHostedByDHCPAgent(
                     network_id=network_id, agent_id=id)
         network.NetworkDhcpAgentBinding(context,
                                         dhcp_agent_id=id,
                                         network_id=network_id).create()
     dhcp_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_DHCP)
     if dhcp_notifier:
         dhcp_notifier.network_added_to_agent(context, network_id,
                                              agent_db.host)
Exemple #4
0
 def bind(self, context, agents, network_id):
     """Bind the network to the agents."""
     # customize the bind logic
     bound_agents = agents[:]
     for agent in agents:
         # saving agent_id to use it after rollback to avoid
         # DetachedInstanceError
         agent_id = agent.id
         try:
             network.NetworkDhcpAgentBinding(
                  context, dhcp_agent_id=agent_id,
                  network_id=network_id).create()
         except exceptions.NeutronDbObjectDuplicateEntry:
             # it's totally ok, someone just did our job!
             bound_agents.remove(agent)
             LOG.info('Agent %s already present', agent_id)
         LOG.debug('Network %(network_id)s is scheduled to be '
                   'hosted by DHCP agent %(agent_id)s',
                   {'network_id': network_id,
                    'agent_id': agent_id})
     super(DhcpFilter, self).bind(context, bound_agents, network_id)
    def bind(self, context, agents, network_id, force_scheduling=False):
        """Bind the network to the agents."""
        # customize the bind logic
        bound_agents = agents[:]
        for agent in agents:
            binding_index = self.get_vacant_network_dhcp_agent_binding_index(
                context, network_id, force_scheduling)
            if binding_index < ndab_model.LOWEST_BINDING_INDEX:
                LOG.debug(
                    'Unable to find a vacant binding_index for '
                    'network %(network_id)s and agent %(agent_id)s', {
                        'network_id': network_id,
                        'agent_id': agent.id
                    })
                continue

            # saving agent_id to use it after rollback to avoid
            # DetachedInstanceError
            agent_id = agent.id
            try:
                network.NetworkDhcpAgentBinding(
                    context,
                    dhcp_agent_id=agent_id,
                    network_id=network_id,
                    binding_index=binding_index).create()
            except exceptions.NeutronDbObjectDuplicateEntry:
                # it's totally ok, someone just did our job!
                bound_agents.remove(agent)
                LOG.info('Agent %s already present', agent_id)
            LOG.debug(
                'Network %(network_id)s is scheduled to be '
                'hosted by DHCP agent %(agent_id)s with binding_index '
                '%(binding_index)d', {
                    'network_id': network_id,
                    'agent_id': agent_id,
                    'binding_index': binding_index
                })
        super(DhcpFilter, self).bind(context, bound_agents, network_id)