Exemple #1
0
def get_ovn_conn(viewer):
    _nb_idl = OvnIdl(constants.OVN_NB_CONNECTION, 'OVN_Northbound', viewer)
    _nb_conn = connection.Connection(
        _nb_idl, timeout=constants.OVSDB_CONNECTION_TIMEOUT)
    ovn_nb = nb_impl_idl.OvnNbApiIdlImpl(_nb_conn, start=True)
    _sb_idl = OvnIdl(constants.OVN_SB_CONNECTION, 'OVN_Southbound', viewer)
    _sb_conn = connection.Connection(
        _sb_idl, timeout=constants.OVSDB_CONNECTION_TIMEOUT)
    ovn_sb = nb_impl_idl.OvnNbApiIdlImpl(_sb_conn, start=True)
    return ovn_nb, ovn_sb
Exemple #2
0
def api_factory(context):
    global _connection
    if _connection is None:
        try:
            _connection = connection.Connection(
                idl=n_connection.idl_factory(),
                timeout=cfg.CONF.ovs_vsctl_timeout)
        except TypeError:
            #pylint: disable=unexpected-keyword-arg,no-value-for-parameter
            _connection = connection.Connection(
                idl_factory=n_connection.idl_factory,  # noqa
                timeout=cfg.CONF.ovs_vsctl_timeout)
    return NeutronOvsdbIdl(_connection)
Exemple #3
0
 def __init__(self, db_connection, timeout, db_change_callback):
     #连接到ovsdb
     idl = df_idl_from_server(db_connection, 'Open_vSwitch',
                              db_change_callback)
     type(self).ovsdb_connection = None
     ovsdb_connection = connection.Connection(idl, timeout)
     super(DFOvsdbApi, self).__init__(ovsdb_connection)
    def _ovsdb_conn(self, conn, schema):
        if schema in OVSDB_SCHEMA:
            if OVSDB_SCHEMA[schema]['conn']:
                return OVSDB_SCHEMA[schema]['conn']
            # The python-ovs Idl class (Open vSwitch Database Interface Definition Language).
            # Take it from server's database
            try:
                # The ovsdbapp Connection object
                i = connection.OvsdbIdl.from_server(conn, schema)
            except Exception as e:
                self.logger.exception(
                    'Could not retrieve schema {} from {}'.format(
                        schema, conn))
                return None

            try:
                # The ovsdbapp Connection object
                conn = connection.Connection(idl=i, timeout=3)
            except Exception as e:
                self.logger.exception('Cannot initiate OVSDB connection: {}',
                                      format(e))
                return None

            # The OVN_Northbound API implementation object
            api = OVSDB_SCHEMA[schema]['Idl'](conn)
            OVSDB_SCHEMA[schema]['conn'] = api
            return api
        else:
            self.logger.exception('Incorrect OVSDB schema: {}', format(schema))
            return None
Exemple #5
0
 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
     ]
     try:
         conn = connection.Connection(idl_factory=native_conn.idl_factory,
                                      timeout=mock.Mock())
     except TypeError:
         conn = connection.Connection(idl=native_conn.idl_factory(),
                                      timeout=mock.Mock())
     conn.start()
     self.assertEqual(3, len(mock_get_schema_helper.mock_calls))
     mock_helper.register_all.assert_called_once_with()
Exemple #6
0
def api_factory():
    global _connection
    global _idl_monitor
    if _connection is None:
        _idl_monitor = n_connection.OvsIdlMonitor()
        _connection = connection.Connection(idl=_idl_monitor,
                                            timeout=cfg.CONF.OVS.ovsdb_timeout)
    return NeutronOvsdbIdl(_connection, _idl_monitor)
Exemple #7
0
def get_ovsdb_connection(connection_string, schema, timeout, tables=None):
    helper = idlutils.get_schema_helper(connection_string, schema)
    if tables:
        for table in tables:
            helper.register_table(table)
    else:
        helper.register_all()
    return connection.Connection(idl.Idl(connection_string, helper), timeout)
Exemple #8
0
 def from_worker(cls, worker_class, driver=None):
     args = (cfg.get_ovn_sb_connection(), 'OVN_Southbound')
     if worker_class == worker.MaintenanceWorker:
         idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)
     else:
         idl_ = ovsdb_monitor.OvnSbIdl.from_server(*args, driver=driver)
     conn = connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
     return cls(conn)
