Example #1
0
def api_factory(context):
    global _connection
    if _connection is None:
        try:
            _connection = connection.Connection(
                idl=connection.idl_factory(),
                timeout=cfg.CONF.ovs_vsctl_timeout)
        except TypeError:
            _connection = connection.Connection(
                idl_factory=connection.idl_factory,
                timeout=cfg.CONF.ovs_vsctl_timeout)
    return NeutronOvsdbIdl(_connection)
Example #2
0
 def initialize(self):
     db_connection = ('%s:%s:%s' % (self.protocol, self.ip, self.port))
     self.ovsdb = connection.Connection(db_connection,
                                        self.timeout,
                                        'OVN_Southbound')
     self.ovsdb_nb = connection.Connection(db_connection,
                                           self.timeout,
                                           'OVN_Northbound')
     self.ovsdb.start()
     self.ovsdb_nb.start()
     self.idl = self.ovsdb.idl
     self.idl_nb = self.ovsdb_nb.idl
Example #3
0
 def test_limit_tables(self):
     self.connection = connection.Connection(cfg.CONF.OVS.ovsdb_connection,
                                             cfg.CONF.ovs_vsctl_timeout,
                                             'Open_vSwitch')
     tables = ['Open_vSwitch', 'Bridge', 'Port']
     self.connection.start(table_name_list=tables)
     self.assertItemsEqual(tables, self.connection.idl.tables.keys())
Example #4
0
class OvsdbTopoIdl(topo_api.API):

    ovsdb_connection = connection.Connection(
        cfg.CONF.OVS.ovsdb_topo_connection, cfg.CONF.ovs_vsctl_timeout, 'Topo')

    def __init__(self, context):
        super(OvsdbTopoIdl, self).__init__(context)
        OvsdbTopoIdl.ovsdb_connection.start()
        self.idl = OvsdbTopoIdl.ovsdb_connection.idl

    @property
    def _tables(self):
        return self.idl.tables

    def transaction(self, check_error=False, log_errors=True, **kwargs):
        return Transaction(self, OvsdbTopoIdl.ovsdb_connection,
                           self.context.vsctl_timeout, check_error, log_errors)

    def update_net(self, net_uuid, tenant, data):
        return cmd.UpdateNetCommand(self, net_uuid, tenant, data)

    def update_port(self, port_uuid, tenant, data, fixed_ips):
        return cmd.UpdatePortCommand(self, port_uuid, tenant, data, fixed_ips)

    def get_port(self, port_uuid):
        return cmd.GetPortCommand(self, port_uuid)

    def get_port_details(self, port_uuid):
        return cmd.GetPortDetailsCommand(self, port_uuid)

    def db_create(self, table, **col_values):
        return cmd.DbCreateCommand(self, table, **col_values)
Example #5
0
 def test_start_with_idl_class(self, wait_for_change, get_schema_helper,
                               transaction_queue):
     idl_class = mock.Mock()
     self.connection = connection.Connection(
         mock.sentinel, mock.sentinel, mock.sentinel, idl_class=idl_class)
     idl_instance = idl_class.return_value
     self.connection.start()
     self.assertEqual(idl_instance, self.connection.idl)
Example #6
0
 def _test_start(self, wfc, idl, gsh, tq, table_name_list=None):
     gsh.return_value = helper = mock.Mock()
     self.connection = connection.Connection(
         mock.Mock(), mock.Mock(), mock.Mock())
     with mock.patch.object(poller, 'Poller') as poller_mock:
         poller_mock.return_value.block.side_effect = eventlet.sleep
         self.connection.start(table_name_list=table_name_list)
     reg_all_called = table_name_list is None
     reg_table_called = table_name_list is not None
     self.assertEqual(reg_all_called, helper.register_all.called)
     self.assertEqual(reg_table_called, helper.register_table.called)
 def test_do_get_schema_helper_retry(self, mock_get_schema_helper,
                                     mock_enable_conn, mock_idl,
                                     mock_wait_for_change, mock_threading):
     mock_helper = mock.Mock()
     # raise until 3rd retry attempt
     mock_get_schema_helper.side_effect = [
         Exception(), Exception(), mock_helper
     ]
     conn = connection.Connection(mock.Mock(), mock.Mock(), mock.Mock())
     conn.start()
     self.assertEqual(3, len(mock_get_schema_helper.mock_calls))
     mock_helper.register_all.assert_called_once_with()
 def test_start_call_graph(self, wait_for_change, idl, transaction_queue):
     self.connection = connection.Connection(mock.sentinel, mock.sentinel,
                                             mock.sentinel)
     self.connection.get_schema_helper = mock.Mock()
     helper = self.connection.get_schema_helper.return_value
     self.connection.update_schema_helper = mock.Mock()
     with mock.patch.object(poller, 'Poller') as poller_mock,\
             mock.patch('threading.Thread'):
         poller_mock.return_value.block.side_effect = eventlet.sleep
         self.connection.start()
     self.connection.get_schema_helper.assert_called_once_with()
     self.connection.update_schema_helper.assert_called_once_with(helper)
