From bb8f7338335001380debffcbf73b62b3e3afca2e Mon Sep 17 00:00:00 2001 From: Fawad Khaliq Date: Tue, 8 Apr 2014 15:49:35 -0700 Subject: [PATCH] Fixed floating IP logic in PLUMgrid plugin * Fixed associate and disassociate floating IP logic * Added unit tests Change-Id: I310f5550bca1c9015cbda3e0338eb90a36dc96f1 Closes-Bug: 1298495 (cherry picked from commit 9f8cb33557ecc7c0a832610e913a56a8b8e6c524) --- .../plugins/plumgrid/drivers/fake_plumlib.py | 12 +++-- neutron/plugins/plumgrid/drivers/plumlib.py | 19 ++++--- .../plumgrid_plugin/plumgrid_plugin.py | 53 +++++++++++-------- .../unit/plumgrid/test_plumgrid_plugin.py | 22 ++++++++ 4 files changed, 73 insertions(+), 33 deletions(-) diff --git a/neutron/plugins/plumgrid/drivers/fake_plumlib.py b/neutron/plugins/plumgrid/drivers/fake_plumlib.py index a0a01023d..bf0cb9f9a 100644 --- a/neutron/plugins/plumgrid/drivers/fake_plumlib.py +++ b/neutron/plugins/plumgrid/drivers/fake_plumlib.py @@ -55,7 +55,7 @@ class Plumlib(): def create_subnet(self, sub_db, net_db, ipnet): pass - def update_subnet(self, org_sub_db, new_sub_db, ipnet): + def update_subnet(self, orig_sub_db, new_sub_db, ipnet): pass def delete_subnet(self, tenant_id, net_db, net_id): @@ -85,11 +85,15 @@ class Plumlib(): def remove_router_interface(self, tenant_id, net_id, router_id): pass - def create_floatingip(self, net_db, floating_ip): + def create_floatingip(self, floating_ip): pass - def update_floatingip(self, net_db, floating_ip, id): + def update_floatingip(self, floating_ip_orig, floating_ip, id): pass - def delete_floatingip(self, net_db, floating_ip_org, id): + def delete_floatingip(self, floating_ip_orig, id): pass + + def disassociate_floatingips(self, fip, port_id): + return dict((key, fip[key]) for key in ("id", "floating_network_id", + "floating_ip_address")) diff --git a/neutron/plugins/plumgrid/drivers/plumlib.py b/neutron/plugins/plumgrid/drivers/plumlib.py index 7a6c98c19..8e2607e85 100644 --- a/neutron/plugins/plumgrid/drivers/plumlib.py +++ b/neutron/plugins/plumgrid/drivers/plumlib.py @@ -57,8 +57,8 @@ class Plumlib(object): def create_subnet(self, sub_db, net_db, ipnet): self.plumlib.create_subnet(sub_db, net_db, ipnet) - def update_subnet(self, org_sub_db, new_sub_db, ipnet): - self.plumlib.update_subnet(org_sub_db, new_sub_db, ipnet) + def update_subnet(self, orig_sub_db, new_sub_db, ipnet): + self.plumlib.update_subnet(orig_sub_db, new_sub_db, ipnet) def delete_subnet(self, tenant_id, net_db, net_id): self.plumlib.delete_subnet(tenant_id, net_db, net_id) @@ -87,11 +87,14 @@ class Plumlib(object): def remove_router_interface(self, tenant_id, net_id, router_id): self.plumlib.remove_router_interface(tenant_id, net_id, router_id) - def create_floatingip(self, net_db, floating_ip): - self.plumlib.create_floatingip(net_db, floating_ip) + def create_floatingip(self, floating_ip): + self.plumlib.create_floatingip(floating_ip) - def update_floatingip(self, net_db, floating_ip, id): - self.plumlib.update_floatingip(net_db, floating_ip, id) + def update_floatingip(self, floating_ip_orig, floating_ip, id): + self.plumlib.update_floatingip(floating_ip_orig, floating_ip, id) - def delete_floatingip(self, net_db, floating_ip_org, id): - self.plumlib.delete_floatingip(net_db, floating_ip_org, id) + def delete_floatingip(self, floating_ip_orig, id): + self.plumlib.delete_floatingip(floating_ip_orig, id) + + def disassociate_floatingips(self, floating_ip, port_id): + self.plumlib.disassociate_floatingips(floating_ip, port_id) diff --git a/neutron/plugins/plumgrid/plumgrid_plugin/plumgrid_plugin.py b/neutron/plugins/plumgrid/plumgrid_plugin/plumgrid_plugin.py index 2e41aadae..17b20f001 100644 --- a/neutron/plugins/plumgrid/plumgrid_plugin/plumgrid_plugin.py +++ b/neutron/plugins/plumgrid/plumgrid_plugin/plumgrid_plugin.py @@ -23,6 +23,7 @@ to the PLUMgrid Network Management System called Director import netaddr from oslo.config import cfg +from sqlalchemy.orm import exc as sa_exc from neutron.api.v2 import attributes from neutron.common import constants @@ -335,7 +336,7 @@ class NeutronPluginPLUMgridV2(db_base_plugin_v2.NeutronDbPluginV2, LOG.debug(_("update_subnet() called")) # Collecting subnet info - org_sub_db = self._get_subnet(context, subnet_id) + orig_sub_db = self._get_subnet(context, subnet_id) with context.session.begin(subtransactions=True): # Plugin DB - Subnet Update @@ -346,7 +347,7 @@ class NeutronPluginPLUMgridV2(db_base_plugin_v2.NeutronDbPluginV2, try: # PLUMgrid Server does not support updating resources yet LOG.debug(_("PLUMgrid Library: update_network() called")) - self._plumlib.update_subnet(org_sub_db, new_sub_db, ipnet) + self._plumlib.update_subnet(orig_sub_db, new_sub_db, ipnet) except Exception as err_message: raise plum_excep.PLUMgridException(err_msg=err_message) @@ -482,14 +483,9 @@ class NeutronPluginPLUMgridV2(db_base_plugin_v2.NeutronDbPluginV2, floating_ip = super(NeutronPluginPLUMgridV2, self).create_floatingip(context, floatingip) - - net_id = floating_ip['floating_network_id'] - net_db = super(NeutronPluginPLUMgridV2, - self).get_network(context, net_id) - try: LOG.debug(_("PLUMgrid Library: create_floatingip() called")) - self._plumlib.create_floatingip(net_db, floating_ip) + self._plumlib.create_floatingip(floating_ip) except Exception as err_message: raise plum_excep.PLUMgridException(err_msg=err_message) @@ -500,18 +496,15 @@ class NeutronPluginPLUMgridV2(db_base_plugin_v2.NeutronDbPluginV2, LOG.debug(_("Neutron PLUMgrid Director: update_floatingip() called")) with context.session.begin(subtransactions=True): - + floating_ip_orig = super(NeutronPluginPLUMgridV2, + self).get_floatingip(context, id) floating_ip = super(NeutronPluginPLUMgridV2, self).update_floatingip(context, id, floatingip) - - net_id = floating_ip['floating_network_id'] - net_db = super(NeutronPluginPLUMgridV2, - self).get_network(context, net_id) - try: LOG.debug(_("PLUMgrid Library: update_floatingip() called")) - self._plumlib.update_floatingip(net_db, floating_ip, id) + self._plumlib.update_floatingip(floating_ip_orig, floating_ip, + id) except Exception as err_message: raise plum_excep.PLUMgridException(err_msg=err_message) @@ -523,21 +516,39 @@ class NeutronPluginPLUMgridV2(db_base_plugin_v2.NeutronDbPluginV2, with context.session.begin(subtransactions=True): - floating_ip_org = super(NeutronPluginPLUMgridV2, - self).get_floatingip(context, id) + floating_ip_orig = super(NeutronPluginPLUMgridV2, + self).get_floatingip(context, id) - net_id = floating_ip_org['floating_network_id'] - net_db = super(NeutronPluginPLUMgridV2, - self).get_network(context, net_id) super(NeutronPluginPLUMgridV2, self).delete_floatingip(context, id) try: LOG.debug(_("PLUMgrid Library: delete_floatingip() called")) - self._plumlib.delete_floatingip(net_db, floating_ip_org, id) + self._plumlib.delete_floatingip(floating_ip_orig, id) except Exception as err_message: raise plum_excep.PLUMgridException(err_msg=err_message) + def disassociate_floatingips(self, context, port_id): + LOG.debug(_("Neutron PLUMgrid Director: disassociate_floatingips() " + "called")) + + try: + fip_qry = context.session.query(l3_db.FloatingIP) + floating_ip = fip_qry.filter_by(fixed_port_id=port_id).one() + + LOG.debug(_("PLUMgrid Library: disassociate_floatingips()" + " called")) + self._plumlib.disassociate_floatingips(floating_ip, port_id) + + except sa_exc.NoResultFound: + pass + + except Exception as err_message: + raise plum_excep.PLUMgridException(err_msg=err_message) + + super(NeutronPluginPLUMgridV2, + self).disassociate_floatingips(context, port_id) + """ Internal PLUMgrid Fuctions """ diff --git a/neutron/tests/unit/plumgrid/test_plumgrid_plugin.py b/neutron/tests/unit/plumgrid/test_plumgrid_plugin.py index 6cc39ce79..8cf1868ec 100644 --- a/neutron/tests/unit/plumgrid/test_plumgrid_plugin.py +++ b/neutron/tests/unit/plumgrid/test_plumgrid_plugin.py @@ -148,3 +148,25 @@ class TestPlumgridProvidernet(PLUMgridPluginV2TestCase): self.assertEqual(net['network'][provider.NETWORK_TYPE], 'vlan') self.assertEqual(net['network'][provider.SEGMENTATION_ID], 3333) self.assertEqual(net['network'][provider.PHYSICAL_NETWORK], 'phy3333') + + +class TestDisassociateFloatingIP(PLUMgridPluginV2TestCase): + + def test_disassociate_floating_ip(self): + port_id = "abcdefgh" + tenant_id = "94eb42de4e331" + fip_net_id = "b843d18245678" + fip_addr = "10.0.3.44" + fip_id = "e623679734051" + fip = {"router_id": "94eb42de4e331", + "tenant_id": tenant_id, + "floating_network_id": fip_net_id, + "fixed_ip_address": "192.168.8.2", + "floating_ip_address": fip_addr, + "port_id": port_id, + "id": fip_id} + plumlib = importutils.import_object(PLUM_DRIVER) + fip_res = plumlib.disassociate_floatingips(fip, port_id) + self.assertEqual(fip_res["id"], fip_id) + self.assertEqual(fip_res["floating_ip_address"], fip_addr) + self.assertEqual(fip_res["floating_network_id"], fip_net_id) -- 2.45.2