Exemple #9
0
 def from_worker(cls, worker_class, driver=None):
     args = (cls.connection_string, cls.schema_helper)
     if worker_class == worker.MaintenanceWorker:
         idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)
     else:
         idl_ = ovsdb_monitor.OvnSbIdl.from_server(*args, driver=driver)
     conn = connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
     return cls(conn)
    def test_ovsdb_monitor_lock(self):
        """Test case to test the ovsdb monitor lock used by OvnConnection.

        This test case created another IDL connection to the NB DB using
        the ovsdb_monitor.OvnConnection.

        With this we will have 2 'ovsdb_monitor.OvnConnection's. At the
        start the lock should be with the IDL connection created by the
        'TestOVNFunctionalBase' setup() function.

        The port up/down events should be handled by the first IDL connection.
        Then we will restart the first IDL connection so that the 2nd IDL
        connection created in this test case gets the lock and it should
        handle the port up/down events.

        Please note that the "self.monitor_nb_idl_con" created by the base
        class is created using 'connection.Connection' and hence it will not
        contend for any lock.
        """
        fake_driver = mock.MagicMock()
        _idl = ovsdb_monitor.OvnNbIdl.from_server(
            self.ovsdb_server_mgr.get_ovsdb_connection_path(),
            'OVN_Northbound', fake_driver)
        tst_ovn_idl_conn = connection.Connection(_idl, timeout=10)
        tst_ovn_idl_conn.start()

        self.mech_driver.set_port_status_up = mock.Mock()
        self.mech_driver.set_port_status_down = mock.Mock()

        with self.port(name='port') as p:
            p = p['port']
            with self.nb_idl_transaction(self.fake_api,
                                         check_error=True) as txn:
                txn.add(
                    cmd.SetLSwitchPortCommand(self.fake_api,
                                              p['id'],
                                              True,
                                              up=False))

            self._test_port_up_down_helper(p, self.mech_driver)
            fake_driver.set_port_status_up.assert_not_called()
            fake_driver.set_port_status_down.assert_not_called()

            # Now restart the mech_driver's IDL connection.
            self.mech_driver._nb_ovn.idl.force_reconnect()
            # Wait till the test_ovn_idl_conn has acquired the lock.
            n_utils.wait_until_true(lambda: tst_ovn_idl_conn.idl.has_lock)

            self.mech_driver.set_port_status_up.reset_mock()
            self.mech_driver.set_port_status_down.reset_mock()
            fake_driver.set_port_status_up.reset_mock()
            fake_driver.set_port_status_down.reset_mock()

            self._test_port_up_down_helper(p, fake_driver)
            self.assertFalse(self.mech_driver.set_port_status_up.called)
            self.assertFalse(self.mech_driver.set_port_status_down.called)
Exemple #11
0
 def start(self):
     connection_string = config.cfg.CONF.ovs.ovsdb_connection
     helper = idlutils.get_schema_helper(connection_string, 'Open_vSwitch')
     tables = ('Open_vSwitch', 'Bridge', 'Port', 'Interface')
     for table in tables:
         helper.register_table(table)
     ovs_idl = idl.Idl(connection_string, helper)
     conn = connection.Connection(
         ovs_idl, timeout=config.cfg.CONF.ovs.ovsdb_connection_timeout)
     return idl_ovs.OvsdbIdl(conn)
Exemple #12
0
 def start(self, connection_string):
     helper = idlutils.get_schema_helper(connection_string,
                                         'Open_vSwitch')
     tables = ('Open_vSwitch', 'Bridge', 'Port', 'Interface')
     for table in tables:
         helper.register_table(table)
     ovs_idl = idl.Idl(connection_string, helper)
     ovs_idl._session.reconnect.set_probe_interval(60000)
     conn = connection.Connection(
         ovs_idl, timeout=180)
     self.idl_ovs = idl_ovs.OvsdbIdl(conn)
    def _test_connection_start(self, mock_wfc, mock_gsh, idl_class, schema):
        mock_gsh.return_value = ovs_idl.SchemaHelper(
            location=schema_files[schema])
        _idl = idl_class.from_server('punix:/tmp/fake', schema, mock.Mock())
        self.ovn_connection = connection.Connection(_idl, mock.Mock())
        with mock.patch.object(poller, 'Poller'), \
                mock.patch('threading.Thread'):
            self.ovn_connection.start()
            # A second start attempt shouldn't re-register.
            self.ovn_connection.start()

        self.ovn_connection.thread.start.assert_called_once_with()