Example #9
0
    def test_idl_factory(self):
        tables = ['Open_vSwitch', 'Bridge', 'Port']

        def _idl_factory():
            connection = cfg.CONF.OVS.ovsdb_connection
            helper = idlutils.get_schema_helper(connection, 'Open_vSwitch')
            for table in tables:
                helper.register_table(table)
            return idl.Idl(connection, helper)

        try:
            self.connection = connection.Connection(
                idl=_idl_factory(),
                timeout=cfg.CONF.ovs_vsctl_timeout,
            )
        except TypeError:
            self.connection = connection.Connection(
                idl_factory=_idl_factory,
                timeout=cfg.CONF.ovs_vsctl_timeout,
            )
        self.connection.start()
        self.assertItemsEqual(tables, self.connection.idl.tables.keys())
Example #10
0
    def _start_ovsdb_server_and_idls(self):
        self.temp_dir = self.useFixture(fixtures.TempDir()).path
        # Start 2 ovsdb-servers one each for OVN NB DB and OVN SB DB
        # ovsdb-server with OVN SB DB can be used to test the chassis up/down
        # events.
        self.ovsdb_server_mgr = self.useFixture(
            process.OvsdbServer(self.temp_dir, self.OVS_INSTALL_SHARE_PATH,
                                ovn_nb_db=True, ovn_sb_db=True))
        cfg.CONF.set_override(
            'ovn_nb_connection',
            self.ovsdb_server_mgr.get_ovsdb_connection_path(),
            'ovn')
        cfg.CONF.set_override(
            'ovn_sb_connection',
            self.ovsdb_server_mgr.get_ovsdb_connection_path(db_type='sb'),
            'ovn')
        num_attempts = 0
        # 5 seconds should be more than enough for the transaction to complete
        # for the test cases.
        # This also fixes the bug #1607639.
        cfg.CONF.set_override(
            'ovsdb_connection_timeout', 5,
            'ovn')

        # Created monitor IDL connection to the OVN NB DB.
        # This monitor IDL connection can be used to
        #   - Verify that the ML2 OVN driver has written to the OVN NB DB
        #     as expected.
        #   - Create and delete resources in OVN NB DB outside of the
        #     ML2 OVN driver scope to test scenarios like ovn_nb_sync.
        while num_attempts < 3:
            try:
                self.monitor_idl_con = connection.Connection(
                    self.ovsdb_server_mgr.get_ovsdb_connection_path(),
                    60, 'OVN_Northbound')
                self.monitor_idl_con.start()
                self.monitor_nb_db_idl = self.monitor_idl_con.idl
                break
            except Exception:
                LOG.exception(_LE("Error connecting to the OVN_Northbound DB"))
                num_attempts += 1
                time.sleep(1)

        trigger = mock.MagicMock()
        if self.ovn_worker:
            trigger.im_class = ovsdb_monitor.OvnWorker
            cfg.CONF.set_override('neutron_sync_mode', 'off', 'ovn')

        # mech_driver.post_fork_initialize creates the IDL connections
        self.mech_driver.post_fork_initialize(mock.ANY, mock.ANY, trigger)
 def initialize(self):
     db_connection = ('%s:%s:%s' % (self.protocol, self.ip, self.port))
     self.ovsdb = connection.Connection(db_connection, self.timeout,
                                        self.db_name)
     self.ovsdb.start()
     self.idl = self.ovsdb.idl
