# Path: mom/HypervisorInterfaces/HypervisorInterface.py
# class HypervisorInterface(object):
#     """
#     HypervisorInterface is an abstract class which defines all interfaces
#     used by MOM to get guest memory statistics and control guest memory
#     ballooning. Its sub classes libvirt and vdsm need implement all these
#     interfaces by calling their respective API.
#     """
#     def getVmList(self):
#         """
#         This method returns a list, which is composed of the active guests'
#         identifiers.
#         """
#         pass
# 
#     def getVmInfo(self, uuid):
#         """
#         This method returns basic information of a given guest, including
#         name, uuid and pid.
#         """
#         pass
# 
#     def startVmMemoryStats(self, uuid):
#         """
#         This method activates the memory statistics of a given guest.
#         """
#         pass
# 
#     def getVmMemoryStats(self, uuid):
#         """
#         This method returns the memory statistics of a given guest. The stat
#         fields are decided by the real hypervisor interface.
#         """
#         pass
# 
#     def getVmBalloonInfo(self, uuid):
#         """
#         This method returns the balloon info a given guest, which includes two
#         fields:
#             balloon_max - The maximum amount of memory the guest may use
#             balloon_cur - The current memory limit (set by ballooning)
#         """
#         pass
# 
#     def setVmBalloonTarget(self, uuid, target):
#         """
#         This method sets the balloon target of a given guest. It's used by the
#         controller Balloon to inflate or deflate the balloon according to this
#         guest's memory usage.
#         """
#         pass
# 
#     def ksmTune(self, tuningParams):
#         """
#         This method is used to set KSM tuning parameters by the controller KSM.
#         """
#         pass
# 
# class HypervisorInterfaceError(Exception):
#     pass


import sys
import API
import supervdsm
import logging
import traceback
from mom.HypervisorInterfaces.HypervisorInterface import HypervisorInterface, \
    HypervisorInterfaceError

    def getVmList(self):
        vmIds = []
        try:
            response = self.vdsm_api.getVMList()
            self._check_status(response)
            vm_list = response['vmList']
            for vm in vm_list:
                if self._vmIsRunning(vm):
                    vmIds.append(vm['vmId'])
            self.logger.debug('VM List: %s', vmIds)
            return vmIds
        except vdsmException as e:
            e.handle_exception()
            return None

    def getVmMemoryStats(self, uuid):
        ret = {}
        try:
            vm = API.VM(uuid)
            response = vm.getStats()
            self._check_status(response)
            usage = int(response['statsList'][0]['memUsage'])
            if usage == 0:
                msg = "VM %s - The ovirt-guest-agent is not active" % uuid
                raise HypervisorInterfaceError(msg)
            stats = response['statsList'][0]['memoryStats']
            if not stats:
                msg = "Detailed guest memory stats are not available, " \
                        "please upgrade guest agent"

# Path: mom/HypervisorInterfaces/HypervisorInterface.py
# class HypervisorInterface(object):
#     """
#     HypervisorInterface is an abstract class which defines all interfaces
#     used by MOM to get guest memory statistics and control guest memory
#     ballooning. Its sub classes libvirt and vdsm need implement all these
#     interfaces by calling their respective API.
#     """
#     def getVmList(self):
#         """
#         This method returns a list, which is composed of the active guests'
#         identifiers.
#         """
#         pass
# 
#     def getVmInfo(self, uuid):
#         """
#         This method returns basic information of a given guest, including
#         name, uuid and pid.
#         """
#         pass
# 
#     def startVmMemoryStats(self, uuid):
#         """
#         This method activates the memory statistics of a given guest.
#         """
#         pass
# 
#     def getVmMemoryStats(self, uuid):
#         """
#         This method returns the memory statistics of a given guest. The stat
#         fields are decided by the real hypervisor interface.
#         """
#         pass
# 
#     def getVmBalloonInfo(self, uuid):
#         """
#         This method returns the balloon info a given guest, which includes two
#         fields:
#             balloon_max - The maximum amount of memory the guest may use
#             balloon_cur - The current memory limit (set by ballooning)
#         """
#         pass
# 
#     def setVmBalloonTarget(self, uuid, target):
#         """
#         This method sets the balloon target of a given guest. It's used by the
#         controller Balloon to inflate or deflate the balloon according to this
#         guest's memory usage.
#         """
#         pass
# 
#     def ksmTune(self, tuningParams):
#         """
#         This method is used to set KSM tuning parameters by the controller KSM.
#         """
#         pass
# 
# class HypervisorInterfaceError(Exception):
#     pass


# Path: mom/HypervisorInterfaces/vdsmInterface.py
import sys
import API
import supervdsm
import logging
import traceback
from mom.HypervisorInterfaces.HypervisorInterface import HypervisorInterface, \
    HypervisorInterfaceError


    def getVmList(self):
        vmIds = []
        try:
            response = self.vdsm_api.getVMList()
            self._check_status(response)
            vm_list = response['vmList']
            for vm in vm_list:
                if self._vmIsRunning(vm):
                    vmIds.append(vm['vmId'])
            self.logger.debug('VM List: %s', vmIds)
            return vmIds
        except vdsmException as e:
            e.handle_exception()
            return None

    def getVmMemoryStats(self, uuid):
        ret = {}
        try:
            vm = API.VM(uuid)
            response = vm.getStats()
            self._check_status(response)
            usage = int(response['statsList'][0]['memUsage'])
            if usage == 0:
                msg = "VM %s - The ovirt-guest-agent is not active" % uuid
                raise HypervisorInterfaceError(msg)
            stats = response['statsList'][0]['memoryStats']
            if not stats:
                msg = "Detailed guest memory stats are not available, " \
                        "please upgrade guest agent"

                raise HypervisorInterfaceError(msg)