From 1a4cce9f34eb58e5b7d469680fbe16f13b697586 Mon Sep 17 00:00:00 2001 From: Edan David Date: Sun, 3 Jul 2016 07:59:25 -0400 Subject: [PATCH] Use bridge_lib's FdbInterface calls instead of execute util When executing 'bridge fdb' linux command use FdbInterface methods for code reuse. Change-Id: Ic8266fcba999e0220cdfc06f6edc062ebef9ca15 --- neutron/agent/linux/bridge_lib.py | 29 ++++++++++---- .../agent/linuxbridge_neutron_agent.py | 39 +++++++------------ .../agent/test_linuxbridge_neutron_agent.py | 4 +- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/neutron/agent/linux/bridge_lib.py b/neutron/agent/linux/bridge_lib.py index c79d0c9a704..e3bf52502c6 100644 --- a/neutron/agent/linux/bridge_lib.py +++ b/neutron/agent/linux/bridge_lib.py @@ -107,18 +107,31 @@ class FdbInterface(object): """provide basic functionality to edit the FDB table""" @classmethod - def add(cls, mac, dev): - return utils.execute(['bridge', 'fdb', 'add', mac, 'dev', dev], - run_as_root=True) + def _execute(cls, op, mac, dev, ip_dst, **kwargs): + cmd = ['bridge', 'fdb', op, mac, 'dev', dev] + if ip_dst is not None: + cmd += ['dst', ip_dst] + return utils.execute(cmd, run_as_root=True, **kwargs) @classmethod - def delete(cls, mac, dev): - return utils.execute(['bridge', 'fdb', 'delete', mac, 'dev', dev], - run_as_root=True) + def add(cls, mac, dev, ip_dst=None, **kwargs): + return cls._execute('add', mac, dev, ip_dst, **kwargs) @classmethod - def show(cls, dev=None): + def append(cls, mac, dev, ip_dst=None, **kwargs): + return cls._execute('append', mac, dev, ip_dst, **kwargs) + + @classmethod + def replace(cls, mac, dev, ip_dst=None, **kwargs): + return cls._execute('replace', mac, dev, ip_dst, **kwargs) + + @classmethod + def delete(cls, mac, dev, ip_dst=None, **kwargs): + return cls._execute('delete', mac, dev, ip_dst, **kwargs) + + @classmethod + def show(cls, dev=None, **kwargs): cmd = ['bridge', 'fdb', 'show'] if dev: cmd += ['dev', dev] - return utils.execute(cmd, run_as_root=True) + return utils.execute(cmd, run_as_root=True, **kwargs) diff --git a/neutron/plugins/ml2/drivers/linuxbridge/agent/linuxbridge_neutron_agent.py b/neutron/plugins/ml2/drivers/linuxbridge/agent/linuxbridge_neutron_agent.py index 6288e5b9399..5c3952f3db8 100644 --- a/neutron/plugins/ml2/drivers/linuxbridge/agent/linuxbridge_neutron_agent.py +++ b/neutron/plugins/ml2/drivers/linuxbridge/agent/linuxbridge_neutron_agent.py @@ -633,10 +633,9 @@ class LinuxBridgeManager(amb.CommonAgentManagerBase): return False try: - utils.execute( - cmd=['bridge', 'fdb', 'append', constants.FLOODING_ENTRY[0], - 'dev', test_iface, 'dst', '1.1.1.1'], - run_as_root=True, log_fail_as_error=False) + bridge_lib.FdbInterface.append(constants.FLOODING_ENTRY[0], + test_iface, '1.1.1.1', + log_fail_as_error=False) return True except RuntimeError: return False @@ -678,8 +677,7 @@ class LinuxBridgeManager(amb.CommonAgentManagerBase): return mac in entries def fdb_bridge_entry_exists(self, mac, interface, agent_ip=None): - entries = utils.execute(['bridge', 'fdb', 'show', 'dev', interface], - run_as_root=True) + entries = bridge_lib.FdbInterface.show(interface) if not agent_ip: return mac in entries @@ -693,38 +691,29 @@ class LinuxBridgeManager(amb.CommonAgentManagerBase): if cfg.CONF.VXLAN.arp_responder: ip_lib.IPDevice(interface).neigh.delete(ip, mac) - def add_fdb_bridge_entry(self, mac, agent_ip, interface, operation="add"): - utils.execute(['bridge', 'fdb', operation, mac, 'dev', interface, - 'dst', agent_ip], - run_as_root=True, - check_exit_code=False) - - def remove_fdb_bridge_entry(self, mac, agent_ip, interface): - utils.execute(['bridge', 'fdb', 'del', mac, 'dev', interface, - 'dst', agent_ip], - run_as_root=True, - check_exit_code=False) - def add_fdb_entries(self, agent_ip, ports, interface): for mac, ip in ports: if mac != constants.FLOODING_ENTRY[0]: self.add_fdb_ip_entry(mac, ip, interface) - self.add_fdb_bridge_entry(mac, agent_ip, interface, - operation="replace") + bridge_lib.FdbInterface.replace(mac, interface, agent_ip, + check_exit_code=False) elif self.vxlan_mode == lconst.VXLAN_UCAST: if self.fdb_bridge_entry_exists(mac, interface): - self.add_fdb_bridge_entry(mac, agent_ip, interface, - "append") + bridge_lib.FdbInterface.append(mac, interface, agent_ip, + check_exit_code=False) else: - self.add_fdb_bridge_entry(mac, agent_ip, interface) + bridge_lib.FdbInterface.add(mac, interface, agent_ip, + check_exit_code=False) def remove_fdb_entries(self, agent_ip, ports, interface): for mac, ip in ports: if mac != constants.FLOODING_ENTRY[0]: self.remove_fdb_ip_entry(mac, ip, interface) - self.remove_fdb_bridge_entry(mac, agent_ip, interface) + bridge_lib.FdbInterface.delete(mac, interface, agent_ip, + check_exit_code=False) elif self.vxlan_mode == lconst.VXLAN_UCAST: - self.remove_fdb_bridge_entry(mac, agent_ip, interface) + bridge_lib.FdbInterface.delete(mac, interface, agent_ip, + check_exit_code=False) def get_agent_id(self): if self.bridge_mappings: diff --git a/neutron/tests/unit/plugins/ml2/drivers/linuxbridge/agent/test_linuxbridge_neutron_agent.py b/neutron/tests/unit/plugins/ml2/drivers/linuxbridge/agent/test_linuxbridge_neutron_agent.py index 2b8a55ec0e0..3aa73493adf 100644 --- a/neutron/tests/unit/plugins/ml2/drivers/linuxbridge/agent/test_linuxbridge_neutron_agent.py +++ b/neutron/tests/unit/plugins/ml2/drivers/linuxbridge/agent/test_linuxbridge_neutron_agent.py @@ -1035,12 +1035,12 @@ class TestLinuxBridgeRpcCallbacks(base.BaseTestCase): self.lb_rpc.fdb_remove(None, fdb_entries) expected = [ - mock.call(['bridge', 'fdb', 'del', + mock.call(['bridge', 'fdb', 'delete', constants.FLOODING_ENTRY[0], 'dev', 'vxlan-1', 'dst', 'agent_ip'], run_as_root=True, check_exit_code=False), - mock.call(['bridge', 'fdb', 'del', 'port_mac', + mock.call(['bridge', 'fdb', 'delete', 'port_mac', 'dev', 'vxlan-1', 'dst', 'agent_ip'], run_as_root=True, check_exit_code=False),