From df660ff30df3097d6336d0bafa1d615e19deee07 Mon Sep 17 00:00:00 2001 From: Divya ChanneGowda Date: Wed, 3 Sep 2014 14:59:34 -0700 Subject: [PATCH] Use dict_extend_functions to populate provider network attributes Use dict_extend_functions mechanism to handle populating additional provider network attributes into Network model in Nuage plugin. Change-Id: I72e5afe3d03ae223fcd8e75f1f4f07624c3a7daf Closes-Bug: #1362308 --- neutron/plugins/nuage/nuage_models.py | 6 +++ neutron/plugins/nuage/nuagedb.py | 1 + neutron/plugins/nuage/plugin.py | 31 ++++------- neutron/tests/unit/nuage/test_nuage_plugin.py | 53 +++++++++++++++++-- 4 files changed, 65 insertions(+), 26 deletions(-) diff --git a/neutron/plugins/nuage/nuage_models.py b/neutron/plugins/nuage/nuage_models.py index 5d30dbcb06..59c773951f 100644 --- a/neutron/plugins/nuage/nuage_models.py +++ b/neutron/plugins/nuage/nuage_models.py @@ -14,6 +14,7 @@ # under the License. import sqlalchemy as sa +from sqlalchemy import orm from neutron.db import model_base from neutron.db import models_v2 @@ -49,6 +50,11 @@ class ProviderNetBinding(model_base.BASEV2): physical_network = sa.Column(sa.String(64), nullable=False) vlan_id = sa.Column(sa.Integer, nullable=False) + network = orm.relationship( + models_v2.Network, + backref=orm.backref("pnetbinding", lazy='joined', + uselist=False, cascade='delete')) + class SubnetL2Domain(model_base.BASEV2): __tablename__ = 'nuage_subnet_l2dom_mapping' diff --git a/neutron/plugins/nuage/nuagedb.py b/neutron/plugins/nuage/nuagedb.py index 1fc261371d..1a3210bc23 100644 --- a/neutron/plugins/nuage/nuagedb.py +++ b/neutron/plugins/nuage/nuagedb.py @@ -203,6 +203,7 @@ def add_network_binding(session, network_id, network_type, physical_network, physical_network=physical_network, vlan_id=vlan_id) session.add(binding) + return binding def get_network_binding(session, network_id): diff --git a/neutron/plugins/nuage/plugin.py b/neutron/plugins/nuage/plugin.py index cd8c9e2d25..c03e7725f2 100644 --- a/neutron/plugins/nuage/plugin.py +++ b/neutron/plugins/nuage/plugin.py @@ -78,6 +78,9 @@ class NuagePlugin(db_base_plugin_v2.NeutronDbPluginV2, self.syncmanager = syncmanager.SyncManager(self.nuageclient) self._synchronization_thread() + db_base_plugin_v2.NeutronDbPluginV2.register_dict_extend_funcs( + attributes.NETWORKS, ['_extend_network_dict_provider_nuage']) + def nuageclient_init(self): server = cfg.CONF.RESTPROXY.server serverauth = cfg.CONF.RESTPROXY.serverauth @@ -392,8 +395,9 @@ class NuagePlugin(db_base_plugin_v2.NeutronDbPluginV2, subnets = self.get_subnets(context, filters=filters) return bool(routers or subnets) - def _extend_network_dict_provider(self, context, network): - binding = nuagedb.get_network_binding(context.session, network['id']) + def _extend_network_dict_provider_nuage(self, network, net_db, + net_binding=None): + binding = net_db.pnetbinding if net_db else net_binding if binding: network[pnet.NETWORK_TYPE] = binding.network_type network[pnet.PHYSICAL_NETWORK] = binding.physical_network @@ -432,6 +436,7 @@ class NuagePlugin(db_base_plugin_v2.NeutronDbPluginV2, return network_type, physical_network, segmentation_id def create_network(self, context, network): + binding = None (network_type, physical_network, vlan_id) = self._process_provider_create(context, network['network']) @@ -444,10 +449,11 @@ class NuagePlugin(db_base_plugin_v2.NeutronDbPluginV2, network) self._process_l3_create(context, net, network['network']) if network_type == 'vlan': - nuagedb.add_network_binding(context.session, net['id'], + binding = nuagedb.add_network_binding(context.session, + net['id'], network_type, physical_network, vlan_id) - self._extend_network_dict_provider(context, net) + self._extend_network_dict_provider_nuage(net, None, binding) return net def _validate_update_network(self, context, id, network): @@ -473,23 +479,6 @@ class NuagePlugin(db_base_plugin_v2.NeutronDbPluginV2, raise n_exc.NetworkInUse(net_id=id) return (is_external_set, subnet) - def get_network(self, context, net_id, fields=None): - net = super(NuagePlugin, self).get_network(context, - net_id, - None) - self._extend_network_dict_provider(context, net) - return self._fields(net, fields) - - def get_networks(self, context, filters=None, fields=None, - sorts=None, limit=None, marker=None, page_reverse=False): - nets = super(NuagePlugin, - self).get_networks(context, filters, None, sorts, - limit, marker, page_reverse) - for net in nets: - self._extend_network_dict_provider(context, net) - - return [self._fields(net, fields) for net in nets] - def update_network(self, context, id, network): pnet._raise_if_updates_provider_attributes(network['network']) with context.session.begin(subtransactions=True): diff --git a/neutron/tests/unit/nuage/test_nuage_plugin.py b/neutron/tests/unit/nuage/test_nuage_plugin.py index e9a6595e72..37c062aff7 100644 --- a/neutron/tests/unit/nuage/test_nuage_plugin.py +++ b/neutron/tests/unit/nuage/test_nuage_plugin.py @@ -547,24 +547,24 @@ class TestNuageExtrarouteTestCase(NuagePluginV2TestCase, class TestNuageProviderNetTestCase(NuagePluginV2TestCase): def test_create_provider_network(self): - phy_net = uuidutils.generate_uuid() + phys_net = uuidutils.generate_uuid() data = {'network': {'name': 'pnet1', 'tenant_id': 'admin', pnet.NETWORK_TYPE: 'vlan', - pnet.PHYSICAL_NETWORK: phy_net, + pnet.PHYSICAL_NETWORK: phys_net, pnet.SEGMENTATION_ID: 123}} network_req = self.new_create_request('networks', data, self.fmt) net = self.deserialize(self.fmt, network_req.get_response(self.api)) self.assertEqual('vlan', net['network'][pnet.NETWORK_TYPE]) - self.assertEqual(phy_net, net['network'][pnet.PHYSICAL_NETWORK]) + self.assertEqual(phys_net, net['network'][pnet.PHYSICAL_NETWORK]) self.assertEqual(123, net['network'][pnet.SEGMENTATION_ID]) def test_create_provider_network_no_admin(self): - phy_net = uuidutils.generate_uuid() + phys_net = uuidutils.generate_uuid() data = {'network': {'name': 'pnet1', 'tenant_id': 'no_admin', pnet.NETWORK_TYPE: 'vlan', - pnet.PHYSICAL_NETWORK: phy_net, + pnet.PHYSICAL_NETWORK: phys_net, pnet.SEGMENTATION_ID: 123}} network_req = self.new_create_request('networks', data, self.fmt) network_req.environ['neutron.context'] = context.Context( @@ -572,6 +572,49 @@ class TestNuageProviderNetTestCase(NuagePluginV2TestCase): res = network_req.get_response(self.api) self.assertEqual(exc.HTTPForbidden.code, res.status_int) + def test_get_network_for_provider_network(self): + phys_net = uuidutils.generate_uuid() + data = {'network': {'name': 'pnet1', + 'tenant_id': 'admin', + pnet.NETWORK_TYPE: 'vlan', + pnet.PHYSICAL_NETWORK: phys_net, + pnet.SEGMENTATION_ID: 123}} + network_req = self.new_create_request('networks', data, self.fmt) + res = self.deserialize(self.fmt, network_req.get_response(self.api)) + + get_req = self.new_show_request('networks', res['network']['id']) + net = self.deserialize(self.fmt, get_req.get_response(self.api)) + self.assertEqual('vlan', net['network'][pnet.NETWORK_TYPE]) + self.assertEqual(phys_net, net['network'][pnet.PHYSICAL_NETWORK]) + self.assertEqual(123, net['network'][pnet.SEGMENTATION_ID]) + + def test_list_networks_for_provider_network(self): + phys_net = uuidutils.generate_uuid() + data1 = {'network': {'name': 'pnet1', + 'tenant_id': 'admin', + pnet.NETWORK_TYPE: 'vlan', + pnet.PHYSICAL_NETWORK: phys_net, + pnet.SEGMENTATION_ID: 123}} + network_req_1 = self.new_create_request('networks', data1, self.fmt) + network_req_1.get_response(self.api) + data2 = {'network': {'name': 'pnet2', + 'tenant_id': 'admin', + pnet.NETWORK_TYPE: 'vlan', + pnet.PHYSICAL_NETWORK: phys_net, + pnet.SEGMENTATION_ID: 234}} + network_req_2 = self.new_create_request('networks', data2, self.fmt) + network_req_2.get_response(self.api) + + list_req = self.new_list_request('networks') + pnets = self.deserialize(self.fmt, list_req.get_response(self.api)) + self.assertEqual(2, len(pnets['networks'])) + self.assertEqual('vlan', pnets['networks'][0][pnet.NETWORK_TYPE]) + self.assertEqual(phys_net, pnets['networks'][0][pnet.PHYSICAL_NETWORK]) + self.assertEqual(123, pnets['networks'][0][pnet.SEGMENTATION_ID]) + self.assertEqual('vlan', pnets['networks'][1][pnet.NETWORK_TYPE]) + self.assertEqual(phys_net, pnets['networks'][1][pnet.PHYSICAL_NETWORK]) + self.assertEqual(234, pnets['networks'][1][pnet.SEGMENTATION_ID]) + class TestNuageSecurityGroupTestCase(NuagePluginV2TestCase, test_sg.TestSecurityGroups):