Example #1
0
    def start(self, plugin):
        # The implementation of this function is same as the base class start()
        # except that OvnIdl object is created instead of idl.Idl
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)
                helper = do_get_schema_helper()

            helper.register_all()
            self.idl = OvnIdl(plugin, self.connection, helper)
            self.idl.set_lock(self.idl.event_lock_name)
            idlutils.wait_for_change(self.idl, self.timeout)
            # We would have received the initial dump of all the logical
            # ports as events by now. Unwatch the create events for
            # logical ports as it is no longer necessary.
            self.idl.unwatch_logical_port_create_events()
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #2
0
    def start(self, driver):
        # The implementation of this function is same as the base class start()
        # except that OvnIdl object is created instead of idl.Idl
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)

                helper = do_get_schema_helper()

            helper.register_all()
            self.idl = OvnIdl(driver, self.connection, helper)
            self.idl.set_lock(self.idl.event_lock_name)
            idlutils.wait_for_change(self.idl, self.timeout)
            # We would have received the initial dump of all the logical
            # ports as events by now. Unwatch the create events for
            # logical ports as it is no longer necessary.
            self.idl.unwatch_logical_port_create_events()
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #3
0
 def __init__(self,
              table_name,
              columns=None,
              format=None,
              respawn_interval=None,
              ovsdb_connection=None):
     self.table_name = table_name
     if ovsdb_connection:
         # if ovsdb connection is configured (e.g. tcp:ip:port), use it,
         # and there is no need to run as root
         helpers.enable_connection_uri(ovsdb_connection)
         cmd = ['ovsdb-client', 'monitor', ovsdb_connection, table_name]
         run_as_root = False
     else:
         cmd = ['ovsdb-client', 'monitor', table_name]
         run_as_root = True
     if columns:
         cmd.append(','.join(columns))
     if format:
         cmd.append('--format=%s' % format)
     super(OvsdbMonitor, self).__init__(cmd,
                                        run_as_root=run_as_root,
                                        respawn_interval=respawn_interval,
                                        log_output=True,
                                        die_on_error=False)
     self.new_events = {'added': [], 'removed': []}
Example #4
0
    def _get_schema_helper(self, tables='all'):
        db_connection = ('%s:%s:%s' % (self.protocol, self.ip, self.port))
        try:
            helper = idlutils.get_schema_helper(db_connection,
                                                self.db_name)
        except Exception:
            # We may have failed do to set-manager not being called
            helpers.enable_connection_uri(db_connection)

            # There is a small window for a race, so retry up to a second
            @retrying.retry(wait_exponential_multiplier=10,
                            stop_max_delay=1000)
            def do_get_schema_helper():
                return idlutils.get_schema_helper(db_connection,
                                                  self.db_name)
            helper = do_get_schema_helper()
        if tables == 'all':
            helper.register_all()
        elif isinstance(tables, dict):
            for table_name, columns in six.iteritems(tables):
                if columns == 'all':
                    helper.register_table(table_name)
                else:
                    helper.register_columns(table_name, columns)
        return helper
Example #5
0
    def test_add_manager_appends(self):
        ovs = ovs_lib.BaseOVS()
        ovsdb_cfg_connections = []
        manager_connections = []
        manager_removal = []

        for _ in range(5):
            _port = self.useFixture(
                port.ExclusivePort(
                    const.PROTO_NAME_TCP,
                    start=net_helpers.OVS_MANAGER_TEST_PORT_FIRST,
                    end=net_helpers.OVS_MANAGER_TEST_PORT_LAST)).port
            ovsdb_cfg_connections.append('tcp:127.0.0.1:%s' % _port)
            manager_connections.append('ptcp:%s:127.0.0.1' % _port)

        for index, conn_uri in enumerate(ovsdb_cfg_connections):
            helpers.enable_connection_uri(conn_uri)
            manager_removal.append(
                ovs.ovsdb.remove_manager(manager_connections[index]))
            self.addCleanup(manager_removal[index].execute)
            self.assertIn(manager_connections[index],
                          ovs.ovsdb.get_manager().execute())

        for remove in manager_removal:
            remove.execute()

        for connection in manager_connections:
            self.assertNotIn(connection, ovs.ovsdb.get_manager().execute())
Example #6
0
    def start(self):
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)
                helper = do_get_schema_helper()

            helper.register_all()
            self.idl = idl.Idl(self.connection, helper)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #7
0
    def start(self):
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection, self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10, stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection, self.schema_name)

                helper = do_get_schema_helper()

            helper.register_all()
            self.idl = idl.Idl(self.connection, helper)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #8
0
 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
Example #9
0
 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
