Exemple #1
0
    def check_agent_router_scheduling_needed(self, context, agent, router):
        """Check if the router scheduling is needed.

        :raises: RouterHostedByL3Agent if router is already assigned
          to a different agent.
        :returns: True if scheduling is needed, otherwise False
        """
        router_id = router['id']
        agent_id = agent['id']
        if router.get('distributed'):
            if router['external_gateway_info']:
                return not self.get_snat_bindings(context, router['id'])
            return False
        query = context.session.query(RouterL3AgentBinding)
        bindings = query.filter_by(router_id=router_id).all()
        if not bindings:
            return True
        for binding in bindings:
            if binding.l3_agent_id == agent_id:
                # router already bound to the agent we need
                return False
        # non-dvr case: centralized router is already bound to some agent
        raise l3agentscheduler.RouterHostedByL3Agent(
            router_id=router_id,
            agent_id=bindings[0].l3_agent_id)
Exemple #2
0
    def add_router_to_l3_agent(self, context, id, router_id):
        """Add a l3 agent to host a router."""
        router = self.get_router(context, router_id)
        with context.session.begin(subtransactions=True):
            agent_db = self._get_agent(context, id)
            if (agent_db['agent_type'] != constants.AGENT_TYPE_L3 or
                not agent_db['admin_state_up'] or
                not self.get_l3_agent_candidates(router, [agent_db])):
                raise l3agentscheduler.InvalidL3Agent(id=id)
            query = context.session.query(RouterL3AgentBinding)
            try:
                binding = query.filter(
                    RouterL3AgentBinding.l3_agent_id == agent_db.id,
                    RouterL3AgentBinding.router_id == router_id).one()
                if binding:
                    raise l3agentscheduler.RouterHostedByL3Agent(
                        router_id=router_id, agent_id=id)
            except exc.NoResultFound:
                pass

            result = self.auto_schedule_routers(context,
                                                agent_db.host,
                                                [router_id])
            if not result:
                raise l3agentscheduler.RouterSchedulingFailed(
                    router_id=router_id, agent_id=id)

        l3_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_L3)
        if l3_notifier:
            l3_notifier.router_added_to_agent(
                context, [router_id], agent_db.host)
    def add_router_to_l3_agent(self, context, agent_id, router_id):
        """Add a l3 agent to host a router."""
        router = self.get_router(context, router_id)
        distributed = router.get('distributed')
        with context.session.begin(subtransactions=True):
            agent_db = self._get_agent(context, agent_id)
            agent_conf = self.get_configuration_dict(agent_db)
            agent_mode = agent_conf.get('agent_mode', 'legacy')
            if (not distributed and agent_mode == 'dvr'
                    or distributed and agent_mode == 'legacy'):
                router_type = ('distributed' if distributed else 'centralized')
                raise l3agentscheduler.RouterL3AgentMismatch(
                    router_type=router_type,
                    router_id=router_id,
                    agent_mode=agent_mode,
                    agent_id=agent_id)
            if (agent_db['agent_type'] != constants.AGENT_TYPE_L3
                    or not agent_db['admin_state_up']
                    or not self.get_l3_agent_candidates(
                        context, router, [agent_db])):
                raise l3agentscheduler.InvalidL3Agent(id=agent_id)
            query = context.session.query(RouterL3AgentBinding)
            if distributed:
                binding = query.filter_by(router_id=router_id,
                                          l3_agent_id=agent_id).first()
                if binding:
                    raise l3agentscheduler.RouterHostedByL3Agent(
                        router_id=router_id, agent_id=binding.l3_agent_id)
            else:
                try:
                    binding = query.filter_by(router_id=router_id).one()

                    raise l3agentscheduler.RouterHostedByL3Agent(
                        router_id=router_id, agent_id=binding.l3_agent_id)
                except exc.NoResultFound:
                    pass

            result = self.auto_schedule_routers(context, agent_db.host,
                                                [router_id])
            if not result:
                raise l3agentscheduler.RouterSchedulingFailed(
                    router_id=router_id, agent_id=agent_id)

        l3_notifier = self.agent_notifiers.get(constants.AGENT_TYPE_L3)
        if l3_notifier:
            l3_notifier.router_added_to_agent(context, [router_id],
                                              agent_db.host)
Exemple #4
0
    def check_agent_router_scheduling_needed(self, context, agent, router):
        """Check if the router scheduling is needed.

        :raises: RouterHostedByL3Agent if router is already assigned
          to a different agent.
        :returns: True if scheduling is needed, otherwise False
        """
        router_id = router['id']
        agent_id = agent['id']
        bindings = rb_obj.RouterL3AgentBinding.get_objects(context,
                                                           router_id=router_id)
        if not bindings:
            return True
        for binding in bindings:
            if binding.l3_agent_id == agent_id:
                # router already bound to the agent we need
                return False
        if router.get('ha'):
            return True
        # legacy router case: router is already bound to some agent
        raise l3agentscheduler.RouterHostedByL3Agent(
            router_id=router_id, agent_id=bindings[0].l3_agent_id)