# 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 logging
from .HypervisorInterface import \
    HypervisorInterface, HypervisorInterfaceError
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA




class VdsmRpcBase(HypervisorInterface):
    def __init__(self):
        self._logger = logging.getLogger('mom.VdsmRpcBase')

    def getVmList(self):
        vmIds = []
        vm_list = self.getAllVmStats().values()
        for vm in vm_list:
            if vm['status'] == 'Up':
                vmIds.append(vm['vmId'])

        self._logger.debug('VM List: %s', vmIds)
        return vmIds

    def getVmMemoryStats(self, uuid):
        vm = self._getVmStats(uuid)

        usage = int(vm['memUsage'])
        if usage == 0:
            msg = "The ovirt-guest-agent is not active"
            raise HypervisorInterfaceError(msg)
        stats = vm['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/vdsmRpcBase.py
import logging
from .HypervisorInterface import \
    HypervisorInterface, HypervisorInterfaceError

# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA




class VdsmRpcBase(HypervisorInterface):
    def __init__(self):
        self._logger = logging.getLogger('mom.VdsmRpcBase')

    def getVmList(self):
        vmIds = []
        vm_list = self.getAllVmStats().values()
        for vm in vm_list:
            if vm['status'] == 'Up':
                vmIds.append(vm['vmId'])

        self._logger.debug('VM List: %s', vmIds)
        return vmIds

    def getVmMemoryStats(self, uuid):
        vm = self._getVmStats(uuid)

        usage = int(vm['memUsage'])
        if usage == 0:
            msg = "The ovirt-guest-agent is not active"
            raise HypervisorInterfaceError(msg)
        stats = vm['memoryStats']
        if not stats:
            msg = "Detailed guest memory stats are not available, " \
                  "please upgrade guest agent"

            raise HypervisorInterfaceError(msg)