]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Refactor code in update_subnet, splitting into individual methods
authormarios <marios@redhat.com>
Fri, 30 May 2014 13:04:18 +0000 (16:04 +0300)
committerMarios Andreou <marios@redhat.com>
Mon, 9 Jun 2014 16:46:40 +0000 (16:46 +0000)
The update_subnet was already long and complex enough and adding
more code to it was undesirable, after commit

I47a3a71d0d196b76eda46b1d960193fb60417ba9

Hence this refactor splits parts of the update_subnet code into
dedicated sub-methods.

Change-Id: I8f98938da88db287290493dc0daf7c81b8b5f542

neutron/db/db_base_plugin_v2.py

index 4d83957712f9046172dc9ad4f4f06d46ef1d310f..cde0fbfede73b0937a95c6cae4ff6f4167c8aa60 100644 (file)
@@ -1213,6 +1213,58 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
 
         return self._make_subnet_dict(subnet)
 
+    def _update_subnet_dns_nameservers(self, context, id, s):
+        old_dns_list = self._get_dns_by_subnet(context, id)
+        new_dns_addr_set = set(s["dns_nameservers"])
+        old_dns_addr_set = set([dns['address']
+                                for dns in old_dns_list])
+
+        new_dns = list(new_dns_addr_set)
+        for dns_addr in old_dns_addr_set - new_dns_addr_set:
+            for dns in old_dns_list:
+                if dns['address'] == dns_addr:
+                    context.session.delete(dns)
+        for dns_addr in new_dns_addr_set - old_dns_addr_set:
+            dns = models_v2.DNSNameServer(
+                address=dns_addr,
+                subnet_id=id)
+            context.session.add(dns)
+        del s["dns_nameservers"]
+        return new_dns
+
+    def _update_subnet_host_routes(self, context, id, s):
+
+        def _combine(ht):
+            return ht['destination'] + "_" + ht['nexthop']
+
+        old_route_list = self._get_route_by_subnet(context, id)
+
+        new_route_set = set([_combine(route)
+                             for route in s['host_routes']])
+
+        old_route_set = set([_combine(route)
+                             for route in old_route_list])
+
+        for route_str in old_route_set - new_route_set:
+            for route in old_route_list:
+                if _combine(route) == route_str:
+                    context.session.delete(route)
+        for route_str in new_route_set - old_route_set:
+            route = models_v2.SubnetRoute(
+                destination=route_str.partition("_")[0],
+                nexthop=route_str.partition("_")[2],
+                subnet_id=id)
+            context.session.add(route)
+
+        # Gather host routes for result
+        new_routes = []
+        for route_str in new_route_set:
+            new_routes.append(
+                {'destination': route_str.partition("_")[0],
+                 'nexthop': route_str.partition("_")[2]})
+        del s["host_routes"]
+        return new_routes
+
     def _update_subnet_allocation_pools(self, context, id, s):
         context.session.query(models_v2.IPAllocationPool).filter_by(
             subnet_id=id).delete()
@@ -1255,54 +1307,11 @@ class NeutronDbPluginV2(neutron_plugin_base_v2.NeutronPluginBaseV2,
         with context.session.begin(subtransactions=True):
             if "dns_nameservers" in s:
                 changed_dns = True
-                old_dns_list = self._get_dns_by_subnet(context, id)
-                new_dns_addr_set = set(s["dns_nameservers"])
-                old_dns_addr_set = set([dns['address']
-                                        for dns in old_dns_list])
-
-                new_dns = list(new_dns_addr_set)
-                for dns_addr in old_dns_addr_set - new_dns_addr_set:
-                    for dns in old_dns_list:
-                        if dns['address'] == dns_addr:
-                            context.session.delete(dns)
-                for dns_addr in new_dns_addr_set - old_dns_addr_set:
-                    dns = models_v2.DNSNameServer(
-                        address=dns_addr,
-                        subnet_id=id)
-                    context.session.add(dns)
-                del s["dns_nameservers"]
-
-            def _combine(ht):
-                return ht['destination'] + "_" + ht['nexthop']
+                new_dns = self._update_subnet_dns_nameservers(context, id, s)
 
             if "host_routes" in s:
                 changed_host_routes = True
-                old_route_list = self._get_route_by_subnet(context, id)
-
-                new_route_set = set([_combine(route)
-                                     for route in s['host_routes']])
-
-                old_route_set = set([_combine(route)
-                                     for route in old_route_list])
-
-                for route_str in old_route_set - new_route_set:
-                    for route in old_route_list:
-                        if _combine(route) == route_str:
-                            context.session.delete(route)
-                for route_str in new_route_set - old_route_set:
-                    route = models_v2.SubnetRoute(
-                        destination=route_str.partition("_")[0],
-                        nexthop=route_str.partition("_")[2],
-                        subnet_id=id)
-                    context.session.add(route)
-
-                # Gather host routes for result
-                new_routes = []
-                for route_str in new_route_set:
-                    new_routes.append(
-                        {'destination': route_str.partition("_")[0],
-                         'nexthop': route_str.partition("_")[2]})
-                del s["host_routes"]
+                new_routes = self._update_subnet_host_routes(context, id, s)
 
             if "allocation_pools" in s:
                 self._validate_allocation_pools(s['allocation_pools'],