Example #12
0
class OvsdbOvnIdl(ovn_api.API):

    ovsdb_connection = connection.Connection(cfg.get_ovn_ovsdb_connection(),
                                             cfg.get_ovn_ovsdb_timeout(),
                                             'OVN_Northbound')

    def __init__(self):
        super(OvsdbOvnIdl, self).__init__()
        OvsdbOvnIdl.ovsdb_connection.start()
        self.idl = OvsdbOvnIdl.ovsdb_connection.idl
        self.ovsdb_timeout = cfg.get_ovn_ovsdb_timeout()

    @property
    def _tables(self):
        return self.idl.tables

    def transaction(self, check_error=False, log_errors=True, **kwargs):
        return impl_idl.Transaction(self, OvsdbOvnIdl.ovsdb_connection,
                                    self.ovsdb_timeout, check_error,
                                    log_errors)

    def create_lswitch(self, lswitch_name, may_exist=True, **columns):
        return cmd.AddLSwitchCommand(self, lswitch_name, may_exist, **columns)

    def delete_lswitch(self, lswitch_name=None, ext_id=None, if_exists=True):
        if (lswitch_name is not None):
            return cmd.DelLSwitchCommand(self, lswitch_name, if_exists)
        else:
            raise RuntimeError(
                _("Currently only supports delete "
                  "by lswitch-name"))

    def set_lswitch_ext_id(self, lswitch_id, ext_id):
        return cmd.LSwitchSetExternalIdCommand(self, lswitch_id, ext_id[0],
                                               ext_id[1])

    def create_lport(self,
                     lport_name,
                     lswitch_name,
                     may_exist=True,
                     **columns):
        return cmd.AddLogicalPortCommand(self, lport_name, lswitch_name,
                                         may_exist, **columns)

    def set_lport(self, lport_name, **columns):
        return cmd.SetLogicalPortCommand(self, lport_name, **columns)

    def delete_lport(self,
                     lport_name=None,
                     lswitch=None,
                     ext_id=None,
                     if_exists=True):
        if (lport_name is not None):
            return cmd.DelLogicalPortCommand(self, lport_name, lswitch,
                                             if_exists)
        else:
            raise RuntimeError(
                _("Currently only supports "
                  "delete by lport-name"))

    def create_acl_rule(self,
                        lswitch_name,
                        priority,
                        match,
                        action,
                        ext_ids_dict=None):
        return cmd.CreateACLRuleCommand(self, lswitch_name, priority, match,
                                        action, ext_ids_dict)

    def get_all_logical_switches_ids(self):
        result = {}
        for row in self._tables['Logical_Switch'].rows.values():
            result[row.name] = row.external_ids
        return result

    def get_all_logical_ports_ids(self):
        result = {}
        for row in self._tables['Logical_Port'].rows.values():
            result[row.name] = row.external_ids
        return result

    def create_lrouter(self, name, may_exist=True, **columns):
        return cmd.AddLRouterCommand(self, name, may_exist, **columns)

    def delete_lrouter(self, name, if_exists=True):
        return cmd.DelLRouterCommand(self, name, if_exists)

    def add_lrouter_port(self,
                         name,
                         lrouter,
                         lswitch,
                         may_exist=True,
                         **columns):
        return cmd.AddLRouterPortCommand(self, name, lrouter, lswitch,
                                         may_exist, **columns)

    def delete_lrouter_port(self, name, lrouter, lswitch, if_exists=True):
        return cmd.DelLRouterPortCommand(self, name, lrouter, lswitch,
                                         if_exists)
