# Path: mom/HypervisorInterfaces/vdsmCommon.py
# def memoize(expiration):
#     def decorator(obj):
#         lock = threading.Lock()
#         cache = obj._cache = {}
#         timestamps = obj._timestamps = {}
# 
#         @functools.wraps(obj)
#         def memoizer(*args, **kwargs):
#             key = str(args) + str(kwargs)
#             now = time.time()
# 
#             # use absolute value of the time difference to avoid issues
#             # with time changing to the past
# 
#             with lock:
#                 if key not in cache or abs(now - timestamps[key]) > expiration:
#                     cache[key] = obj(*args, **kwargs)
#                     timestamps[key] = now
#                 return cache[key]
#         return memoizer
#     return decorator
# 
# Path: mom/HypervisorInterfaces/vdsmjsonrpcInterface.py
# class JsonRpcVdsmInterface(VdsmRpcBase):
#     """
#     vdsmInterface provides a wrapper for the VDSM API so that VDSM-
#     related error handling can be consolidated in one place.  An instance of
#     this class provides a single VDSM connection that can be shared by all
#     threads.
#     """
# 
#     def __init__(self):
#         super(JsonRpcVdsmInterface, self).__init__()
#         self._vdsm_api = self.checked_call(jsonrpcvdscli.connect)\
#                 .orRaise(ConnectionError, 'No connection to VDSM.')
# 
#         self.checked_call(self._vdsm_api.ping)
# 
#     def _check_status(self, response):
#         try:
#             if response['status']['code']:
#                 raise vdsmException(response, self._logger)
# 
#         # This does not look as RPC response, ignore this check
#         except (AttributeError, TypeError):
#             pass
# 
# 
#     @memoize(expiration=CACHE_EXPIRATION)
#     def getAllVmStats(self):
#         vms = {}
#         ret = self.checked_call(self._vdsm_api.getAllVmStats)
# 
#         # the possible missing key is handled
#         # by the Optional result type transparently
#         for vm in ret['result']:
#             vms[vm['vmId']] = vm
# 
#         for vm in ret['items']:
#             vms[vm['vmId']] = vm
# 
#         return vms
# 
#     def setVmBalloonTarget(self, uuid, target):
#         self.checked_call(self._vdsm_api.setBalloonTarget, uuid, target)
# 
#     def setVmCpuTune(self, uuid, quota, period):
#         self.checked_call(self._vdsm_api.setCpuTuneQuota, uuid, quota)
#         self.checked_call(self._vdsm_api.setCpuTunePeriod, uuid, period)
# 
#     def getVmIoTunePolicy(self, vmId):
#         result = self.checked_call(self._vdsm_api.getIoTunePolicy, vmId)
#         return result.get('items', []).orNone()
# 
#     def getVmIoTune(self, vmId):
#         result = self.checked_call(self._vdsm_api.getIoTune, vmId)
#         return result.get('items', []).orNone()
# 
#     def setVmIoTune(self, vmId, tunables):
#         self.checked_call(self._vdsm_api.setIoTune, vmId, tunables)
# 
#     def ksmTune(self, tuningParams):
#         self.checked_call(self._vdsm_api.setKsmTune, tuningParams)
# 
#     def checked_call(self, vdsm_method, *args, **kwargs):
#         try:
#             response = vdsm_method(*args, **kwargs)
#             self._check_status(response)
#             return Optional(response)
#         except socket.error as e:
#             self._logger.error("Cannot connect to VDSM! {0}".format(e))
#             return Optional.missing()
#         except vdsmException as e:
#             e.handle_exception()
#             return Optional.missing()
#         except jsonrpcvdscli.JsonRpcNoResponseError as e:
#             self._logger.error("No response from VDSM arrived! {0}".format(e))
#             return Optional.missing()
# 
# CACHE_EXPIRATION = 5


from .vdsmCommon import memoize
from mom.HypervisorInterfaces.vdsmjsonrpcInterface import JsonRpcVdsmInterface, \
    CACHE_EXPIRATION
# Memory Overcommitment Manager
# Copyright (C) 2016 Martin Sivak, Red Hat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA



class JsonRpcVdsmBulkInterface(JsonRpcVdsmInterface):
    """
    JsonRpcVdsmBulkInterface extends the JsonRpcVdsmInterface and
    overrides the getIoTune and getIoTunePolicy methods so that
    the new vdsm api can be utilized
    """
    def __init__(self):
        super(JsonRpcVdsmBulkInterface, self).__init__()