Example #10
0
def get_schema_helper(connection, schema_name, retry=True):
    try:
        return _get_schema_helper(connection, schema_name)
    except Exception:
        with excutils.save_and_reraise_exception(reraise=False) as ctx:
            if not retry:
                ctx.reraise = True
            # We may have failed due to set-manager not being called
            helpers.enable_connection_uri(connection)

            # There is a small window for a race, so retry up to a second
            @tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
                            stop=tenacity.stop_after_delay(1),
                            reraise=True)
            def do_get_schema_helper():
                return _get_schema_helper(connection, schema_name)

            return do_get_schema_helper()
Example #11
0
def get_schema_helper(connection, schema_name, retry=True):
    try:
        return _get_schema_helper(connection, schema_name)
    except Exception:
        with excutils.save_and_reraise_exception(reraise=False) as ctx:
            if not retry:
                ctx.reraise = True
            # We may have failed due to set-manager not being called
            helpers.enable_connection_uri(connection)

            # There is a small window for a race, so retry up to a second
            @tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
                            stop=tenacity.stop_after_delay(1),
                            reraise=True)
            def do_get_schema_helper():
                return _get_schema_helper(connection, schema_name)

            return do_get_schema_helper()
Example #12
0
def idl_factory():
    conn = cfg.CONF.OVS.ovsdb_connection
    schema_name = 'Open_vSwitch'
    try:
        helper = idlutils.get_schema_helper(conn, schema_name)
    except Exception:
        helpers.enable_connection_uri(conn)

        @tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
                        stop=tenacity.stop_after_delay(1),
                        reraise=True)
        def do_get_schema_helper():
            return idlutils.get_schema_helper(conn, schema_name)

        helper = do_get_schema_helper()

    # TODO(twilson) We should still select only the tables/columns we use
    helper.register_all()
    return idl.Idl(conn, helper)
Example #13
0
    def get_schema_helper(self):
        """Retrieve the schema helper object from OVSDB"""
        try:
            helper = idlutils.get_schema_helper(self.connection,
                                                self.schema_name)
        except Exception:
            # We may have failed do to set-manager not being called
            helpers.enable_connection_uri(self.connection)

            # There is a small window for a race, so retry up to a second
            @tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
                            stop=tenacity.stop_after_delay(1),
                            reraise=True)
            def do_get_schema_helper():
                return idlutils.get_schema_helper(self.connection,
                                                  self.schema_name)
            helper = do_get_schema_helper()

        return helper
Example #14
0
    def get_schema_helper(self):
        """Retrieve the schema helper object from OVSDB"""
        try:
            helper = idlutils.get_schema_helper(self.connection,
                                                self.schema_name)
        except Exception:
            # We may have failed do to set-manager not being called
            helpers.enable_connection_uri(self.connection)

            # There is a small window for a race, so retry up to a second
            @tenacity.retry(wait=tenacity.wait_exponential(multiplier=0.01),
                            stop=tenacity.stop_after_delay(1),
                            reraise=True)
            def do_get_schema_helper():
                return idlutils.get_schema_helper(self.connection,
                                                  self.schema_name)
            helper = do_get_schema_helper()

        return helper
Example #15
0
 def __init__(self, table_name, columns=None, format=None,
              respawn_interval=None, ovsdb_connection=None):
     if ovsdb_connection:
         # if ovsdb connection is configured (e.g. tcp:ip:port), use it,
         # and there is no need to run as root
         helpers.enable_connection_uri(ovsdb_connection)
         cmd = ['ovsdb-client', 'monitor', ovsdb_connection, table_name]
         run_as_root = False
     else:
         cmd = ['ovsdb-client', 'monitor', table_name]
         run_as_root = True
     if columns:
         cmd.append(','.join(columns))
     if format:
         cmd.append('--format=%s' % format)
     super(OvsdbMonitor, self).__init__(cmd, run_as_root=run_as_root,
                                        respawn_interval=respawn_interval,
                                        log_output=True,
                                        die_on_error=True)
Example #16
0
    def start(self, table_name_list=None):
        """
        :param table_name_list: A list of table names for schema_helper to
                register. When this parameter is given, schema_helper will only
                register tables which name are in list. Otherwise,
                schema_helper will register all tables for given schema_name as
                default.
        """
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection,
                                              set_timeout=True)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)

                helper = do_get_schema_helper()

            if table_name_list is None:
                helper.register_all()
            else:
                for table_name in table_name_list:
                    helper.register_table(table_name)
            self.idl = self.idl_class(self.connection, helper)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