Example #13
0
class OvsdbIdl(api.API):

    ovsdb_connection = connection.Connection(cfg.CONF.OVS.ovsdb_connection,
                                             cfg.CONF.ovs_vsctl_timeout,
                                             'Open_vSwitch')

    def __init__(self, context):
        super(OvsdbIdl, self).__init__(context)
        # it's a chicken and egg problem: by default, the manager that
        # corresponds to the connection URI is in most cases not enabled in
        # local ovsdb, so we still need ovs-vsctl to set it to allow
        # connections
        helpers.enable_connection_uri(self.ovsdb_connection.connection)
        OvsdbIdl.ovsdb_connection.start()
        self.idl = OvsdbIdl.ovsdb_connection.idl

    @property
    def _tables(self):
        return self.idl.tables

    @property
    def _ovs(self):
        return list(self._tables['Open_vSwitch'].rows.values())[0]

    def transaction(self, check_error=False, log_errors=True, **kwargs):
        return Transaction(self, OvsdbIdl.ovsdb_connection,
                           self.context.vsctl_timeout, check_error, log_errors)

    def add_br(self, name, may_exist=True):
        return cmd.AddBridgeCommand(self, name, may_exist)

    def del_br(self, name, if_exists=True):
        return cmd.DelBridgeCommand(self, name, if_exists)

    def br_exists(self, name):
        return cmd.BridgeExistsCommand(self, name)

    def port_to_br(self, name):
        return cmd.PortToBridgeCommand(self, name)

    def iface_to_br(self, name):
        # For our purposes, ports and interfaces always have the same name
        return cmd.PortToBridgeCommand(self, name)

    def list_br(self):
        return cmd.ListBridgesCommand(self)

    def br_get_external_id(self, name, field):
        return cmd.BrGetExternalIdCommand(self, name, field)

    def br_set_external_id(self, name, field, value):
        return cmd.BrSetExternalIdCommand(self, name, field, value)

    def db_set(self, table, record, *col_values):
        return cmd.DbSetCommand(self, table, record, *col_values)

    def db_clear(self, table, record, column):
        return cmd.DbClearCommand(self, table, record, column)

    def db_get(self, table, record, column):
        return cmd.DbGetCommand(self, table, record, column)

    def db_list(self, table, records=None, columns=None, if_exists=False):
        return cmd.DbListCommand(self, table, records, columns, if_exists)

    def db_find(self, table, *conditions, **kwargs):
        return cmd.DbFindCommand(self, table, *conditions, **kwargs)

    def set_controller(self, bridge, controllers):
        return cmd.SetControllerCommand(self, bridge, controllers)

    def del_controller(self, bridge):
        return cmd.DelControllerCommand(self, bridge)

    def get_controller(self, bridge):
        return cmd.GetControllerCommand(self, bridge)

    def set_fail_mode(self, bridge, mode):
        return cmd.SetFailModeCommand(self, bridge, mode)

    def add_port(self, bridge, port, may_exist=True):
        return cmd.AddPortCommand(self, bridge, port, may_exist)

    def del_port(self, port, bridge=None, if_exists=True):
        return cmd.DelPortCommand(self, port, bridge, if_exists)

    def list_ports(self, bridge):
        return cmd.ListPortsCommand(self, bridge)
Example #14
0
 def setUp(self):
     super(OVSDBConnectionTestCase, self).setUp()
     self.connection = connection.Connection(cfg.CONF.OVS.ovsdb_connection,
                                             cfg.CONF.ovs_vsctl_timeout,
                                             'Open_vSwitch')
Example #15
0
from neutron.agent.ovsdb.native import connection
from neutron.agent.ovsdb.native import idlutils
from neutron.i18n import _LE

OPTS = [
    cfg.StrOpt('ovsdb_connection',
               default='tcp:127.0.0.1:6640',
               help=_('The connection string for the native OVSDB backend')),
]
cfg.CONF.register_opts(OPTS, 'OVS')
# TODO(twilson) DEFAULT.ovs_vsctl_timeout should be OVS.vsctl_timeout
cfg.CONF.import_opt('ovs_vsctl_timeout', 'neutron.agent.common.ovs_lib')

LOG = logging.getLogger(__name__)

ovsdb_connection = connection.Connection(cfg.CONF.OVS.ovsdb_connection,
                                         cfg.CONF.ovs_vsctl_timeout)


