def required_state(self):

        try:
            network = self.db.get_network(self.context, self.network_id)
        except NoResultFound:
            # Nothing in Neutron, so we expect nothing in ACI
            return None

        subnets = self.db.get_subnets_by_network(self.context, self.network_id)

        LOG.info(subnets)

        address_scopes = []

        for subnet in subnets:
            scope = self._get_address_scope(subnet)
            if scope:
                address_scopes.append(scope)

        ports = self.db.get_ports(self.context,
                                  {"network_id": [self.network_id]})

        binding_hosts = []
        for port in ports:
            # for some reason the port binding mixin is not populating the host.
            # its ineffecient but we use the ml2 db to get for each port.
            binding_host = ml2_db.get_port_binding_host(
                self.context.session, port['id'])

            if binding_host and (binding_host not in binding_hosts):
                binding_hosts.append(binding_host)

        LOG.info(binding_hosts)
        LOG.info(address_scopes)
    def dvr_deletens_if_no_port(self, context, port_id):
        """Delete the DVR namespace if no dvr serviced port exists."""
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        port_host = ml2_db.get_port_binding_host(port_id)
        if not router_ids:
            LOG.debug(
                "No namespaces available for this DVR port %(port)s " "on host %(host)s",
                {"port": port_id, "host": port_host},
            )
            return []
        removed_router_info = []
        for router_id in router_ids:
            subnet_ids = self.get_subnet_ids_on_router(admin_context, router_id)
            port_exists_on_subnet = False
            for subnet in subnet_ids:
                if self.check_ports_active_on_host_and_subnet(admin_context, port_host, port_id, subnet):
                    port_exists_on_subnet = True
                    break

            if port_exists_on_subnet:
                continue
            filter_rtr = {"device_id": [router_id], "device_owner": [q_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(admin_context, filters=filter_rtr)
            for prt in int_ports:
                dvr_binding = ml2_db.get_dvr_port_binding_by_host(context.session, prt["id"], port_host)
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding["router_id"] = None
                    dvr_binding.update(dvr_binding)
            agent = self._get_agent_by_type_and_host(context, q_const.AGENT_TYPE_L3, port_host)
            info = {"router_id": router_id, "host": port_host, "agent_id": str(agent.id)}
            removed_router_info.append(info)
            LOG.debug("Router namespace %(router_id)s on host %(host)s " "to be deleted", info)
        return removed_router_info
Exemple #3
0
    def dvr_deletens_if_no_port(self, context, port_id, port_host=None):
        """Delete the DVR namespace if no dvr serviced port exists."""
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        if not port_host:
            port_host = ml2_db.get_port_binding_host(admin_context.session,
                                                     port_id)
            if not port_host:
                LOG.debug('Host name not found for port %s', port_id)
                return []

        if not router_ids:
            LOG.debug(
                'No namespaces available for this DVR port %(port)s '
                'on host %(host)s', {
                    'port': port_id,
                    'host': port_host
                })
            return []
        removed_router_info = []
        for router_id in router_ids:
            subnet_ids = self.get_subnet_ids_on_router(admin_context,
                                                       router_id)
            port_exists_on_subnet = False
            for subnet in subnet_ids:
                if self.check_ports_on_host_and_subnet(admin_context,
                                                       port_host, port_id,
                                                       subnet):
                    port_exists_on_subnet = True
                    break

            if port_exists_on_subnet:
                continue
            filter_rtr = {
                'device_id': [router_id],
                'device_owner': [n_const.DEVICE_OWNER_DVR_INTERFACE]
            }
            int_ports = self._core_plugin.get_ports(admin_context,
                                                    filters=filter_rtr)
            for port in int_ports:
                dvr_binding = (ml2_db.get_dvr_port_binding_by_host(
                    context.session, port['id'], port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)
            agent = self._get_agent_by_type_and_host(context,
                                                     n_const.AGENT_TYPE_L3,
                                                     port_host)
            info = {
                'router_id': router_id,
                'host': port_host,
                'agent_id': str(agent.id)
            }
            removed_router_info.append(info)
            LOG.debug(
                'Router namespace %(router_id)s on host %(host)s '
                'to be deleted', info)
        return removed_router_info
    def get_dvr_routers_to_remove(self, context, port_id, port_host=None):
        """Returns info about which routers should be removed from <port_host>

        In case dvr serviceable port is about to be deleted we need to check
        if any dvr routers should be removed from l3 agent on port's host
        """
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        if not port_host:
            port_host = ml2_db.get_port_binding_host(admin_context.session,
                                                     port_id)
            if not port_host:
                LOG.debug('Host name not found for port %s', port_id)
                return []

        if not router_ids:
            LOG.debug('No DVR routers for this DVR port %(port)s '
                      'on host %(host)s', {'port': port_id,
                                           'host': port_host})
            return []
        agent = self._get_agent_by_type_and_host(
            context, n_const.AGENT_TYPE_L3, port_host)
        removed_router_info = []
        for router_id in router_ids:
            snat_binding = context.session.query(
                l3agent_sch_db.RouterL3AgentBinding).filter_by(
                    router_id=router_id).filter_by(
                        l3_agent_id=agent.id).first()
            if snat_binding:
                # not removing from the agent hosting SNAT for the router
                continue
            subnet_ids = self.get_subnet_ids_on_router(admin_context,
                                                       router_id)
            if self.check_dvr_serviceable_ports_on_host(
                    admin_context, port_host, subnet_ids, except_port=port_id):
                continue
            filter_rtr = {'device_id': [router_id],
                          'device_owner':
                          [n_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(
                admin_context, filters=filter_rtr)
            for port in int_ports:
                dvr_binding = (ml2_db.
                               get_dvr_port_binding_by_host(context.session,
                                                            port['id'],
                                                            port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)

            info = {'router_id': router_id, 'host': port_host,
                    'agent_id': str(agent.id)}
            removed_router_info.append(info)
            LOG.debug('Router %(router_id)s on host %(host)s to be deleted',
                      info)
        return removed_router_info
Exemple #5
0
    def test_get_port_binding_host(self):
        network_id = uuidutils.generate_uuid()
        port_id = uuidutils.generate_uuid()
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id)
        self._setup_neutron_portbinding(port_id, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx, port_id)
        self.assertEqual(host, port_host)
Exemple #6
0
    def test_get_port_binding_host(self):
        network_id = uuidutils.generate_uuid()
        port_id = uuidutils.generate_uuid()
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id)
        self._setup_neutron_portbinding(port_id, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx, port_id)
        self.assertEqual(host, port_host)
Exemple #7
0
    def test_get_port_binding_host(self):
        network_id = 'foo-network-id'
        port_id = 'foo-port-id'
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id)
        self._setup_neutron_portbinding(port_id, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
        self.assertEqual(host, port_host)
    def dvr_deletens_if_no_port(self, context, port_id, port_host=None):
        """Delete the DVR namespace if no dvr serviced port exists."""
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        if not port_host:
            port_host = ml2_db.get_port_binding_host(admin_context.session,
                                                     port_id)
            if not port_host:
                LOG.debug('Host name not found for port %s', port_id)
                return []

        if not router_ids:
            LOG.debug('No namespaces available for this DVR port %(port)s '
                      'on host %(host)s', {'port': port_id,
                                           'host': port_host})
            return []
        removed_router_info = []
        for router_id in router_ids:
            subnet_ids = self.get_subnet_ids_on_router(admin_context,
                                                       router_id)
            port_exists_on_subnet = False
            for subnet in subnet_ids:
                if self.check_ports_on_host_and_subnet(admin_context,
                                                       port_host,
                                                       port_id,
                                                       subnet):
                    port_exists_on_subnet = True
                    break

            if port_exists_on_subnet:
                continue
            filter_rtr = {'device_id': [router_id],
                          'device_owner':
                          [n_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(
                admin_context, filters=filter_rtr)
            for port in int_ports:
                dvr_binding = (ml2_db.
                               get_dvr_port_binding_by_host(context.session,
                                                            port['id'],
                                                            port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)
            agent = self._get_agent_by_type_and_host(context,
                                                     n_const.AGENT_TYPE_L3,
                                                     port_host)
            info = {'router_id': router_id, 'host': port_host,
                    'agent_id': str(agent.id)}
            removed_router_info.append(info)
            LOG.debug('Router namespace %(router_id)s on host %(host)s '
                      'to be deleted', info)
        return removed_router_info
Exemple #9
0
    def test_get_port_binding_host(self):
        network_id = 'foo-network-id'
        port_id = 'foo-port-id'
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id)
        self._setup_neutron_portbinding(port_id, vif_type, host)

        port_host = ml2_db.get_port_binding_host(port_id)
        self.assertEqual(host, port_host)
    def dvr_deletens_if_no_port(self, context, port_id, port_host=None):
        """Delete the DVR namespace if no dvr serviced port exists."""
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        if not port_host:
            port_host = ml2_db.get_port_binding_host(admin_context.session,
                                                     port_id)
            if not port_host:
                LOG.debug('Host name not found for port %s', port_id)
                return []

        if not router_ids:
            LOG.debug('No namespaces available for this DVR port %(port)s '
                      'on host %(host)s', {'port': port_id,
                                           'host': port_host})
            return []
        agent = self._get_agent_by_type_and_host(
            context, n_const.AGENT_TYPE_L3, port_host)
        removed_router_info = []
        for router_id in router_ids:
            snat_binding = context.session.query(
                CentralizedSnatL3AgentBinding).filter_by(
                    router_id=router_id).filter_by(
                        l3_agent_id=agent.id).first()
            if snat_binding:
                # not removing from the agent hosting SNAT for the router
                continue
            subnet_ids = self.get_subnet_ids_on_router(admin_context,
                                                       router_id)
            if self.check_dvr_serviceable_ports_on_host(
                    admin_context, port_host, subnet_ids, except_port=port_id):
                continue
            filter_rtr = {'device_id': [router_id],
                          'device_owner':
                          [n_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(
                admin_context, filters=filter_rtr)
            for port in int_ports:
                dvr_binding = (ml2_db.
                               get_dvr_port_binding_by_host(context.session,
                                                            port['id'],
                                                            port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)

            info = {'router_id': router_id, 'host': port_host,
                    'agent_id': str(agent.id)}
            removed_router_info.append(info)
            LOG.debug('Router namespace %(router_id)s on host %(host)s '
                      'to be deleted', info)
        return removed_router_info
Exemple #11
0
    def test_get_port_binding_host_multiple_results_found(self):
        network_id = 'foo-network-id'
        port_id = 'foo-port-id'
        port_id_one = 'foo-port-id-one'
        port_id_two = 'foo-port-id-two'
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id_one)
        self._setup_neutron_portbinding(port_id_one, vif_type, host)
        self._setup_neutron_port(network_id, port_id_two)
        self._setup_neutron_portbinding(port_id_two, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
        self.assertIsNone(port_host)
Exemple #12
0
    def test_get_port_binding_host_multiple_results_found(self):
        network_id = 'foo-network-id'
        port_id = 'foo-port-id'
        port_id_one = 'foo-port-id-one'
        port_id_two = 'foo-port-id-two'
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id_one)
        self._setup_neutron_portbinding(port_id_one, vif_type, host)
        self._setup_neutron_port(network_id, port_id_two)
        self._setup_neutron_portbinding(port_id_two, vif_type, host)

        port_host = ml2_db.get_port_binding_host(port_id)
        self.assertIsNone(port_host)
Exemple #13
0
 def port_bound_to_host(self, context, port_id, host):
     try:
         port = self.get_port(context, port_id)
         if port['device_owner'] == const.DEVICE_OWNER_DVR_INTERFACE:
             bindings = db.get_dvr_port_bindings(port_id)
             for b in bindings:
                 if b.host == host:
                     return True
             LOG.debug("No Binding exists for port %s", port_id)
             return False
         else:
             port_host = db.get_port_binding_host(port_id)
             return (port_host == host)
     except exc.PortNotFound:
         LOG.debug("Port not found %s", port_id)
         return False
Exemple #14
0
    def test_get_port_binding_host_multiple_results_found(self):
        network_id = uuidutils.generate_uuid()
        port_id = uuidutils.generate_uuid()
        port_id_one = uuidutils.generate_uuid()
        port_id_two = uuidutils.generate_uuid()
        # NOTE(manjeets) to check startswith testcase we
        # need port ids with same prefix
        port_id_one = port_id[:8] + port_id_one[8:]
        port_id_two = port_id[:8] + port_id_two[8:]
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id_one)
        self._setup_neutron_portbinding(port_id_one, vif_type, host)
        self._setup_neutron_port(network_id, port_id_two)
        self._setup_neutron_portbinding(port_id_two, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx, port_id[:8])
        self.assertIsNone(port_host)
Exemple #15
0
 def _get_host_from_qos_id(self, context, qos_id):
     qos = self._get_qos(context, qos_id)
     if qos['router_id'] is not None:
         plugin = manager.NeutronManager.get_service_plugins().get(
             service_constants.L3_ROUTER_NAT)
         adminContext = context if context.is_admin else context.elevated()
         l3_agents = plugin.get_l3_agents_hosting_routers(
             adminContext, [qos['router_id']],
             admin_state_up=True, active=True)
         return l3_agents[0].host
     elif qos['port_id'] is not None:
         """
         plugin = manager.NeutronManager.get_plugin()
         adminContext = context if context.is_admin else context.elevated()
         return plugin.get_port_binding_host(adminContext, qos['port_id'])
         """
         return db.get_port_binding_host(qos['port_id'])
     else:
         return None
Exemple #16
0
    def test_get_port_binding_host_multiple_results_found(self):
        network_id = uuidutils.generate_uuid()
        port_id = uuidutils.generate_uuid()
        port_id_one = uuidutils.generate_uuid()
        port_id_two = uuidutils.generate_uuid()
        # NOTE(manjeets) to check startswith testcase we
        # need port ids with same prefix
        port_id_one = port_id[:8] + port_id_one[8:]
        port_id_two = port_id[:8] + port_id_two[8:]
        host = 'fake_host'
        vif_type = portbindings.VIF_TYPE_UNBOUND
        self._setup_neutron_network(network_id)
        self._setup_neutron_port(network_id, port_id_one)
        self._setup_neutron_portbinding(port_id_one, vif_type, host)
        self._setup_neutron_port(network_id, port_id_two)
        self._setup_neutron_portbinding(port_id_two, vif_type, host)

        port_host = ml2_db.get_port_binding_host(self.ctx, port_id[:8])
        self.assertIsNone(port_host)
Exemple #17
0
 def _get_host_from_qos_id(self, context, qos_id):
     qos = self._get_qos(context, qos_id)
     if qos['router_id'] is not None:
         plugin = manager.NeutronManager.get_service_plugins().get(
             service_constants.L3_ROUTER_NAT)
         adminContext = context if context.is_admin else context.elevated()
         l3_agents = plugin.get_l3_agents_hosting_routers(
             adminContext, [qos['router_id']],
             admin_state_up=True, active=True)
         return l3_agents[0].host
     elif qos['port_id'] is not None:
         """
         plugin = manager.NeutronManager.get_plugin()
         adminContext = context if context.is_admin else context.elevated()
         return plugin.get_port_binding_host(adminContext, qos['port_id'])
         """
         return db.get_port_binding_host(qos['port_id'])
     else:
         return None
Exemple #18
0
    def dvr_deletens_if_no_vm(self, context, port_id):
        """Delete the DVR namespace if no VM exists."""
        router_ids = self.get_dvr_routers_by_vmportid(context, port_id)
        port_host = ml2_db.get_port_binding_host(port_id)
        if not router_ids:
            LOG.debug('No namespaces available for this DVR port %(port)s '
                      'on host %(host)s', {'port': port_id,
                                           'host': port_host})
            return []
        removed_router_info = []
        for router_id in router_ids:
            subnet_ids = self.get_subnet_ids_on_router(context, router_id)
            vm_exists_on_subnet = False
            for subnet in subnet_ids:
                if self.check_vm_exists_on_subnet(context,
                                                  port_host,
                                                  port_id,
                                                  subnet):
                    vm_exists_on_subnet = True
                    break

            if vm_exists_on_subnet:
                continue
            filter_rtr = {'device_id': [router_id],
                          'device_owner':
                          [q_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(
                context, filters=filter_rtr)
            for prt in int_ports:
                dvr_binding = (ml2_db.
                               get_dvr_port_binding_by_host(context.session,
                                                            prt['id'],
                                                            port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)
            self.delete_namespace_on_host(context, port_host, router_id)
            info = {'router_id': router_id, 'host': port_host}
            removed_router_info.append(info)
            LOG.debug('Deleted router namespace %(router_id)s '
                      'on host %(host)s', info)
        return removed_router_info
    def dvr_deletens_if_no_vm(self, context, port_id):
        """Delete the DVR namespace if no VM exists."""
        router_ids = self.get_dvr_routers_by_vmportid(context, port_id)
        port_host = ml2_db.get_port_binding_host(port_id)
        if not router_ids:
            LOG.debug('No namespaces available for this DVR port %(port)s '
                      'on host %(host)s', {'port': port_id,
                                           'host': port_host})
            return []
        removed_router_info = []
        for router_id in router_ids:
            subnet_ids = self.get_subnet_ids_on_router(context, router_id)
            vm_exists_on_subnet = False
            for subnet in subnet_ids:
                if self.check_vm_exists_on_subnet(context,
                                                  port_host,
                                                  port_id,
                                                  subnet):
                    vm_exists_on_subnet = True
                    break

            if vm_exists_on_subnet:
                continue
            filter_rtr = {'device_id': [router_id],
                          'device_owner':
                          [q_const.DEVICE_OWNER_DVR_INTERFACE]}
            int_ports = self._core_plugin.get_ports(
                context, filters=filter_rtr)
            for prt in int_ports:
                dvr_binding = (ml2_db.
                               get_dvr_port_binding_by_host(context.session,
                                                            prt['id'],
                                                            port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)
            self.delete_namespace_on_host(context, port_host, router_id)
            info = {'router_id': router_id, 'host': port_host}
            removed_router_info.append(info)
            LOG.debug('Deleted router namespace %(router_id)s '
                      'on host %(host)s', info)
        return removed_router_info
Exemple #20
0
 def port_bound_to_host(self, port_id, host):
     port_host = db.get_port_binding_host(port_id)
     return (port_host == host)
Exemple #21
0
    def test_get_port_binding_host_result_not_found(self):
        port_id = uuidutils.generate_uuid()

        port_host = ml2_db.get_port_binding_host(port_id)
        self.assertIsNone(port_host)
Exemple #22
0
    def dvr_deletens_if_no_port(self, context, port_id, port_host=None):
        """Delete the DVR namespace if no dvr serviced port exists."""
        admin_context = context.elevated()
        router_ids = self.get_dvr_routers_by_portid(admin_context, port_id)
        if not port_host:
            port_host = ml2_db.get_port_binding_host(admin_context.session,
                                                     port_id)
            if not port_host:
                LOG.debug('Host name not found for port %s', port_id)
                return []

        if not router_ids:
            LOG.debug(
                'No namespaces available for this DVR port %(port)s '
                'on host %(host)s', {
                    'port': port_id,
                    'host': port_host
                })
            return []
        agent = self._get_agent_by_type_and_host(context,
                                                 n_const.AGENT_TYPE_L3,
                                                 port_host)
        removed_router_info = []
        for router_id in router_ids:
            snat_binding = context.session.query(
                CentralizedSnatL3AgentBinding).filter_by(
                    router_id=router_id).filter_by(
                        l3_agent_id=agent.id).first()
            if snat_binding:
                # not removing from the agent hosting SNAT for the router
                continue
            subnet_ids = self.get_subnet_ids_on_router(admin_context,
                                                       router_id)
            if self.check_dvr_serviceable_ports_on_host(admin_context,
                                                        port_host,
                                                        subnet_ids,
                                                        except_port=port_id):
                continue
            filter_rtr = {
                'device_id': [router_id],
                'device_owner': [n_const.DEVICE_OWNER_DVR_INTERFACE]
            }
            int_ports = self._core_plugin.get_ports(admin_context,
                                                    filters=filter_rtr)
            for port in int_ports:
                dvr_binding = (ml2_db.get_dvr_port_binding_by_host(
                    context.session, port['id'], port_host))
                if dvr_binding:
                    # unbind this port from router
                    dvr_binding['router_id'] = None
                    dvr_binding.update(dvr_binding)

            info = {
                'router_id': router_id,
                'host': port_host,
                'agent_id': str(agent.id)
            }
            removed_router_info.append(info)
            LOG.debug(
                'Router namespace %(router_id)s on host %(host)s '
                'to be deleted', info)
        return removed_router_info
Exemple #23
0
 def get_bindinghost_by_portid(self, port_id):
     return db.get_port_binding_host(port_id)
Exemple #24
0
    def test_get_port_binding_host_result_not_found(self):
        port_id = uuidutils.generate_uuid()

        port_host = ml2_db.get_port_binding_host(self.ctx.session, port_id)
        self.assertIsNone(port_host)