def get_bridge_name_for_port_name(self, port_name):
try:
- return self.run_vsctl(['port-to-br', port_name], check_error=True)
+ return self.run_vsctl(
+ ['port-to-br', port_name], check_error=True).strip()
except RuntimeError as e:
with excutils.save_and_reraise_exception() as ctxt:
if 'Exit code: 1\n' in str(e):
def port_exists(self, port_name):
return bool(self.get_bridge_name_for_port_name(port_name))
+ def get_bridge_for_iface(self, iface):
+ res = self.run_vsctl(['iface-to-br', iface])
+ if res:
+ return res.strip()
+
+ def get_bridges(self):
+ res = self.run_vsctl(['list-br'], check_error=True)
+ return res.strip().split('\n') if res else []
+
+ def get_bridge_external_bridge_id(self, bridge):
+ res = self.run_vsctl(['br-get-external-id', bridge, 'bridge-id'])
+ if res:
+ return res.strip()
+
+ def set_db_attribute(self, table_name, record, column, value):
+ args = ["set", table_name, record, "%s=%s" % (column, value)]
+ self.run_vsctl(args)
+
+ def clear_db_attribute(self, table_name, record, column):
+ args = ["clear", table_name, record, column]
+ self.run_vsctl(args)
+
+ def _db_get_map(self, table, record, column, check_error=False):
+ def db_str_to_map(full_str):
+ entries = full_str.strip("{}").split(", ")
+ ret = {}
+ for e in entries:
+ key, sep, value = e.partition('=')
+ if sep != "":
+ ret[key] = value.strip('"')
+ return ret
+
+ output = self.db_get_val(table, record, column,
+ check_error=check_error)
+ if output:
+ return db_str_to_map(output)
+ return {}
+
+ def db_get_val(self, table, record, column, check_error=False):
+ output = self.run_vsctl(["get", table, record, column], check_error)
+ if output:
+ return output.rstrip("\n\r")
+
class OVSBridge(BaseOVS):
def __init__(self, br_name, root_helper):
self.run_vsctl(["--", "--if-exists", "del-port", self.br_name,
port_name])
- def set_db_attribute(self, table_name, record, column, value):
- args = ["set", table_name, record, "%s=%s" % (column, value)]
- self.run_vsctl(args)
-
- def clear_db_attribute(self, table_name, record, column):
- args = ["clear", table_name, record, column]
- self.run_vsctl(args)
-
def run_ofctl(self, cmd, args, process_input=None):
full_args = ["ovs-ofctl", cmd, self.br_name] + args
try:
]
return self.add_port(local_name, *options)
- def db_get_map(self, table, record, column, check_error=False):
- output = self.run_vsctl(["get", table, record, column], check_error)
- if output:
- output_str = output.rstrip("\n\r")
- return self.db_str_to_map(output_str)
- return {}
-
- def db_get_val(self, table, record, column, check_error=False):
- output = self.run_vsctl(["get", table, record, column], check_error)
- if output:
- return output.rstrip("\n\r")
-
- def db_str_to_map(self, full_str):
- list = full_str.strip("{}").split(", ")
- ret = {}
- for e in list:
- if e.find("=") == -1:
- continue
- arr = e.split("=")
- ret[arr[0]] = arr[1].strip("\"")
- return ret
-
def get_port_name_list(self):
res = self.run_vsctl(["list-ports", self.br_name], check_error=True)
if res:
return []
def get_port_stats(self, port_name):
- return self.db_get_map("Interface", port_name, "statistics")
+ return self._db_get_map("Interface", port_name, "statistics")
def get_xapi_iface_id(self, xs_vif_uuid):
args = ["xe", "vif-param-get", "param-name=other-config",
edge_ports = []
port_names = self.get_port_name_list()
for name in port_names:
- external_ids = self.db_get_map("Interface", name, "external_ids",
- check_error=True)
+ external_ids = self._db_get_map("Interface", name, "external_ids",
+ check_error=True)
ofport = self.db_get_val("Interface", name, "ofport",
check_error=True)
if "iface-id" in external_ids and "attached-mac" in external_ids:
# rows since the interface identifier is unique
for data in json_result['data']:
port_name = data[name_idx]
- switch = get_bridge_for_iface(self.root_helper, port_name)
+ switch = self.get_bridge_for_iface(port_name)
if switch != self.br_name:
continue
ofport = data[ofport_idx]
self.br.br_name)
-def get_bridge_for_iface(root_helper, iface):
- args = ["ovs-vsctl", "--timeout=%d" % cfg.CONF.ovs_vsctl_timeout,
- "iface-to-br", iface]
- try:
- return utils.execute(args, root_helper=root_helper).strip()
- except Exception:
- LOG.exception(_LE("Interface %s not found."), iface)
- return None
-
-
-def get_bridges(root_helper):
- args = ["ovs-vsctl", "--timeout=%d" % cfg.CONF.ovs_vsctl_timeout,
- "list-br"]
- try:
- return utils.execute(args, root_helper=root_helper).strip().split("\n")
- except Exception as e:
- with excutils.save_and_reraise_exception():
- LOG.exception(_LE("Unable to retrieve bridges. Exception: %s"), e)
-
-
-def get_bridge_external_bridge_id(root_helper, bridge):
- args = ["ovs-vsctl", "--timeout=2", "br-get-external-id",
- bridge, "bridge-id"]
- try:
- return utils.execute(args, root_helper=root_helper).strip()
- except Exception:
- LOG.exception(_LE("Bridge %s not found."), bridge)
- return None
-
-
def _build_flow_expr_str(flow_dict, cmd):
flow_expr_arr = []
actions = None
except RuntimeError:
root_helper = agent_config.get_root_helper(conf)
# Maybe the device is OVS port, so try to delete
- bridge_name = ovs_lib.get_bridge_for_iface(root_helper, device.name)
+ ovs = ovs_lib.BaseOVS(root_helper)
+ bridge_name = ovs.get_bridge_for_iface(device.name)
if bridge_name:
bridge = ovs_lib.OVSBridge(bridge_name, root_helper)
bridge.delete_port(device.name)
configuration_bridges = set([conf.ovs_integration_bridge,
conf.external_network_bridge])
- ovs_bridges = set(ovs_lib.get_bridges(conf.AGENT.root_helper))
+ ovs = ovs_lib.BaseOVS(conf.AGENT.root_helper)
+ ovs_bridges = set(ovs.get_bridges())
available_configuration_bridges = configuration_bridges & ovs_bridges
if conf.ovs_all_ports:
def setup_ancillary_bridges(self, integ_br, tun_br):
'''Setup ancillary bridges - for example br-ex.'''
- ovs_bridges = set(ovs_lib.get_bridges(self.root_helper))
+ ovs = ovs_lib.BaseOVS(self.root_helper)
+ ovs_bridges = set(ovs.get_bridges())
# Remove all known bridges
ovs_bridges.remove(integ_br)
if self.enable_tunneling:
# bridge-id's configured
br_names = []
for bridge in ovs_bridges:
- id = ovs_lib.get_bridge_external_bridge_id(self.root_helper,
- bridge)
- if id != bridge:
+ bridge_id = ovs.get_bridge_external_bridge_id(bridge)
+ if bridge_id != bridge:
br_names.append(bridge)
ovs_bridges.difference_update(br_names)
ancillary_bridges = []
self.int_ofports = {}
self.phys_ofports = {}
ip_wrapper = ip_lib.IPWrapper(self.root_helper)
- ovs_bridges = ovs_lib.get_bridges(self.root_helper)
+ ovs = ovs_lib.BaseOVS(self.root_helper)
+ ovs_bridges = ovs.get_bridges()
for physical_network, bridge in bridge_mappings.iteritems():
LOG.info(_LI("Mapping physical network %(physical_network)s to "
"bridge %(bridge)s"),
iface = 'tap0'
br = 'br-int'
root_helper = 'sudo'
+ if exp_timeout:
+ self.br.vsctl_timeout = exp_timeout
self.execute.return_value = 'br-int'
exp_timeout_str = self._build_timeout_opt(exp_timeout)
- self.assertEqual(ovs_lib.get_bridge_for_iface(root_helper, iface), br)
+ self.assertEqual(self.br.get_bridge_for_iface(iface), br)
self.execute.assert_called_once_with(
["ovs-vsctl", exp_timeout_str, "iface-to-br", iface],
root_helper=root_helper)
root_helper = 'sudo'
self.execute.side_effect = Exception
- self.assertIsNone(ovs_lib.get_bridge_for_iface(root_helper, iface))
+ self.assertIsNone(self.br.get_bridge_for_iface(iface))
self.execute.assert_called_once_with(
["ovs-vsctl", self.TO, "iface-to-br", iface],
root_helper=root_helper)
def _test_get_bridges(self, exp_timeout=None):
bridges = ['br-int', 'br-ex']
root_helper = 'sudo'
+ if exp_timeout:
+ self.br.vsctl_timeout = exp_timeout
self.execute.return_value = 'br-int\nbr-ex\n'
timeout_str = self._build_timeout_opt(exp_timeout)
- self.assertEqual(ovs_lib.get_bridges(root_helper), bridges)
+ self.assertEqual(self.br.get_bridges(), bridges)
self.execute.assert_called_once_with(
["ovs-vsctl", timeout_str, "list-br"],
root_helper=root_helper)
return_value='00:00:00:00:00:01'),
mock.patch('neutron.agent.linux.utils.get_interface_mac',
return_value='00:00:00:00:00:01'),
- mock.patch('neutron.agent.linux.ovs_lib.'
- 'get_bridges'),
+ mock.patch('neutron.agent.linux.ovs_lib.BaseOVS.get_bridges'),
mock.patch('neutron.openstack.common.loopingcall.'
'FixedIntervalLoopingCall',
new=MockFixedIntervalLoopingCall)):
mock.patch.object(ip_lib.IpLinkCommand, "delete"),
mock.patch.object(ip_lib.IpLinkCommand, "set_up"),
mock.patch.object(ip_lib.IpLinkCommand, "set_mtu"),
- mock.patch.object(ovs_lib, "get_bridges")
+ mock.patch.object(ovs_lib.BaseOVS, "get_bridges")
) as (devex_fn, sysexit_fn, utilsexec_fn, remflows_fn, ovs_addfl_fn,
ovs_addport_fn, ovs_delport_fn, br_addport_fn, br_delport_fn,
addveth_fn, linkdel_fn, linkset_fn, linkmtu_fn, get_br_fn):
def _test_ancillary_bridges(self, bridges, ancillary):
device_ids = ancillary[:]
- def pullup_side_effect(self, *args):
+ def pullup_side_effect(*args):
# Check that the device_id exists, if it does return it
# if it does not return None
try:
return_value='00:00:00:00:00:01'),
mock.patch('neutron.agent.linux.ovs_lib.OVSBridge.'
'set_secure_mode'),
- mock.patch('neutron.agent.linux.ovs_lib.get_bridges',
+ mock.patch('neutron.agent.linux.ovs_lib.BaseOVS.get_bridges',
return_value=bridges),
- mock.patch(
- 'neutron.agent.linux.ovs_lib.get_bridge_external_bridge_id',
- side_effect=pullup_side_effect)):
+ mock.patch('neutron.agent.linux.ovs_lib.BaseOVS.'
+ 'get_bridge_external_bridge_id',
+ side_effect=pullup_side_effect)):
self.agent = ovs_neutron_agent.OVSNeutronAgent(**self.kwargs)
self.assertEqual(len(ancillary), len(self.agent.ancillary_brs))
if ancillary:
add_veth = self.ipwrapper.return_value.add_veth
add_veth.return_value = [self.inta, self.intb]
- self.get_bridges = mock.patch.object(ovs_lib, 'get_bridges').start()
+ self.get_bridges = mock.patch.object(ovs_lib.BaseOVS,
+ 'get_bridges').start()
self.get_bridges.return_value = [self.INT_BRIDGE,
self.TUN_BRIDGE,
self.MAP_TUN_BRIDGE]
self.ipdevice_expected = []
self.ipwrapper_expected = [mock.call('sudo')]
- self.get_bridges_expected = [mock.call('sudo'), mock.call('sudo')]
+ self.get_bridges_expected = [mock.call(), mock.call()]
self.inta_expected = []
self.intb_expected = []
'phy-%s' % self.MAP_TUN_BRIDGE)
]
- self.get_bridges_expected = [mock.call('sudo'), mock.call('sudo')]
+ self.get_bridges_expected = [mock.call(), mock.call()]
self.inta_expected = [mock.call.link.set_up()]
self.intb_expected = [mock.call.link.set_up()]
with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs_br_cls:
br_patch = mock.patch(
- 'neutron.agent.linux.ovs_lib.get_bridge_for_iface')
+ 'neutron.agent.linux.ovs_lib.BaseOVS.get_bridge_for_iface')
with br_patch as mock_get_bridge_for_iface:
mock_get_bridge_for_iface.return_value = 'br-int'
ovs_bridge = mock.Mock()
util.unplug_device(conf, device)
- mock_get_bridge_for_iface.assert_called_once_with(
- conf.AGENT.root_helper, 'tap1')
+ mock_get_bridge_for_iface.assert_called_once_with('tap1')
ovs_br_cls.assert_called_once_with('br-int',
conf.AGENT.root_helper)
ovs_bridge.assert_has_calls(
with mock.patch('neutron.agent.linux.ovs_lib.OVSBridge') as ovs_br_cls:
br_patch = mock.patch(
- 'neutron.agent.linux.ovs_lib.get_bridge_for_iface')
+ 'neutron.agent.linux.ovs_lib.BaseOVS.get_bridge_for_iface')
with br_patch as mock_get_bridge_for_iface:
with mock.patch.object(util.LOG, 'debug') as debug:
mock_get_bridge_for_iface.return_value = None
util.unplug_device(conf, device)
- mock_get_bridge_for_iface.assert_called_once_with(
- conf.AGENT.root_helper, 'tap1')
+ mock_get_bridge_for_iface.assert_called_once_with('tap1')
self.assertEqual(ovs_br_cls.mock_calls, [])
self.assertTrue(debug.called)
mock.patch('neutron.common.config.setup_logging'),
mock.patch('neutron.cmd.ovs_cleanup.setup_conf',
return_value=conf),
- mock.patch('neutron.agent.linux.ovs_lib.get_bridges',
+ mock.patch('neutron.agent.linux.ovs_lib.BaseOVS.get_bridges',
return_value=bridges),
mock.patch('neutron.agent.linux.ovs_lib.OVSBridge'),
mock.patch.object(util, 'collect_neutron_ports',