class Transaction(api.Transaction):
    def __init__(self, context, api, check_error=False, log_errors=False):
        self.context = context
        self.api = api
        self.check_error = check_error
        self.log_errors = log_errors
        self.commands = []
        self.results = Queue.Queue(1)

    def add(self, command):
        """Add a command to the transaction

        returns The command passed as a convenience
Example #16
0
class OvsdbIdl(api.API):

    ovsdb_connection = connection.Connection(cfg.CONF.OVS.ovsdb_connection,
                                             cfg.CONF.ovs_vsctl_timeout,
                                             'Open_vSwitch')

    def __init__(self, context):
        super(OvsdbIdl, self).__init__(context)
        OvsdbIdl.ovsdb_connection.start()
        self.idl = OvsdbIdl.ovsdb_connection.idl

    @property
    def _tables(self):
        return self.idl.tables

    @property
    def _ovs(self):
        return list(self._tables['Open_vSwitch'].rows.values())[0]

    def transaction(self, check_error=False, log_errors=True, **kwargs):
        return Transaction(self, OvsdbIdl.ovsdb_connection,
                           self.context.vsctl_timeout,
                           check_error, log_errors)

    def add_br(self, name, may_exist=True, datapath_type=None):
        return cmd.AddBridgeCommand(self, name, may_exist, datapath_type)

    def del_br(self, name, if_exists=True):
        return cmd.DelBridgeCommand(self, name, if_exists)

    def br_exists(self, name):
        return cmd.BridgeExistsCommand(self, name)

    def port_to_br(self, name):
        return cmd.PortToBridgeCommand(self, name)

    def iface_to_br(self, name):
        return cmd.InterfaceToBridgeCommand(self, name)

    def list_br(self):
        return cmd.ListBridgesCommand(self)

    def br_get_external_id(self, name, field):
        return cmd.BrGetExternalIdCommand(self, name, field)

    def br_set_external_id(self, name, field, value):
        return cmd.BrSetExternalIdCommand(self, name, field, value)

    def db_create(self, table, **col_values):
        return cmd.DbCreateCommand(self, table, **col_values)

    def db_destroy(self, table, record):
        return cmd.DbDestroyCommand(self, table, record)

    def db_set(self, table, record, *col_values):
        return cmd.DbSetCommand(self, table, record, *col_values)

    def db_clear(self, table, record, column):
        return cmd.DbClearCommand(self, table, record, column)

    def db_get(self, table, record, column):
        return cmd.DbGetCommand(self, table, record, column)

    def db_list(self, table, records=None, columns=None, if_exists=False):
        return cmd.DbListCommand(self, table, records, columns, if_exists)

    def db_find(self, table, *conditions, **kwargs):
        return cmd.DbFindCommand(self, table, *conditions, **kwargs)

    def set_controller(self, bridge, controllers):
        return cmd.SetControllerCommand(self, bridge, controllers)

    def del_controller(self, bridge):
        return cmd.DelControllerCommand(self, bridge)

    def get_controller(self, bridge):
        return cmd.GetControllerCommand(self, bridge)

    def set_fail_mode(self, bridge, mode):
        return cmd.SetFailModeCommand(self, bridge, mode)

    def add_port(self, bridge, port, may_exist=True):
        return cmd.AddPortCommand(self, bridge, port, may_exist)

    def del_port(self, port, bridge=None, if_exists=True):
        return cmd.DelPortCommand(self, port, bridge, if_exists)

    def list_ports(self, bridge):
        return cmd.ListPortsCommand(self, bridge)

    def list_ifaces(self, bridge):
        return cmd.ListIfacesCommand(self, bridge)
Example #17
0
from ovsdbapp.schema.open_vswitch import impl_idl

from neutron.agent.ovsdb.native import connection
from neutron.agent.ovsdb.native import vlog
from neutron.conf.agent import ovs_conf

NeutronOVSDBTransaction = moves.moved_class(impl_idl.OvsVsctlTransaction,
                                            'NeutronOVSDBTransaction',
                                            __name__)

VswitchdInterfaceAddException = moves.moved_class(
    impl_idl.VswitchdInterfaceAddException, 'VswitchdInterfaceAddException',
    __name__)

Transaction = moves.moved_class(transaction.Transaction, 'Transaction',
                                __name__)

ovs_conf.register_ovs_agent_opts()
_connection = connection.Connection(idl_factory=connection.idl_factory,
                                    timeout=cfg.CONF.ovs_vsctl_timeout)


def api_factory(context):
    return NeutronOvsdbIdl(_connection)


class NeutronOvsdbIdl(impl_idl.OvsdbIdl):
    def __init__(self, connection):
        vlog.use_python_logger()
        super(NeutronOvsdbIdl, self).__init__(connection)