diff --git a/neutron/db/l3_db.py b/neutron/db/l3_db.py index 454691e5b7..ab6017f5c2 100644 --- a/neutron/db/l3_db.py +++ b/neutron/db/l3_db.py @@ -33,6 +33,7 @@ from neutron import manager from neutron.openstack.common import log as logging from neutron.openstack.common.notifier import api as notifier_api from neutron.openstack.common import uuidutils +from neutron.plugins.common import constants LOG = logging.getLogger(__name__) @@ -230,6 +231,12 @@ class L3_NAT_db_mixin(l3.RouterPluginBase): if ports: raise l3.RouterInUse(router_id=id) + #TODO(nati) Refactor here when we have router insertion model + vpnservice = manager.NeutronManager.get_service_plugins().get( + constants.VPN) + if vpnservice: + vpnservice.check_router_in_use(context, id) + # delete any gw port device_filter = {'device_id': [id], 'device_owner': [DEVICE_OWNER_ROUTER_GW]} diff --git a/neutron/db/vpn/vpn_db.py b/neutron/db/vpn/vpn_db.py index f82f4ee5fb..1daaa64f1e 100644 --- a/neutron/db/vpn/vpn_db.py +++ b/neutron/db/vpn/vpn_db.py @@ -583,6 +583,14 @@ class VPNPluginDb(VPNPluginBase, base_db.CommonDbMixin): self._make_vpnservice_dict, filters=filters, fields=fields) + def check_router_in_use(self, context, router_id): + vpnservices = self.get_vpnservices( + context, filters={'router_id': [router_id]}) + if vpnservices: + raise vpnaas.RouterInUseByVPNService( + router_id=router_id, + vpnservice_id=vpnservices[0]['id']) + class VPNPluginRpcDbMixin(): def _get_agent_hosting_vpn_services(self, context, host): diff --git a/neutron/extensions/vpnaas.py b/neutron/extensions/vpnaas.py index e85174bbd2..dc89752a4f 100644 --- a/neutron/extensions/vpnaas.py +++ b/neutron/extensions/vpnaas.py @@ -65,6 +65,10 @@ class VPNServiceInUse(qexception.InUse): message = _("VPNService %(vpnservice_id)s is still in use") +class RouterInUseByVPNService(qexception.InUse): + message = _("Router %(router_id)s is used by VPNService %(vpnservice_id)s") + + class VPNStateInvalid(qexception.BadRequest): message = _("Invalid state %(state)s of vpnaas resource %(id)s") diff --git a/neutron/tests/unit/db/vpn/test_db_vpnaas.py b/neutron/tests/unit/db/vpn/test_db_vpnaas.py index ced5cf2c77..f4f4b525b3 100644 --- a/neutron/tests/unit/db/vpn/test_db_vpnaas.py +++ b/neutron/tests/unit/db/vpn/test_db_vpnaas.py @@ -770,6 +770,15 @@ class TestVpnaas(VPNPluginDbTestCase): expected) return vpnservice + def test_delete_router_in_use_by_vpnservice(self): + """Test delete router in use by vpn service.""" + with self.subnet(cidr='10.2.0.0/24') as subnet: + with self.router() as router: + with self.vpnservice(subnet=subnet, + router=router): + self._delete('routers', router['router']['id'], + expected_code=webob.exc.HTTPConflict.code) + def _set_active(self, model, resource_id): service_plugin = manager.NeutronManager.get_service_plugins()[ constants.VPN]