# Copyright 2013 Cloudbase Solutions Srl
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""
Utility class for VM related operations.
Based on the "root/virtualization/v2" namespace available starting with
Hyper-V Server / Windows Server 2012.
"""

import sys

if sys.platform == 'win32':
    import wmi

from oslo_config import cfg
from oslo_log import log as logging

from ceilometer.compute.virt import inspector
from ceilometer.i18n import _

CONF = cfg.CONF
LOG = logging.getLogger(__name__)


class HyperVException(inspector.InspectorException):
    pass


class UtilsV2(object):

    _VIRTUAL_SYSTEM_TYPE_REALIZED = 'Microsoft:Hyper-V:System:Realized'

    _PROC_SETTING = 'Msvm_ProcessorSettingData'
    _MEMORY_SETTING = "Msvm_MemorySettingData"
    _SYNTH_ETH_PORT = 'Msvm_SyntheticEthernetPortSettingData'
    _ETH_PORT_ALLOC = 'Msvm_EthernetPortAllocationSettingData'
    _PORT_ACL_SET_DATA = 'Msvm_EthernetSwitchPortAclSettingData'
    _STORAGE_ALLOC = 'Msvm_StorageAllocationSettingData'
    _VS_SETTING_DATA = 'Msvm_VirtualSystemSettingData'
    _METRICS_ME = 'Msvm_MetricForME'
    _BASE_METRICS_VALUE = 'Msvm_BaseMetricValue'

    _CPU_METRIC_NAME = 'Aggregated Average CPU Utilization'
    _MEMORY_METRIC_NAME = 'Aggregated Average Memory Utilization'
    _NET_IN_METRIC_NAME = 'Filtered Incoming Network Traffic'
    _NET_OUT_METRIC_NAME = 'Filtered Outgoing Network Traffic'
    # Disk metrics are supported from Hyper-V 2012 R2
    _DISK_RD_METRIC_NAME = 'Disk Data Read'
    _DISK_WR_METRIC_NAME = 'Disk Data Written'
    _DISK_LATENCY_METRIC_NAME = 'Average Disk Latency'
    _DISK_IOPS_METRIC_NAME = 'Average Normalized Disk Throughput'

    def __init__(self, host='.'):