]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Avoid constructing a RouterInfo object to get namespace name
authorCarl Baldwin <carl.baldwin@hp.com>
Fri, 10 Oct 2014 05:12:43 +0000 (05:12 +0000)
committerCarl Baldwin <carl.baldwin@hp.com>
Fri, 10 Oct 2014 19:02:54 +0000 (19:02 +0000)
Constructing a RouterInfo object just for a string concatenation is
inefficient and adds more dependence on the class which needs
refactoring.

Change-Id: Ibaf369d6ebe9285a0c845802def59bfa26ac0fd5

neutron/agent/l3_agent.py
neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py
neutron/tests/unit/services/vpn/test_vpn_agent.py
neutron/tests/unit/test_l3_agent.py

index 829f7dd532ef145c89409d4a6ac293a4046386c7..e23b600fbbd5b6f40c8715bbc4a9ea40b9e981b4 100644 (file)
@@ -244,7 +244,7 @@ class LinkLocalAllocator(object):
 class RouterInfo(l3_ha_agent.RouterMixin):
 
     def __init__(self, router_id, root_helper, use_namespaces, router,
-                 use_ipv6=False):
+                 use_ipv6=False, ns_name=None):
         self.router_id = router_id
         self.ex_gw_port = None
         self._snat_enabled = None
@@ -257,6 +257,7 @@ class RouterInfo(l3_ha_agent.RouterMixin):
         self.use_namespaces = use_namespaces
         # Invoke the setter for establishing initial SNAT action
         self.router = router
