<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def rename_index(self, old_name, new_name=None):
 """ Renames an index. :param old_name: The name of the index to rename from. :type old_name: str :param new_name: The name of the index to rename to. :type new_name: str or None :rtype: Table """

        old_name = self._normalize_identifier(old_name)
        normalized_new_name = self._normalize_identifier(new_name)

        if old_name == normalized_new_name:
            return self

        if not self.has_index(old_name):
            raise IndexDoesNotExist(old_name, self._name)

        if self.has_index(normalized_new_name):
            raise IndexAlreadyExists(normalized_new_name, self._name)

        old_index = self._indexes[old_name]

        if old_index.is_primary():
            self.drop_primary_key()

            return self.set_primary_key(old_index.get_columns(), new_name)

        del self._indexes[old_name]

        if old_index.is_unique():
            return self.add_unique_index(old_index.get_columns(), new_name)

        return self.add_index(old_index.get_columns(), new_name, old_index.get_flags())