'Create a bridge unless it already exists.
:param interface: the interface to create the bridge on.
:param net_attrs: dictionary with  attributes used to create bridge.
:param gateway: whether or not the bridge is a gateway.
:param filtering: whether or not to create filters on the bridge.
If net_attrs is set, it will add the net_attrs[\'gateway\'] to the bridge
using net_attrs[\'broadcast\'] and net_attrs[\'cidr\'].  It will also add
the ip_v6 address specified in net_attrs[\'cidr_v6\'] if use_ipv6 is set.
The code will attempt to move any IPs that already exist on the
interface onto the bridge and reset the default gateway if necessary.'

@staticmethod
@utils.synchronized('lock_bridge', external=True)
def ensure_bridge(bridge, interface, net_attrs=None, gateway=True, filtering=True):

    if (not device_exists(bridge)):
        LOG.debug('Starting    Bridge    %s', bridge)
        (out, err) = _execute('brctl', 'addbr', bridge, check_exit_code=False, run_as_root=True)
        if (err and (err != ("device    %s    already    exists;    can't    create    bridge    with    the    same    name\n" % bridge))):
            msg = (_('Failed    to    add    bridge:    %s') % err)
            raise exception.NovaException(msg)
        _execute('brctl', 'setfd', bridge, 0, run_as_root=True)
        _execute('brctl', 'stp', bridge, 'off', run_as_root=True)
        _execute('ip', 'link', 'set', bridge, 'up', run_as_root=True)
    if interface:
        LOG.debug('Adding    interface    %(interface)s    to    bridge    %(bridge)s', {'interface': interface, 'bridge': bridge})
        (out, err) = _execute('brctl', 'addif', bridge, interface, check_exit_code=False, run_as_root=True)
        if (err and (err != ("device    %s    is    already    a    member    of    a    bridge;    can't    enslave    it    to    bridge    %s.\n" % (interface, bridge)))):
            msg = (_('Failed    to    add    interface:    %s') % err)
            raise exception.NovaException(msg)
        if (not CONF.fake_network):
            interface_addrs = netifaces.ifaddresses(interface)
            interface_mac = interface_addrs[netifaces.AF_LINK][0]['addr']
            _execute('ip', 'link', 'set', bridge, 'address', interface_mac, run_as_root=True)
        (out, err) = _execute('ip', 'link', 'set', interface, 'up', check_exit_code=False, run_as_root=True)
        old_routes = []
        (out, err) = _execute('ip', 'route', 'show', 'dev', interface)
        for line in out.split('\n'):
            fields = line.split()
            if (fields and ('via' in fields)):
                old_routes.append(fields)
                _execute('ip', 'route', 'del', run_as_root=True, *fields)
        (out, err) = _execute('ip', 'addr', 'show', 'dev', interface, 'scope', 'global')
        for line in out.split('\n'):
            fields = line.split()
            if (fields and (fields[0] == 'inet')):
                if (fields[(-2)] in ('secondary', 'dynamic')):
                    params = fields[1:(-2)]
                else:
                    params = fields[1:(-1)]
                _execute(run_as_root=True, check_exit_code=[0, 2, 254], *_ip_bridge_cmd('del', params, fields[(-1)]))
                _execute(run_as_root=True, check_exit_code=[0, 2, 254], *_ip_bridge_cmd('add', params, bridge))
        for fields in old_routes:
            _execute('ip', 'route', 'add', run_as_root=True, *fields)
    if filtering:
        ipv4_filter = iptables_manager.ipv4['filter']
        if gateway:
            for rule in get_gateway_rules(bridge):
                ipv4_filter.add_rule(*rule)
        else:
            ipv4_filter.add_rule('FORWARD', ('--in-interface    %s    -j    %s' % (bridge, CONF.iptables_drop_action)))
            ipv4_filter.add_rule('FORWARD', ('--out-interface    %s    -j    %s' % (bridge, CONF.iptables_drop_action)))
