From: Carl Baldwin Date: Fri, 10 Oct 2014 05:12:43 +0000 (+0000) Subject: Avoid constructing a RouterInfo object to get namespace name X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=f3dd0e823cbdba1cfaa37433aa56d20edd36f75e;p=openstack-build%2Fneutron-build.git Avoid constructing a RouterInfo object to get namespace name Constructing a RouterInfo object just for a string concatenation is inefficient and adds more dependence on the class which needs refactoring. Change-Id: Ibaf369d6ebe9285a0c845802def59bfa26ac0fd5 --- diff --git a/neutron/agent/l3_agent.py b/neutron/agent/l3_agent.py index 829f7dd53..e23b600fb 100644 --- a/neutron/agent/l3_agent.py +++ b/neutron/agent/l3_agent.py @@ -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) diff --git a/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py b/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py index 57edddcb4..c5c3afeef 100644 --- a/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py +++ b/neutron/tests/unit/services/firewall/agents/l3reference/test_firewall_l3_agent.py @@ -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): diff --git a/neutron/tests/unit/services/vpn/test_vpn_agent.py b/neutron/tests/unit/services/vpn/test_vpn_agent.py index 2a2a9a14f..987b26cd4 100644 --- a/neutron/tests/unit/services/vpn/test_vpn_agent.py +++ b/neutron/tests/unit/services/vpn/test_vpn_agent.py @@ -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)) diff --git a/neutron/tests/unit/test_l3_agent.py b/neutron/tests/unit/test_l3_agent.py index 264804a59..a8b74ea76 100644 --- a/neutron/tests/unit/test_l3_agent.py +++ b/neutron/tests/unit/test_l3_agent.py @@ -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(),