+        self.ns_name = ns_name
         self.iptables_manager = iptables_manager.IptablesManager(
             root_helper=root_helper,
             use_ipv6=use_ipv6,
@@ -289,10 +290,6 @@ class RouterInfo(l3_ha_agent.RouterMixin):
             # Gateway port was removed, remove rules
             self._snat_action = 'remove_rules'
 
-    @property
-    def ns_name(self):
-        return NS_PREFIX + self.router_id if self.use_namespaces else None
-
     def perform_snat_action(self, snat_callback, *args):
         # Process SNAT rules for attached subnets
         if self._snat_action:
@@ -576,14 +573,6 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback,
         self.target_ex_net_id = None
         self.use_ipv6 = ipv6_utils.is_enabled()
 
-    def _get_router_info(self, router_id, router):
-        return RouterInfo(
-            router_id=router_id,
-            root_helper=self.root_helper,
-            use_namespaces=self.conf.use_namespaces,
-            router=router,
-            use_ipv6=self.use_ipv6)
-
     def _check_config_params(self):
         """Check items in configuration files.
 
@@ -619,9 +608,8 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback,
             return set()
 
     def _get_routers_namespaces(self, router_ids):
-        namespaces = set(self._get_router_info(id, router=None).ns_name
-                         for id in router_ids)
-        namespaces.update(self.get_snat_ns_name(id) for id in router_ids)
+        namespaces = set(self.get_ns_name(rid) for rid in router_ids)
+        namespaces.update(self.get_snat_ns_name(rid) for rid in router_ids)
         return namespaces
 
     def _cleanup_namespaces(self, router_namespaces, router_ids):
@@ -764,7 +752,14 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback,
                     raise Exception(msg)
 
     def _router_added(self, router_id, router):
-        ri = self._get_router_info(router_id, router)
+        ns_name = (self.get_ns_name(router_id)
+                   if self.conf.use_namespaces else None)
+        ri = RouterInfo(router_id=router_id,
+                        root_helper=self.root_helper,
+                        use_namespaces=self.conf.use_namespaces,
+                        router=router,
+                        use_ipv6=self.use_ipv6,
+                        ns_name=ns_name)
         self.router_info[router_id] = ri
         if self.conf.use_namespaces:
             self._create_router_namespace(ri)
@@ -1253,6 +1248,9 @@ class L3NATAgent(firewall_l3_agent.FWaaSL3AgentRpcCallback,
     def get_fip_ns_name(self, ext_net_id):
         return (FIP_NS_PREFIX + ext_net_id)
 
+    def get_ns_name(self, router_id):
+        return (NS_PREFIX + router_id)
+
     def get_snat_ns_name(self, router_id):
         return (SNAT_NS_PREFIX + router_id)
 
index 57edddcb45f7a66022575b1bf09ad9b6513c1aed..c5c3afeef428c85141507000c2eb2c6ea91cbc20 100644 (file)
@@ -335,8 +335,9 @@ class TestFwaasL3AgentRpcCallback(base.BaseTestCase):
 
     def _prepare_router_data(self, use_namespaces):
         router = {'id': str(uuid.uuid4()), 'tenant_id': str(uuid.uuid4())}
+        ns = "ns-" + router['id']
         return l3_agent.RouterInfo(router['id'], self.conf.root_helper,
-                                   use_namespaces, router=router)
+                                   use_namespaces, router=router, ns_name=ns)
 
     def _get_router_info_list_with_namespace_helper(self,
                                                     router_use_namespaces):
index 2a2a9a14f8dea0f87007f0333a3e75ab67f7f8e8..987b26cd4d115237ca32e12be6b1d7aad57f8b06 100644 (file)
@@ -93,8 +93,9 @@ class TestVPNAgent(base.BaseTestCase):
 
     def test_get_namespace(self):
         router_id = _uuid()
+        ns = "ns-" + router_id
         ri = l3_agent.RouterInfo(router_id, self.conf.root_helper,
-                                 self.conf.use_namespaces, {})
+                                 self.conf.use_namespaces, {}, ns_name=ns)
         self.agent.router_info = {router_id: ri}
         namespace = self.agent.get_namespace(router_id)
         self.assertTrue(namespace.endswith(router_id))
index 264804a596e1d2d637358ac52ba1f7e46bbee92f..a8b74ea76f79247050ab559afcf107e94805630d 100644 (file)
@@ -431,8 +431,9 @@ class TestBasicRouterOperations(base.BaseTestCase):
 
     def test_router_info_create(self):
         id = _uuid()
+        ns = "ns-" + id
         ri = l3_agent.RouterInfo(id, self.conf.root_helper,
-                                 self.conf.use_namespaces, {})
+                                 self.conf.use_namespaces, {}, ns_name=ns)
 
         self.assertTrue(ri.ns_name.endswith(id))
 
@@ -449,8 +450,9 @@ class TestBasicRouterOperations(base.BaseTestCase):
             'enable_snat': True,
             'routes': [],
             'gw_port': ex_gw_port}
+        ns = "ns-" + id
         ri = l3_agent.RouterInfo(id, self.conf.root_helper,
-                                 self.conf.use_namespaces, router)
+                                 self.conf.use_namespaces, router, ns_name=ns)
         self.assertTrue(ri.ns_name.endswith(id))
         self.assertEqual(ri.router, router)
 
@@ -520,9 +522,10 @@ class TestBasicRouterOperations(base.BaseTestCase):
         self._test_internal_network_action('remove')
 
     def _test_external_gateway_action(self, action, router):
-        ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper,
-                                 self.conf.use_namespaces, router=router)
         agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
+        ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper,
+                                 self.conf.use_namespaces, router=router,
+                                 ns_name=agent.get_ns_name(router['id']))
         # Special setup for dvr routers
         if router.get('distributed'):
             agent.conf.agent_mode = 'dvr_snat'
@@ -590,9 +593,10 @@ class TestBasicRouterOperations(base.BaseTestCase):
 
     def test_external_gateway_updated(self):
         router = prepare_router_data(num_internal_ports=2)
-        ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper,
-                                 self.conf.use_namespaces, router=router)
         agent = l3_agent.L3NATAgent(HOSTNAME, self.conf)
+        ri = l3_agent.RouterInfo(router['id'], self.conf.root_helper,
+                                 self.conf.use_namespaces, router=router,
+                                 ns_name=agent.get_ns_name(router['id']))
         interface_name, ex_gw_port = self._prepare_ext_gw_test(agent)
 
         fake_fip = {'floatingips': [{'id': _uuid(),