
from __future__ import absolute_import
from __future__ import division

import six

ERR_OK = 0
ERR_BAD_PARAMS = 21
ERR_BAD_ADDR = 22
ERR_BAD_NIC = 23
ERR_USED_NIC = 24
ERR_BAD_BONDING = 25
ERR_BAD_VLAN = 26
ERR_BAD_BRIDGE = 27
ERR_USED_BRIDGE = 28
ERR_FAILED_IFUP = 29
ERR_FAILED_IFDOWN = 30
ERR_USED_BOND = 31
ERR_OVS_CONNECTION = 32


class ConfigNetworkError(Exception):
    def __init__(self, errCode, message):
        self.errCode = errCode
        self.message = message
        super(ConfigNetworkError, self).__init__(errCode, message)


class OvsDBConnectionError(ConfigNetworkError):
    def __init__(self, *args):
        message = _get_message(args)
        super(OvsDBConnectionError, self).__init__(errCode=ERR_OVS_CONNECTION,
                                                   message=message)

    @staticmethod
    def is_ovs_db_conn_error(err_msg):
        return 'database connection failed' in err_msg[0]


class RollbackIncomplete(Exception):
    pass


def _get_message(args):
    for arg in args:
        if isinstance(arg, six.string_types):
            return arg