# Path: mom/HypervisorInterfaces/vdsmCommon.py
# def memoize(expiration):
#     def decorator(obj):
#         lock = threading.Lock()
#         cache = obj._cache = {}
#         timestamps = obj._timestamps = {}
# 
#         @functools.wraps(obj)
#         def memoizer(*args, **kwargs):
#             key = str(args) + str(kwargs)
#             now = time.time()
# 
#             # use absolute value of the time difference to avoid issues
#             # with time changing to the past
# 
#             with lock:
#                 if key not in cache or abs(now - timestamps[key]) > expiration:
#                     cache[key] = obj(*args, **kwargs)
#                     timestamps[key] = now
#                 return cache[key]
#         return memoizer
#     return decorator
# 
# Path: mom/HypervisorInterfaces/vdsmjsonrpcInterface.py
# class JsonRpcVdsmInterface(VdsmRpcBase):
#     """
#     vdsmInterface provides a wrapper for the VDSM API so that VDSM-
#     related error handling can be consolidated in one place.  An instance of
#     this class provides a single VDSM connection that can be shared by all
#     threads.
#     """
# 
#     def __init__(self):
#         super(JsonRpcVdsmInterface, self).__init__()
#         self._vdsm_api = self.checked_call(jsonrpcvdscli.connect)\
#                 .orRaise(ConnectionError, 'No connection to VDSM.')
# 
#         self.checked_call(self._vdsm_api.ping)
# 
#     def _check_status(self, response):
#         try:
#             if response['status']['code']:
#                 raise vdsmException(response, self._logger)
# 
#         # This does not look as RPC response, ignore this check
#         except (AttributeError, TypeError):
#             pass
# 
# 
#     @memoize(expiration=CACHE_EXPIRATION)
#     def getAllVmStats(self):
#         vms = {}
#         ret = self.checked_call(self._vdsm_api.getAllVmStats)
# 
#         # the possible missing key is handled
#         # by the Optional result type transparently
#         for vm in ret['result']:
#             vms[vm['vmId']] = vm
# 
#         for vm in ret['items']:
#             vms[vm['vmId']] = vm
# 
#         return vms
# 
#     def setVmBalloonTarget(self, uuid, target):
#         self.checked_call(self._vdsm_api.setBalloonTarget, uuid, target)
# 
#     def setVmCpuTune(self, uuid, quota, period):
#         self.checked_call(self._vdsm_api.setCpuTuneQuota, uuid, quota)
#         self.checked_call(self._vdsm_api.setCpuTunePeriod, uuid, period)
# 
#     def getVmIoTunePolicy(self, vmId):
#         result = self.checked_call(self._vdsm_api.getIoTunePolicy, vmId)
#         return result.get('items', []).orNone()
# 
#     def getVmIoTune(self, vmId):
#         result = self.checked_call(self._vdsm_api.getIoTune, vmId)
#         return result.get('items', []).orNone()
# 
#     def setVmIoTune(self, vmId, tunables):
#         self.checked_call(self._vdsm_api.setIoTune, vmId, tunables)
# 
#     def ksmTune(self, tuningParams):
#         self.checked_call(self._vdsm_api.setKsmTune, tuningParams)
# 
#     def checked_call(self, vdsm_method, *args, **kwargs):
#         try:
#             response = vdsm_method(*args, **kwargs)
#             self._check_status(response)
#             return Optional(response)
#         except socket.error as e:
#             self._logger.error("Cannot connect to VDSM! {0}".format(e))
#             return Optional.missing()
#         except vdsmException as e:
#             e.handle_exception()
#             return Optional.missing()
#         except jsonrpcvdscli.JsonRpcNoResponseError as e:
#             self._logger.error("No response from VDSM arrived! {0}".format(e))
#             return Optional.missing()
# 
# CACHE_EXPIRATION = 5


# Path: mom/HypervisorInterfaces/vdsmjsonrpcbulkInterface.py
from .vdsmCommon import memoize
from mom.HypervisorInterfaces.vdsmjsonrpcInterface import JsonRpcVdsmInterface, \
    CACHE_EXPIRATION

# Memory Overcommitment Manager
# Copyright (C) 2016 Martin Sivak, Red Hat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA



class JsonRpcVdsmBulkInterface(JsonRpcVdsmInterface):
    """
    JsonRpcVdsmBulkInterface extends the JsonRpcVdsmInterface and
    overrides the getIoTune and getIoTunePolicy methods so that
    the new vdsm api can be utilized
    """
    def __init__(self):
        super(JsonRpcVdsmBulkInterface, self).__init__()


    @memoize(expiration=CACHE_EXPIRATION)