
from __future__ import absolute_import

import os

import six

from vdsm import constants
from vdsm.storage import qemuimg
from vdsm.common.config import config


STORAGE = "00_storage"
IMAGE_NAMESPACE = '01_img'
VOLUME_NAMESPACE = '02_vol'
LVM_ACTIVATION_NAMESPACE = '03_lvm'
VOLUME_LEASE_NAMESPACE = "04_lease"

VG_EXTENT_SIZE_MB = 128
COW_OVERHEAD = 1.1


STAT_BYTES_PER_BLOCK = 512

BLOCK_SIZE_512 = 512
BLOCK_SIZE_4K = 4096

METADATA_SIZE = BLOCK_SIZE_512

BLOCK_SIZE_AUTO = 0

BLOCK_SIZE_NONE = 1

ALIGNMENT_1M = 1024 ** 2
ALIGNMENT_2M = 2 * ALIGNMENT_1M
ALIGNMENT_4M = 4 * ALIGNMENT_1M
ALIGNMENT_8M = 8 * ALIGNMENT_1M

HOSTS_512_1M = 2000
HOSTS_4K_1M = 250
HOSTS_4K_2M = 500
HOSTS_4K_4M = 1000
HOSTS_4K_8M = 2000

FILE_VOLUME_PERMISSIONS = 0o660
LEASE_FILEEXT = ".lease"


UNKNOWN_VOL = 0
PREALLOCATED_VOL = 1
SPARSE_VOL = 2

UNKNOWN_FORMAT = 3
COW_FORMAT = 4
RAW_FORMAT = 5

SHARED_VOL = 6
INTERNAL_VOL = 7
LEAF_VOL = 8

VOL_TYPE = [PREALLOCATED_VOL, SPARSE_VOL]
VOL_FORMAT = [COW_FORMAT, RAW_FORMAT]



LEGACY_V2V_DATA_DISKTYPE = "1"

VOL_DISKTYPE = frozenset([
    DATA_DISKTYPE,
    ISOF_DISKTYPE,
    MEMD_DISKTYPE,
    MEMM_DISKTYPE,
    OVFS_DISKTYPE,
    HEVD_DISKTYPE,
    HESD_DISKTYPE,
    HEMD_DISKTYPE,
    HECI_DISKTYPE,
    LEGACY_DATA_DISKTYPE,
    LEGACY_V2V_DATA_DISKTYPE,
])

VOLUME_TYPES = {UNKNOWN_VOL: 'UNKNOWN', PREALLOCATED_VOL: 'PREALLOCATED',
                SPARSE_VOL: 'SPARSE',
                UNKNOWN_FORMAT: 'UNKNOWN', COW_FORMAT: 'COW',
                RAW_FORMAT: 'RAW',
                SHARED_VOL: 'SHARED', INTERNAL_VOL: 'INTERNAL',
                LEAF_VOL: 'LEAF'}

ILLEGAL_VOL = "ILLEGAL"
LEGAL_VOL = "LEGAL"
FAKE_VOL = "FAKE"

FMT2STR = {
    COW_FORMAT: qemuimg.FORMAT.QCOW2,
    RAW_FORMAT: qemuimg.FORMAT.RAW,
}

BLANK_UUID = "00000000-0000-0000-0000-000000000000"

UUID_GLOB_PATTERN = '*-*-*-*-*'

REMOVED_IMAGE_PREFIX = "_remove_me_"
ZEROED_IMAGE_PREFIX = REMOVED_IMAGE_PREFIX + "ZERO_"


def fmt2str(format):
    return FMT2STR[format]


def type2name(volType):
    try:
        return VOLUME_TYPES[volType]
    except IndexError:
        return None


def name2type(name):
    for (k, v) in six.iteritems(VOLUME_TYPES):
        if v == name.upper():
            return k
    return None


TYPE = "TYPE"
FORMAT = "FORMAT"
DISKTYPE = "DISKTYPE"
VOLTYPE = "VOLTYPE"
PUUID = "PUUID"
DOMAIN = "DOMAIN"
CTIME = "CTIME"
IMAGE = "IMAGE"
DESCRIPTION = "DESCRIPTION"
LEGALITY = "LEGALITY"
MTIME = "MTIME"

DESCRIPTION_SIZE = 210

DEFAULT_GENERATION = 0

TAG_PREFIX_MD = "MD_"
TAG_PREFIX_IMAGE = "IU_"
TAG_PREFIX_PARENT = "PU_"
TAG_VOL_UNINIT = "OVIRT_VOL_INITIALIZING"
VOLUME_TAGS = [TAG_PREFIX_PARENT,
               TAG_PREFIX_IMAGE,
               TAG_PREFIX_MD]

SUPPORTED_BLOCKSIZE = (512,)

DOMAIN_VERSIONS = (0, 2, 3, 4, 5)

SUPPORTED_DOMAIN_VERSIONS = DOMAIN_VERSIONS

P_VDSM_LIB = os.path.join(constants.P_VDSM_LIB, 'storage/')
P_VDSM_STORAGE = os.path.join(constants.P_VDSM_RUN, 'storage/')

DOMAIN_MNT_POINT = 'mnt'
REPO_DATA_CENTER = config.get('irs', 'repository')
REPO_MOUNT_DIR = os.path.join(REPO_DATA_CENTER, DOMAIN_MNT_POINT)