Exemple #14
0
def get_idl_singleton():
    global _idl

    conn = cfg.CONF.net_ansible_openvswitch.ovsdb_connection
    schema = "Open_vSwitch"

    if _idl is None:
        _connection = connection.Connection(idl=idl_factory(conn, schema),
                                            timeout=10)
        _idl = impl_idl.OvsdbIdl(_connection)

    return _idl
Exemple #15
0
def short_living_ovsdb_api(api_class, idl):
    """Context manager for short living connections to the database.

    :param api_class: Class implementing the database calls
                      (e.g. from the impl_idl module)
    :param idl: An instance of IDL class (e.g. instance of OvnNbIdl)
    """
    conn = connection.Connection(idl, timeout=ovn_conf.get_ovn_ovsdb_timeout())
    api = api_class(conn)
    try:
        yield api
    finally:
        api.ovsdb_connection.stop()
    def test_ssl_connection(self, mock_cfg, mock_os, mock_get_schema_helper,
                            mock_idl, mock_threading, mock_stream):
        mock_os.path.isfile.return_value = True
        mock_cfg.CONF.OVS.ovsdb_connection = 'ssl:127.0.0.1:6640'
        mock_cfg.CONF.OVS.ssl_key_file = SSL_KEY_FILE
        mock_cfg.CONF.OVS.ssl_cert_file = SSL_CERT_FILE
        mock_cfg.CONF.OVS.ssl_ca_cert_file = SSL_CA_FILE

        conn = connection.Connection(idl=native_conn.idl_factory(), timeout=1)
        conn.start()
        mock_stream.ssl_set_private_key_file.assert_called_once_with(
            SSL_KEY_FILE)
        mock_stream.ssl_set_certificate_file.assert_called_once_with(
            SSL_CERT_FILE)
        mock_stream.ssl_set_ca_cert_file.assert_called_once_with(SSL_CA_FILE)
Exemple #17
0
 def test_ssl_connection(self):
     self.mock_os.path.isfile.return_value = True
     self.mock_cfg.CONF.OVS.ovsdb_connection = 'ssl:127.0.0.1:6640'
     self.mock_cfg.CONF.OVS.ssl_key_file = SSL_KEY_FILE
     self.mock_cfg.CONF.OVS.ssl_cert_file = SSL_CERT_FILE
     self.mock_cfg.CONF.OVS.ssl_ca_cert_file = SSL_CA_FILE
     ovs_idl_monitor = self._get_ovs_idl_monitor()
     conn = connection.Connection(idl=ovs_idl_monitor, timeout=1)
     conn.start()
     self.mock_stream.ssl_set_private_key_file.assert_called_once_with(
         SSL_KEY_FILE)
     self.mock_stream.ssl_set_certificate_file.assert_called_once_with(
         SSL_CERT_FILE)
     self.mock_stream.ssl_set_ca_cert_file.assert_called_once_with(
         SSL_CA_FILE)
Exemple #18
0
def get_connection(db_class, trigger=None, driver=None):
    # The trigger is the start() method of the worker class
    if db_class == OvsdbNbOvnIdl:
        args = (cfg.get_ovn_nb_connection(), 'OVN_Northbound')
        cls = ovsdb_monitor.OvnNbIdl
    elif db_class == OvsdbSbOvnIdl:
        args = (cfg.get_ovn_sb_connection(), 'OVN_Southbound')
        cls = ovsdb_monitor.OvnSbIdl

    if trigger and utils.get_method_class(trigger) == ovsdb_monitor.OvnWorker:
        idl_ = cls.from_server(*args, driver=driver)
    else:
        if db_class == OvsdbSbOvnIdl:
            idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)
        else:
            idl_ = ovsdb_monitor.BaseOvnIdl.from_server(*args)
    return connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
Exemple #19
0
def get_connection(db_class, trigger=None, driver=None, binding_events=False):
    if db_class == OvsdbNbOvnIdl:
        args = (cfg.get_ovn_nb_connection(), 'OVN_Northbound')
    elif db_class == OvsdbSbOvnIdl:
        args = (cfg.get_ovn_sb_connection(), 'OVN_Southbound')

    if binding_events:
        if db_class == OvsdbNbOvnIdl:
            idl_ = ovsdb_monitor.OvnNbIdl.from_server(*args, driver=driver)
        else:
            idl_ = ovsdb_monitor.OvnSbIdl.from_server(*args, driver=driver)
    else:
        if db_class == OvsdbNbOvnIdl:
            idl_ = ovsdb_monitor.BaseOvnIdl.from_server(*args)
        else:
            idl_ = ovsdb_monitor.BaseOvnSbIdl.from_server(*args)

    return connection.Connection(idl_, timeout=cfg.get_ovn_ovsdb_timeout())
