]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Collapse create_subnet into single method
authorPavel Bondar <pbondar@infoblox.com>
Thu, 25 Jun 2015 13:32:22 +0000 (16:32 +0300)
committerPavel Bondar <pbondar@infoblox.com>
Thu, 2 Jul 2015 09:11:43 +0000 (09:11 +0000)
Previously create_subnet called different methods for subnet allocation
with subnetpool and without it.

_create_subnet_from_implicit_pool and _create_subnet_from_pool
were collapsed into single method _create_subnet.
This is intermediate step for supporting pluggable ipam.

Partially-Implements: blueprint neutron-ipam

Change-Id: Ia6cfc2c15e29f983a623772f5473166c075a20e4

neutron/db/db_base_plugin_common.py
neutron/db/db_base_plugin_v2.py
neutron/db/ipam_non_pluggable_backend.py

index 1bbca99e10ba19f18fc2cb04b7ee51b72c04dec1..29816ca39ac026cad85faec107ab554d16e70c09 100644 (file)
@@ -227,7 +227,7 @@ class DbBasePluginCommon(common_db_mixin.CommonDbMixin):
                 attributes.NETWORKS, res, network)
         return self._fields(res, fields)
 
-    def _make_subnet_args(self, context, shared, detail,
+    def _make_subnet_args(self, shared, detail,
                           subnet, subnetpool_id=None):
         args = {'tenant_id': detail.tenant_id,
                 'id': detail.subnet_id,
index 52a09188ddfa89fe1bf5970cfb3151e8cc0243fc..a30cb7fb6266c07ce3801292c6ba71e85012a89c 100644 (file)
@@ -442,60 +442,23 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
     @oslo_db_api.wrap_db_retry(max_retries=db_api.MAX_RETRIES,
                                retry_on_request=True,
                                retry_on_deadlock=True)
-    def _create_subnet_from_pool(self, context, subnet, subnetpool_id):
+    def _create_subnet(self, context, subnet, subnetpool_id):
         s = subnet['subnet']
-        self._validate_pools_with_subnetpool(s)
 
-        with context.session.begin(subtransactions=True):
-            subnetpool = self._get_subnetpool(context, subnetpool_id)
-            self._validate_ip_version_with_subnetpool(s, subnetpool)
-
-            network = self._get_network(context, s["network_id"])
-            allocator = subnet_alloc.SubnetAllocator(subnetpool, context)
-            req = ipam.SubnetRequestFactory.get_request(context, s, subnetpool)
-
-            ipam_subnet = allocator.allocate_subnet(req)
-            detail = ipam_subnet.get_details()
-            subnet = self._save_subnet(context,
-                                       network,
-                                       self._make_subnet_args(
-                                              context,
-                                              network.shared,
-                                              detail,
-                                              s,
-                                              subnetpool_id=subnetpool['id']),
-                                       s['dns_nameservers'],
-                                       s['host_routes'],
-                                       s['allocation_pools'])
-        if hasattr(network, 'external') and network.external:
-            self._update_router_gw_ports(context,
-                                         network,
-                                         subnet)
-        return self._make_subnet_dict(subnet)
-
-    def _create_subnet_from_implicit_pool(self, context, subnet):
-        s = subnet['subnet']
-        self._validate_subnet(context, s)
-        id = s.get('id', uuidutils.generate_uuid())
-        detail = ipam.SpecificSubnetRequest(s['tenant_id'],
-                                            id,
-                                            s['cidr'])
         with context.session.begin(subtransactions=True):
             network = self._get_network(context, s["network_id"])
-            self._validate_subnet_cidr(context, network, s['cidr'])
-            subnet = self._save_subnet(context,
-                                       network,
-                                       self._make_subnet_args(context,
-                                                              network.shared,
-                                                              detail,
-                                                              s),
-                                       s['dns_nameservers'],
-                                       s['host_routes'],
-                                       s['allocation_pools'])
+            subnet = self._allocate_subnet(context,
+                                           network,
+                                           s,
+                                           subnetpool_id)
         if hasattr(network, 'external') and network.external:
             self._update_router_gw_ports(context,
                                          network,
                                          subnet)
+        # If this subnet supports auto-addressing, then update any
+        # internal ports on the network with addresses for this subnet.
+        if ipv6_utils.is_auto_address_subnet(subnet):
+            self._add_auto_addrs_on_network_ports(context, subnet)
         return self._make_subnet_dict(subnet)
 
     def _get_subnetpool_id(self, subnet):
@@ -550,24 +513,16 @@ class NeutronDbPluginV2(ipam_non_pluggable_backend.IpamNonPluggableBackend,
 
         s['tenant_id'] = self._get_tenant_id_for_create(context, s)
         subnetpool_id = self._get_subnetpool_id(s)
-        if not subnetpool_id:
+        if subnetpool_id:
+            self._validate_pools_with_subnetpool(s)
+        else:
             if not has_cidr:
                 msg = _('A cidr must be specified in the absence of a '
                         'subnet pool')
                 raise n_exc.BadRequest(resource='subnets', msg=msg)
-            # Create subnet from the implicit(AKA null) pool
-            created_subnet = self._create_subnet_from_implicit_pool(context,
-                                                                    subnet)
-        else:
-            created_subnet = self._create_subnet_from_pool(context, subnet,
-                                                           subnetpool_id)
-
-        # If this subnet supports auto-addressing, then update any
-        # internal ports on the network with addresses for this subnet.
-        if ipv6_utils.is_auto_address_subnet(created_subnet):
-            self._add_auto_addrs_on_network_ports(context, created_subnet)
+            self._validate_subnet(context, s)
 
-        return created_subnet
+        return self._create_subnet(context, subnet, subnetpool_id)
 
     def update_subnet(self, context, id, subnet):
         """Update the subnet with new info.
index 4941776f0c0abeb4fbd16faf51b8f3c2ba8e26a7..f97d603867db0a075e808d964faab46e8b3b93db 100644 (file)
@@ -27,6 +27,8 @@ from neutron.common import exceptions as n_exc
 from neutron.common import ipv6_utils
 from neutron.db import ipam_backend_mixin
 from neutron.db import models_v2
+from neutron import ipam
+from neutron.ipam import subnet_alloc
 from neutron.ipam import utils as ipam_utils
 
 LOG = logging.getLogger(__name__)
@@ -465,3 +467,30 @@ class IpamNonPluggableBackend(ipam_backend_mixin.IpamBackendMixin):
             raise n_exc.IpAddressInUse(net_id=network_id,
                                        ip_address=ip_address)
         return ip_address
+
+    def _allocate_subnet(self, context, network, subnet, subnetpool_id):
+        subnetpool = None
+        if subnetpool_id:
+            subnetpool = self._get_subnetpool(context, subnetpool_id)
+            self._validate_ip_version_with_subnetpool(subnet, subnetpool)
+
+        subnet_request = ipam.SubnetRequestFactory.get_request(context,
+                                                               subnet,
+                                                               subnetpool)
+
+        if subnetpool_id:
+            driver = subnet_alloc.SubnetAllocator(subnetpool, context)
+            ipam_subnet = driver.allocate_subnet(subnet_request)
+            subnet_request = ipam_subnet.get_details()
+
+        subnet = self._save_subnet(context,
+                                   network,
+                                   self._make_subnet_args(
+                                       network.shared,
+                                       subnet_request,
+                                       subnet,
+                                       subnetpool_id),
+                                   subnet['dns_nameservers'],
+                                   subnet['host_routes'],
+                                   subnet['allocation_pools'])
+        return subnet