]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Decompose db_base_plugin_v2.py with changes
authorPavel Bondar <pbondar@infoblox.com>
Mon, 8 Jun 2015 11:15:30 +0000 (14:15 +0300)
committerJohn Belamaric <jbelamaric@infoblox.com>
Sat, 13 Jun 2015 04:14:29 +0000 (00:14 -0400)
This commit is a preparation step for using pluggable IPAM.
1. Moved get_subnets functionality to db_base_plugin_common to make it
accessible by ipam backends.
2. Reworked update_subnet routine:
- moved db part into update_db_subnet;

Partially-Implements: blueprint neutron-ipam

Change-Id: Idb8f54d9fccaad1137222d156590c37d86aa576b

neutron/db/db_base_plugin_common.py
neutron/db/db_base_plugin_v2.py
neutron/db/ipam_backend_mixin.py
neutron/db/ipam_non_pluggable_backend.py
neutron/tests/unit/db/test_db_base_plugin_v2.py

index d6a136c1db331a69b9d002d5707847f382a16e6f..c69c543e9f8e42784be8f4b9448cb0d33e6157ee 100644 (file)
@@ -203,6 +203,18 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
         # a lot of stress on the db. Consider adding a cache layer
         return context.session.query(models_v2.Subnet).all()
 
+    def _get_subnets(self, context, filters=None, fields=None,
+                     sorts=None, limit=None, marker=None,
+                     page_reverse=False):
+        marker_obj = self._get_marker_obj(context, 'subnet', limit, marker)
+        return self._get_collection(context, models_v2.Subnet,
+                                    self._make_subnet_dict,
+                                    filters=filters, fields=fields,
+                                    sorts=sorts,
+                                    limit=limit,
+                                    marker_obj=marker_obj,
+                                    page_reverse=page_reverse)
+
     def _make_network_dict(self, network, fields=None,
                            process_extensions=True):
         res = {'id': network['id'],
index 8dcd27554a4fa3bcf620f8b5a1acea2fb0a859a9..0ff7e653d29c8aeb9cb159eb03af68686c39b6a9 100644 (file)
@@ -788,9 +788,6 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
         dns lease or we support gratuitous DHCP offers
         """
         s = subnet['subnet']
-        changed_host_routes = False
-        changed_dns = False
-        changed_allocation_pools = False
         db_subnet = self._get_subnet(context, id)
         # Fill 'ip_version' and 'allocation_pools' fields with the current
         # value since _validate_subnet() expects subnet spec has 'ip_version'
@@ -806,30 +803,10 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
             self._validate_gw_out_of_pools(s["gateway_ip"], allocation_pools)
 
         with context.session.begin(subtransactions=True):
-            if "dns_nameservers" in s:
-                changed_dns = True
-                new_dns = self._update_subnet_dns_nameservers(context, id, s)
-
-            if "host_routes" in s:
-                changed_host_routes = True
-                new_routes = self._update_subnet_host_routes(context, id, s)
-
-            if "allocation_pools" in s:
-                self._validate_allocation_pools(s['allocation_pools'],
-                                                s['cidr'])
-                changed_allocation_pools = True
-                new_pools = self._update_subnet_allocation_pools(context,
-                                                                 id, s)
-            subnet = self._get_subnet(context, id)
-            subnet.update(s)
+            subnet, changes = self._update_db_subnet(context, id, s)
         result = self._make_subnet_dict(subnet)
         # Keep up with fields that changed
-        if changed_dns:
-            result['dns_nameservers'] = new_dns
-        if changed_host_routes:
-            result['host_routes'] = new_routes
-        if changed_allocation_pools:
-            result['allocation_pools'] = new_pools
+        result.update(changes)
         return result
 
     def _subnet_check_ip_allocations(self, context, subnet_id):
@@ -912,14 +889,8 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
     def get_subnets(self, context, filters=None, fields=None,
                     sorts=None, limit=None, marker=None,
                     page_reverse=False):
-        marker_obj = self._get_marker_obj(context, 'subnet', limit, marker)
-        return self._get_collection(context, models_v2.Subnet,
-                                    self._make_subnet_dict,
-                                    filters=filters, fields=fields,
-                                    sorts=sorts,
-                                    limit=limit,
-                                    marker_obj=marker_obj,
-                                    page_reverse=page_reverse)
+        return self._get_subnets(context, filters, fields, sorts, limit,
+                                 marker, page_reverse)
 
     def get_subnets_count(self, context, filters=None):
         return self._get_collection_count(context, models_v2.Subnet,
index 74853bd619edd765b0590b16b316bde914e63b45..f7b231d12cd82934d63310c4d046e60537877177 100644 (file)
@@ -105,12 +105,12 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
         del s["dns_nameservers"]
         return new_dns
 
-    def _update_subnet_allocation_pools(self, context, id, s):
+    def _update_subnet_allocation_pools(self, context, subnet_id, s):
         context.session.query(models_v2.IPAllocationPool).filter_by(
-            subnet_id=id).delete()
+            subnet_id=subnet_id).delete()
         new_pools = [models_v2.IPAllocationPool(first_ip=p['start'],
                                                 last_ip=p['end'],
-                                                subnet_id=id)
+                                                subnet_id=subnet_id)
                      for p in s['allocation_pools']]
         context.session.add_all(new_pools)
         # Call static method with self to redefine in child
@@ -123,6 +123,26 @@ class IpamBackendMixin(db_base_plugin_common.DbBasePluginCommon):
         del s['allocation_pools']
         return result_pools
 
+    def _update_db_subnet(self, context, subnet_id, s):
+        changes = {}
+        if "dns_nameservers" in s:
+            changes['dns_nameservers'] = (
+                self._update_subnet_dns_nameservers(context, subnet_id, s))
+
+        if "host_routes" in s:
+            changes['host_routes'] = self._update_subnet_host_routes(
+                context, subnet_id, s)
+
+        if "allocation_pools" in s:
+            self._validate_allocation_pools(s['allocation_pools'],
+                                            s['cidr'])
+            changes['allocation_pools'] = (
+                self._update_subnet_allocation_pools(context, subnet_id, s))
+
+        subnet = self._get_subnet(context, subnet_id)
+        subnet.update(s)
+        return subnet, changes
+
     def _validate_allocation_pools(self, ip_pools, subnet_cidr):
         """Validate IP allocation pools.
 
index bb929975a6bcdb9ea86373f6483d23266b646d1c..c1fb4bc9631c3a83cc88344793fa3a6f199e2d45 100644 (file)
@@ -221,7 +221,7 @@ class IpamNonPluggableBackend(ipam_backend_mixin.IpamBackendMixin):
                     raise n_exc.InvalidInput(error_message=msg)
 
                 filter = {'network_id': [network_id]}
-                subnets = self.get_subnets(context, filters=filter)
+                subnets = self._get_subnets(context, filters=filter)
                 for subnet in subnets:
                     if ipam_utils.check_subnet_ip(subnet['cidr'],
                                                   fixed['ip_address']):
@@ -352,7 +352,7 @@ class IpamNonPluggableBackend(ipam_backend_mixin.IpamBackendMixin):
         ips = []
         v6_stateless = []
         net_id_filter = {'network_id': [p['network_id']]}
-        subnets = self.get_subnets(context, filters=net_id_filter)
+        subnets = self._get_subnets(context, filters=net_id_filter)
         is_router_port = (
             p['device_owner'] in constants.ROUTER_INTERFACE_OWNERS or
             p['device_owner'] == constants.DEVICE_OWNER_ROUTER_SNAT)
index a73753ce1825f03e6cc0d4ac35b3e260d65c8d42..ba99aa6248760baccff968085c851f042a483b1f 100644 (file)
@@ -5422,7 +5422,7 @@ class TestNeutronDbPluginV2(base.BaseTestCase):
     def _test__allocate_ips_for_port(self, subnets, port, expected):
         plugin = db_base_plugin_v2.NeutronDbPluginV2()
         with mock.patch.object(db_base_plugin_v2.NeutronDbPluginV2,
-                               'get_subnets') as get_subnets:
+                               '_get_subnets') as get_subnets:
             with mock.patch.object(db_base_plugin_v2.NeutronDbPluginV2,
                                    '_check_unique_ip') as check_unique:
                 context = mock.Mock()