Merge "Protect FM API shared data with thread locking"

This commit is contained in:
Zuul 2019-04-10 20:02:48 +00:00 committed by Gerrit Code Review
commit 8421d623d9
1 changed files with 132 additions and 112 deletions

View File

@ -14,6 +14,9 @@ import copy
from . import constants from . import constants
import six import six
import fm_core import fm_core
import threading
fm_api_lock = threading.Lock()
class ClientException(Exception): class ClientException(Exception):
@ -91,7 +94,8 @@ class FaultAPIsBase(object):
sep + data.severity + sep + self._check_val(data.reason_text) + sep + data.severity + sep + self._check_val(data.reason_text) +
sep + data.alarm_type + sep + data.probable_cause + sep + sep + data.alarm_type + sep + data.probable_cause + sep +
self._check_val(data.proposed_repair_action) + sep + self._check_val(data.proposed_repair_action) + sep +
str(data.service_affecting) + sep + str(data.suppression) + sep) str(data.service_affecting) + sep +
str(data.suppression) + sep)
@staticmethod @staticmethod
def _str_to_alarm(alarm_str): def _str_to_alarm(alarm_str):
@ -172,76 +176,86 @@ class FaultAPIsBase(object):
class FaultAPIs(FaultAPIsBase): class FaultAPIs(FaultAPIsBase):
def set_fault(self, data): def set_fault(self, data):
self._check_required_attributes(data) with fm_api_lock:
self._validate_attributes(data) self._check_required_attributes(data)
buff = self._alarm_to_str(data) self._validate_attributes(data)
try: buff = self._alarm_to_str(data)
return fm_core.set(buff) try:
except (RuntimeError, SystemError, TypeError): return fm_core.set(buff)
return None except (RuntimeError, SystemError, TypeError):
return None
def clear_fault(self, alarm_id, entity_instance_id): def clear_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP with fm_api_lock:
buff = (sep + self._check_val(alarm_id) + sep + sep = constants.FM_CLIENT_STR_SEP
self._check_val(entity_instance_id) + sep) buff = (sep + self._check_val(alarm_id) + sep +
try: self._check_val(entity_instance_id) + sep)
resp = fm_core.clear(buff) try:
# resp may be True/False/None after FaultAPIsV2 implementation. resp = fm_core.clear(buff)
# to keep FaultAPIs the same as before, return False for None case. # resp may be True/False/None after FaultAPIsV2
if resp is True: # implementation.
return True # To keep FaultAPIs the same as before,
else: # return False for None case.
if resp is True:
return True
else:
return False
except (RuntimeError, SystemError, TypeError):
return False return False
except (RuntimeError, SystemError, TypeError):
return False
def get_fault(self, alarm_id, entity_instance_id): def get_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP with fm_api_lock:
buff = (sep + self._check_val(alarm_id) + sep + sep = constants.FM_CLIENT_STR_SEP
self._check_val(entity_instance_id) + sep) buff = (sep + self._check_val(alarm_id) + sep +
try: self._check_val(entity_instance_id) + sep)
resp = fm_core.get(buff) try:
if resp: resp = fm_core.get(buff)
return self._str_to_alarm(resp) if resp:
except (RuntimeError, SystemError, TypeError): return self._str_to_alarm(resp)
pass except (RuntimeError, SystemError, TypeError):
return None pass
return None
def clear_all(self, entity_instance_id): def clear_all(self, entity_instance_id):
try: with fm_api_lock:
resp = fm_core.clear_all(entity_instance_id) try:
# resp may be True/False/None after FaultAPIsV2 implementation. resp = fm_core.clear_all(entity_instance_id)
# to keep FaultAPIs the same as before, return False for None case. # resp may be True/False/None after FaultAPIsV2
if resp is True: # implementation.
return True # To keep FaultAPIs the same as before,
else: # return False for None case.
if resp is True:
return True
else:
return False
except (RuntimeError, SystemError, TypeError):
return False return False
except (RuntimeError, SystemError, TypeError):
return False
def get_faults(self, entity_instance_id): def get_faults(self, entity_instance_id):
try: with fm_api_lock:
resp = fm_core.get_by_eid(entity_instance_id) try:
if resp: resp = fm_core.get_by_eid(entity_instance_id)
data = [] if resp:
for i in resp: data = []
data.append(self._str_to_alarm(i)) for i in resp:
return data data.append(self._str_to_alarm(i))
except (RuntimeError, SystemError, TypeError): return data
pass except (RuntimeError, SystemError, TypeError):
return None pass
return None
def get_faults_by_id(self, alarm_id): def get_faults_by_id(self, alarm_id):
try: with fm_api_lock:
resp = fm_core.get_by_aid(alarm_id) try:
if resp: resp = fm_core.get_by_aid(alarm_id)
data = [] if resp:
for i in resp: data = []
data.append(self._str_to_alarm(i)) for i in resp:
return data data.append(self._str_to_alarm(i))
except (RuntimeError, SystemError, TypeError): return data
pass except (RuntimeError, SystemError, TypeError):
return None pass
return None
class FaultAPIsV2(FaultAPIsBase): class FaultAPIsV2(FaultAPIsBase):
@ -251,10 +265,11 @@ class FaultAPIsV2(FaultAPIsBase):
# Exception: 1. Input Alarm format is not valid # Exception: 1. Input Alarm format is not valid
# 2. When there is operation failure # 2. When there is operation failure
def set_fault(self, data): def set_fault(self, data):
self._check_required_attributes(data) with fm_api_lock:
self._validate_attributes(data) self._check_required_attributes(data)
buff = self._alarm_to_str(data) self._validate_attributes(data)
uuid = fm_core.set(buff) buff = self._alarm_to_str(data)
uuid = fm_core.set(buff)
if uuid is None: if uuid is None:
raise APIException("Failed to execute set_fault.") raise APIException("Failed to execute set_fault.")
return uuid return uuid
@ -264,76 +279,81 @@ class FaultAPIsV2(FaultAPIsBase):
# Alarm doesn't exist: False # Alarm doesn't exist: False
# Exception: When there is operation failure # Exception: When there is operation failure
def clear_fault(self, alarm_id, entity_instance_id): def clear_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP with fm_api_lock:
buff = (sep + self._check_val(alarm_id) + sep + sep = constants.FM_CLIENT_STR_SEP
self._check_val(entity_instance_id) + sep) buff = (sep + self._check_val(alarm_id) + sep +
resp = fm_core.clear(buff) self._check_val(entity_instance_id) + sep)
if resp is False: resp = fm_core.clear(buff)
# There is operation failure if resp is False:
raise APIException("Failed to execute clear_fault.") # There is operation failure
elif resp is None: raise APIException("Failed to execute clear_fault.")
# alarm is not found elif resp is None:
return False # alarm is not found
else: return False
return True else:
return True
# Input: alarm_id, entity_instance_id # Input: alarm_id, entity_instance_id
# Return: Success: Alarm # Return: Success: Alarm
# Alarm doesn't exist: None # Alarm doesn't exist: None
# Exception: When there is operation failure # Exception: When there is operation failure
def get_fault(self, alarm_id, entity_instance_id): def get_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP with fm_api_lock:
buff = (sep + self._check_val(alarm_id) + sep + sep = constants.FM_CLIENT_STR_SEP
self._check_val(entity_instance_id) + sep) buff = (sep + self._check_val(alarm_id) + sep +
resp = fm_core.get(buff) self._check_val(entity_instance_id) + sep)
if resp is False: resp = fm_core.get(buff)
raise APIException("Failed to execute get_fault.") if resp is False:
else: raise APIException("Failed to execute get_fault.")
return self._str_to_alarm(resp) if resp else None else:
return self._str_to_alarm(resp) if resp else None
# Input: entity_instance_id # Input: entity_instance_id
# Return: Success: True # Return: Success: True
# Alarm doesn't exist: False # Alarm doesn't exist: False
# Exception: When there is operation failure # Exception: When there is operation failure
def clear_all(self, entity_instance_id): def clear_all(self, entity_instance_id):
resp = fm_core.clear_all(entity_instance_id) with fm_api_lock:
if resp is False: resp = fm_core.clear_all(entity_instance_id)
# There is operation failure if resp is False:
raise APIException("Failed to execute clear_all.") # There is operation failure
elif resp is None: raise APIException("Failed to execute clear_all.")
# alarm is not found elif resp is None:
return False # alarm is not found
else: return False
return True else:
return True
# Input: entity_instance_id # Input: entity_instance_id
# Return: Success: Alarm list # Return: Success: Alarm list
# Alarm doesn't exist: None # Alarm doesn't exist: None
# Exception: When there is operation failure # Exception: When there is operation failure
def get_faults(self, entity_instance_id): def get_faults(self, entity_instance_id):
resp = fm_core.get_by_eid(entity_instance_id) with fm_api_lock:
if resp is False: resp = fm_core.get_by_eid(entity_instance_id)
raise APIException("Failed to execute get_faults.") if resp is False:
elif resp: raise APIException("Failed to execute get_faults.")
data = [] elif resp:
for i in resp: data = []
data.append(self._str_to_alarm(i)) for i in resp:
return data data.append(self._str_to_alarm(i))
else: return data
return None else:
return None
# Input: alarm_id # Input: alarm_id
# Return: Success: Alarm list # Return: Success: Alarm list
# Alarm doesn't exist: None # Alarm doesn't exist: None
# Exception: When there is operation failure # Exception: When there is operation failure
def get_faults_by_id(self, alarm_id): def get_faults_by_id(self, alarm_id):
resp = fm_core.get_by_aid(alarm_id) with fm_api_lock:
if resp is False: resp = fm_core.get_by_aid(alarm_id)
raise APIException("Failed to execute get_faults_by_id.") if resp is False:
elif resp: raise APIException("Failed to execute get_faults_by_id.")
data = [] elif resp:
for i in resp: data = []
data.append(self._str_to_alarm(i)) for i in resp:
return data data.append(self._str_to_alarm(i))
else: return data
return None else:
return None