def get_schema_helper(connection_string, db_name='Open_vSwitch', tables='all'):
    try:
        helper = idlutils.get_schema_helper(connection_string, db_name)
    except Exception:
        # We may have failed do to set-manager not being called
        helpers.enable_connection_uri(connection_string)

        # There is a small window for a race, so retry up to a second
        @retrying.retry(wait_exponential_multiplier=10, stop_max_delay=1000)
        def do_get_schema_helper():
            return idlutils.get_schema_helper(connection_string, db_name)

        helper = do_get_schema_helper()
    if tables == 'all':
        helper.register_all()
    elif isinstance(tables, dict):
        for table_name, columns in six.iteritems(tables):
            if columns == 'all':
                helper.register_table(table_name)
            else:
                helper.register_columns(table_name, columns)
    return helper
Example #18
0
    def start(self, driver, table_name_list=None):
        # The implementation of this function is same as the base class start()
        # except that OvnIdl object is created instead of idl.Idl
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)

                helper = do_get_schema_helper()

            if table_name_list is None:
                helper.register_all()
            else:
                for table_name in table_name_list:
                    helper.register_table(table_name)

            idl_cls = self.get_ovn_idl_cls()
            self.idl = idl_cls(driver, self.connection, helper)
            self.idl.set_lock(self.idl.event_lock_name)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.idl.post_initialize(driver)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #19
0
    def start(self, table_name_list=None):
        """
        :param table_name_list: A list of table names for schema_helper to
                register. When this parameter is given, schema_helper will only
                register tables which name are in list. Otherwise,
                schema_helper will register all tables for given schema_name as
                default.
        """
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)
                helper = do_get_schema_helper()

            if table_name_list is None:
                helper.register_all()
            else:
                for table_name in table_name_list:
                    helper.register_table(table_name)
            self.idl = idl.Idl(self.connection, helper)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #20
0
    def start(self, driver, table_name_list=None):
        # The implementation of this function is same as the base class start()
        # except that OvnIdl object is created instead of idl.Idl
        with self.lock:
            if self.idl is not None:
                return

            try:
                helper = idlutils.get_schema_helper(self.connection,
                                                    self.schema_name)
            except Exception:
                # We may have failed do to set-manager not being called
                helpers.enable_connection_uri(self.connection)

                # There is a small window for a race, so retry up to a second
                @retrying.retry(wait_exponential_multiplier=10,
                                stop_max_delay=1000)
                def do_get_schema_helper():
                    return idlutils.get_schema_helper(self.connection,
                                                      self.schema_name)
                helper = do_get_schema_helper()

            if table_name_list is None:
                helper.register_all()
            else:
                for table_name in table_name_list:
                    helper.register_table(table_name)

            idl_cls = self.get_ovn_idl_cls()
            self.idl = idl_cls(driver, self.connection, helper)
            self.idl.set_lock(self.idl.event_lock_name)
            idlutils.wait_for_change(self.idl, self.timeout)
            self.idl.post_initialize(driver)
            self.poller = poller.Poller()
            self.thread = threading.Thread(target=self.run)
            self.thread.setDaemon(True)
            self.thread.start()
Example #21
0
def get_schema_helper(connection_string, db_name='Open_vSwitch', tables='all'):
    try:
        helper = idlutils.get_schema_helper(connection_string,
                                            db_name)
    except Exception:
        # We may have failed do to set-manager not being called
        helpers.enable_connection_uri(connection_string)

        # There is a small window for a race, so retry up to a second
        @retrying.retry(wait_exponential_multiplier=10,
                        stop_max_delay=1000)
        def do_get_schema_helper():
            return idlutils.get_schema_helper(connection_string,
                                              db_name)
        helper = do_get_schema_helper()
    if tables == 'all':
        helper.register_all()
    elif isinstance(tables, dict):
        for table_name, columns in six.iteritems(tables):
            if columns == 'all':
                helper.register_table(table_name)
            else:
                helper.register_columns(table_name, columns)
    return helper
Example #22
0
 def test_enable_connection_uri(self):
     for conn_uri, manager_uri in CONNECTION_TO_MANAGER_URI_MAP:
         helpers.enable_connection_uri(conn_uri)
         self.execute.assert_called_with(
             ['ovs-vsctl', 'set-manager', manager_uri], run_as_root=True)
Example #23
0
 def _get_ovsdb_helper(self, connection):
     try:
         return idlutils.get_schema_helper(connection, self.SCHEMA)
     except Exception:
         helpers.enable_connection_uri(connection)
         return self._do_get_schema_helper(connection)
Example #24
0
 def test_enable_connection_uri(self):
     for conn_uri, manager_uri in CONNECTION_TO_MANAGER_URI_MAP:
         helpers.enable_connection_uri(conn_uri)
         self.execute.assert_called_with(
             ['ovs-vsctl', 'set-manager', manager_uri],
             run_as_root=True)
Example #25
0
 def _get_ovsdb_helper(self, connection):
     try:
         return idlutils.get_schema_helper(connection, self.SCHEMA)
     except Exception:
         helpers.enable_connection_uri(connection)
         return self._do_get_schema_helper(connection)