Exemple #20
0
    def _get_ovsdb_client(self, module):
        try:
            from ovsdbapp.backend.ovs_idl import command
            from ovsdbapp.backend.ovs_idl import connection
            from ovsdbapp.backend.ovs_idl import idlutils
            from ovsdbapp.schema.open_vswitch import impl_idl
        except ImportError as e:
            self.module.log(msg=str(e))
            self.module.fail_json(msg="ovsdbapp module is required")

        class GetIfaceCommand(command.ReadOnlyCommand):
            def __init__(self, api, iface):
                super(GetIfaceCommand, self).__init__(api)
                self.iface = iface

            def run_idl(self, txn):
                iface = idlutils.row_by_value(self.api.idl, 'Interface',
                                              'name', self.iface)
                self.result = iface

        class TcOvsdbIdl(impl_idl.OvsdbIdl):
            def __init__(self, connection):
                super(TcOvsdbIdl, self).__init__(connection)

            def get_iface(self, iface):
                return GetIfaceCommand(self, iface)

        endpoint = ("tcp:%(host)s:%(port)s" % module.params)
        client = None
        try:
            idl = connection.OvsdbIdl.from_server(endpoint, 'Open_vSwitch')
            connection = connection.Connection(idl=idl, timeout=3)
            client = TcOvsdbIdl(connection)
        except Exception as e:
            self.module.fail_json(msg=("could not connect to openvswitch. "
                                       "error: %s") % str(e))
        return client
Exemple #21
0
    def __init__(self, nb_api, db_connection, timeout):
        if DFOvsdbApi.ovsdb_connection is None:
            idl = df_idl_from_server(nb_api, db_connection, 'Open_vSwitch')
            DFOvsdbApi.ovsdb_connection = connection.Connection(idl, timeout)

        super(DFOvsdbApi, self).__init__(DFOvsdbApi.ovsdb_connection)
Exemple #22
0
 def create_connection(cls, schema):
     idl = connection.OvsdbIdl.from_server(cls.schema_map[schema], schema)
     return connection.Connection(idl, constants.DEFAULT_TIMEOUT)
Exemple #23
0
 def start(self):
     ovsdb_monitor._check_and_set_ssl_files(self.SCHEMA)
     conn = connection.Connection(self,
                                  timeout=config.get_ovn_ovsdb_timeout())
     return idl_ovn.OvsdbSbOvnIdl(conn)
Exemple #24
0
 def create_connection(cls, schema):
     idl = connection.OvsdbIdl.from_server(cls.schema_map[schema], schema)
     return connection.Connection(idl, 0)
Exemple #25
0
 def start(self):
     conn = connection.Connection(self, timeout=180)
     ovsdbSbConn = OvsdbSbOvnIdl(conn)
     if self._events:
         self.notify_handler.watch_events(self._events)
     return ovsdbSbConn
Exemple #26
0
 def start(self):
     conn = connection.Connection(self,
                                  timeout=config.get_ovn_ovsdb_timeout())
     return idl_ovn.OvsdbSbOvnIdl(conn)
Exemple #27
0
 def set_connection(cls):
     idl = TestingOvsIdl.from_server(cls.schema_map, cls.default_tables)
     cls._connection = connection.Connection(idl, constants.DEFAULT_TIMEOUT)
Exemple #28
0
 def __init__(self, idl=None, constr=None, schema=None, timeout=60):
     self.idl = idl or ovsdb_monitor.BaseOvnIdl.from_server(constr, schema)
     self.connection = connection.Connection(idl=self.idl, timeout=timeout)
Exemple #29
0
 def start(self):
     LOG.info('Getting OvsdbSbOvnIdl for MetadataAgent with retry')
     conn = connection.Connection(self,
                                  timeout=config.get_ovn_ovsdb_timeout())
     return impl_idl_ovn.OvsdbSbOvnIdl(conn)
Exemple #30
0
def api_factory(context):
    global _connection
    if _connection is None:
        _connection = connection.Connection(idl=n_connection.idl_factory(),
                                            timeout=cfg.CONF.OVS.ovsdb_timeout)
    return NeutronOvsdbIdl(_connection)