Implement FaultAPIsV2 to distinguish fail and no data case

Current FaultAPIs (get_fault/get_faults/get_faults_by_id) cannot
distinguish error and no data case. None will be returned for both
case. To separate these two cases, and implement the code more as
pythonic way, FaultAPIsV2 is added, which will raise exception for
error case.
In order to separate the cases, the C functions called by API are
modified to return FALSE for error, and return None for no data.

Test:
Pass basic deploy test, and "fm alarm-*" cmd work as before.
Use fm_api_test.py/fm_api_v2_test.py to manual trigger API call,
confirm alarm could be created/get/deleted, and value is the same
as "fm alarm-list".
Modify fmManager to simulate error case, confirm exception is
generated for FaultAPIsV2, and behavior is not changed for
FaultAPIs.

Story: 2004859
Task: 29097

Change-Id: Ic1c0a15eb8dfec6c368099b096d6a158da0d3c77
Signed-off-by: Shuicheng Lin <shuicheng.lin@intel.com>
This commit is contained in:
Shuicheng Lin 2019-02-19 19:23:19 +08:00
parent e5debd54ab
commit cca4dc0c54
5 changed files with 279 additions and 88 deletions

View File

@ -2,4 +2,4 @@ SRC_DIR="."
COPY_LIST="$PKG_BASE/LICENSE"
EXCLUDE_FILES_FROM_TAR="centos"
TIS_PATCH_VER=13
TIS_PATCH_VER=14

View File

@ -20,6 +20,10 @@ class ClientException(Exception):
pass
class APIException(Exception):
pass
# Fields explanation:
#
# alarm_id: a text string of the alarm identifier
@ -70,66 +74,7 @@ class Fault(object):
return value
class FaultAPIs(object):
def set_fault(self, data):
self._check_required_attributes(data)
self._validate_attributes(data)
buff = self._alarm_to_str(data)
try:
return fm_core.set(buff)
except (RuntimeError, SystemError, TypeError):
return None
def clear_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
try:
return fm_core.clear(buff)
except (RuntimeError, SystemError, TypeError):
return False
def get_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
try:
resp = fm_core.get(buff)
return self._str_to_alarm(resp) if resp else None
except (RuntimeError, SystemError, TypeError):
return None
def clear_all(self, entity_instance_id):
try:
return fm_core.clear_all(entity_instance_id)
except (RuntimeError, SystemError, TypeError):
return False
def get_faults(self, entity_instance_id):
try:
resp = fm_core.get_by_eid(entity_instance_id)
if resp is not None:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
except (RuntimeError, SystemError, TypeError):
pass
return None
def get_faults_by_id(self, alarm_id):
try:
resp = fm_core.get_by_aid(alarm_id)
if resp is not None:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
except (RuntimeError, SystemError, TypeError):
pass
return None
class FaultAPIsBase(object):
@staticmethod
def _check_val(data):
if data is None:
@ -222,3 +167,125 @@ class FaultAPIs(object):
if given < threshold:
return True
return False
class FaultAPIs(FaultAPIsBase):
def set_fault(self, data):
self._check_required_attributes(data)
self._validate_attributes(data)
buff = self._alarm_to_str(data)
try:
return fm_core.set(buff)
except (RuntimeError, SystemError, TypeError):
return None
def clear_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
try:
return fm_core.clear(buff)
except (RuntimeError, SystemError, TypeError):
return False
def get_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
try:
resp = fm_core.get(buff)
if resp:
return self._str_to_alarm(resp)
except (RuntimeError, SystemError, TypeError):
pass
return None
def clear_all(self, entity_instance_id):
try:
return fm_core.clear_all(entity_instance_id)
except (RuntimeError, SystemError, TypeError):
return False
def get_faults(self, entity_instance_id):
try:
resp = fm_core.get_by_eid(entity_instance_id)
if resp:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
except (RuntimeError, SystemError, TypeError):
pass
return None
def get_faults_by_id(self, alarm_id):
try:
resp = fm_core.get_by_aid(alarm_id)
if resp:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
except (RuntimeError, SystemError, TypeError):
pass
return None
class FaultAPIsV2(FaultAPIsBase):
def set_fault(self, data):
self._check_required_attributes(data)
self._validate_attributes(data)
buff = self._alarm_to_str(data)
uuid = fm_core.set(buff)
if uuid is None:
raise APIException("Failed to execute set_fault.")
return uuid
def clear_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
resp = fm_core.clear(buff)
if resp is False:
raise APIException("Failed to execute clear_fault.")
def get_fault(self, alarm_id, entity_instance_id):
sep = constants.FM_CLIENT_STR_SEP
buff = (sep + self._check_val(alarm_id) + sep +
self._check_val(entity_instance_id) + sep)
resp = fm_core.get(buff)
if resp is False:
raise APIException("Failed to execute get_fault.")
else:
return self._str_to_alarm(resp) if resp else None
def clear_all(self, entity_instance_id):
resp = fm_core.clear_all(entity_instance_id)
if resp is False:
raise APIException("Failed to execute clear_all.")
def get_faults(self, entity_instance_id):
resp = fm_core.get_by_eid(entity_instance_id)
if resp is False:
raise APIException("Failed to execute get_faults.")
elif resp:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
else:
return None
def get_faults_by_id(self, alarm_id):
resp = fm_core.get_by_aid(alarm_id)
if resp is False:
raise APIException("Failed to execute get_faults_by_id.")
elif resp:
data = []
for i in resp:
data.append(self._str_to_alarm(i))
return data
else:
return None

View File

@ -17,9 +17,11 @@
# under the License.
import sys
from fm_api import Fault, FaultAPIs
from fm_api import fm_api
from fm_api import constants
ser = fm_api.FaultAPIs()
def print_alarm(alarm):
alarm_str = "alarm_id: " + alarm.alarm_id + ", "
@ -35,36 +37,32 @@ def print_alarm(alarm):
def create():
ser = FaultAPIs()
fault = Fault(alarm_id=constants.FM_ALARM_ID_VM_RESCUED,
alarm_state=constants.FM_ALARM_STATE_SET,
entity_type_id=constants.FM_ENTITY_TYPE_INSTANCE,
entity_instance_id=constants.FM_ENTITY_TYPE_INSTANCE + '=' + 'a4e4cdb7-2ee6-4818-84c8-5310fcd67b5d',
severity=constants.FM_ALARM_SEVERITY_CRITICAL,
reason_text="Unknown",
alarm_type=constants.FM_ALARM_TYPE_5,
probable_cause=constants.ALARM_PROBABLE_CAUSE_8,
proposed_repair_action=None,
service_affecting=False,
suppression=False)
fault = fm_api.Fault(alarm_id=constants.FM_ALARM_ID_VM_FAILED,
alarm_state=constants.FM_ALARM_STATE_SET,
entity_type_id=constants.FM_ENTITY_TYPE_INSTANCE,
entity_instance_id=constants.FM_ENTITY_TYPE_INSTANCE + '=' + 'a4e4cdb7-2ee6-4818-84c8-5310fcd67b5d',
severity=constants.FM_ALARM_SEVERITY_CRITICAL,
reason_text="Unknown",
alarm_type=constants.FM_ALARM_TYPE_5,
probable_cause=constants.ALARM_PROBABLE_CAUSE_8,
proposed_repair_action=None,
service_affecting=False,
suppression=False)
uuid = ser.set_fault(fault)
print(uuid)
def delete(alarm_id, instance_id):
ser = FaultAPIs()
ret = ser.clear_fault(alarm_id, instance_id)
print("Delete fault return %s" % str(ret))
def del_all(instance_id):
ser = FaultAPIs()
ret = ser.clear_all(instance_id)
print("Delete faults return: %s" % str(ret))
def get(alarm_id, instance_id):
ser = FaultAPIs()
a = ser.get_fault(alarm_id, instance_id)
if a is not None:
print_alarm(a)
@ -73,7 +71,6 @@ def get(alarm_id, instance_id):
def get_all(instance_id):
ser = FaultAPIs()
ll = ser.get_faults(instance_id)
if ll is not None:
print("Total alarm returned: %d\n"
@ -85,7 +82,6 @@ def get_all(instance_id):
def get_list(alarm_id):
ser = FaultAPIs()
ll = ser.get_faults_by_id(alarm_id)
if ll is not None:
print("Total alarm returned: %d\n"

125
fm-api/fm_api_v2_test.py Normal file
View File

@ -0,0 +1,125 @@
# -*- encoding: utf-8 -*-
#
# Copyright (c) 2014 Wind River Systems, Inc.
#
# Author:
#
# 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.
import sys
from fm_api import fm_api
from fm_api import constants
ser = fm_api.FaultAPIsV2()
def print_alarm(alarm):
alarm_str = "alarm_id: " + alarm.alarm_id + ", "
alarm_str += "uuid: " + alarm.uuid + ", "
alarm_str += "alarm_type: " + alarm.alarm_type + "\n"
alarm_str += "state: " + alarm.alarm_state + ", "
alarm_str += "severity: " + alarm.severity + ", "
alarm_str += "entity_type_id: " + alarm.entity_type_id + ", "
alarm_str += "timestamp: " + alarm.timestamp + "\n"
alarm_str += "entity_instance_id: " + alarm.entity_instance_id + ", "
alarm_str += "probable cause:" + alarm.probable_cause + "\n"
print(alarm_str)
def create():
fault = fm_api.Fault(alarm_id=constants.FM_ALARM_ID_VM_FAILED,
alarm_state=constants.FM_ALARM_STATE_SET,
entity_type_id=constants.FM_ENTITY_TYPE_INSTANCE,
entity_instance_id=constants.FM_ENTITY_TYPE_INSTANCE + '=' + 'a4e4cdb7-2ee6-4818-84c8-5310fcd67b5d',
severity=constants.FM_ALARM_SEVERITY_CRITICAL,
reason_text="Unknown",
alarm_type=constants.FM_ALARM_TYPE_5,
probable_cause=constants.ALARM_PROBABLE_CAUSE_8,
proposed_repair_action=None,
service_affecting=False,
suppression=False)
try:
uuid = ser.set_fault(fault)
print(uuid)
except Exception:
print("Fail to create fault")
def delete(alarm_id, instance_id):
try:
ser.clear_fault(alarm_id, instance_id)
print("Delete fault success")
except Exception:
print("Fail to delete fault")
def del_all(instance_id):
try:
ser.clear_all(instance_id)
print("Delete faults success")
except Exception:
print("Fail to delete faults")
def get(alarm_id, instance_id):
try:
a = ser.get_fault(alarm_id, instance_id)
if a is not None:
print_alarm(a)
else:
print("Alarm not found")
except Exception:
print("Fail to get alarm")
def get_all(instance_id):
try:
ll = ser.get_faults(instance_id)
if ll is not None:
print("Total alarm returned: %d\n"
% len(ll))
for i in ll:
print_alarm(i)
else:
print("No alarm returned")
except Exception:
print("Fail to get alarm")
def get_list(alarm_id):
try:
ll = ser.get_faults_by_id(alarm_id)
if ll is not None:
print("Total alarm returned: %d\n"
% len(ll))
for i in ll:
print_alarm(i)
else:
print("No alarm returned")
except Exception:
print("Fail to get alarm")
if __name__ == "__main__":
if sys.argv[1] == "create":
sys.exit(create())
elif sys.argv[1] == "del":
sys.exit(delete(sys.argv[2], sys.argv[3]))
elif sys.argv[1] == "get":
sys.exit(get(sys.argv[2], sys.argv[3]))
elif sys.argv[1] == "get_all":
sys.exit(get_all(sys.argv[2]))
elif sys.argv[1] == "del_all":
sys.exit(del_all(sys.argv[2]))
elif sys.argv[1] == "get_list":
sys.exit(get_list(sys.argv[2]))

View File

@ -108,13 +108,13 @@ static PyObject * _fm_get(PyObject * self, PyObject *args) {
if (!PyArg_ParseTuple(args, "s", &filter)) {
ERROR_LOG("Failed to parse args");
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
filter_str.assign(filter);
if (!fm_alarm_filter_from_string(filter_str, &af)) {
ERROR_LOG("Invalid alarm filter: (%s)", filter_str.c_str());
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
rc = fm_get_fault(&af,&ad);
@ -126,13 +126,14 @@ static PyObject * _fm_get(PyObject * self, PyObject *args) {
if (rc == FM_ERR_ENTITY_NOT_FOUND) {
DEBUG_LOG("Alarm id (%s), Entity id:(%s) not found",
af.alarm_id, af.entity_instance_id);
Py_RETURN_NONE;
} else if (rc == FM_ERR_NOCONNECT) {
WARNING_LOG("Failed to connect to FM manager");
} else {
ERROR_LOG("Failed to get alarm by filter: (%s) (%s), error code: (%d)",
af.alarm_id, af.entity_instance_id, rc);
}
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
@ -145,7 +146,7 @@ static PyObject * _fm_get_by_aid(PyObject * self, PyObject *args, PyObject* kwar
memset(alm_id, 0 , sizeof(alm_id));
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", keywords, &aid, &max)) {
ERROR_LOG("Failed to parse args");
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
strncpy(alm_id, aid, sizeof(alm_id)-1);
@ -154,7 +155,7 @@ static PyObject * _fm_get_by_aid(PyObject * self, PyObject *args, PyObject* kwar
lst.resize(max);
} catch(...) {
ERROR_LOG("Failed to allocate memory");
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
unsigned int max_alarms_to_get = max;
EFmErrorT rc = fm_get_faults_by_id(&alm_id, &(lst[0]), &max_alarms_to_get);
@ -176,12 +177,13 @@ static PyObject * _fm_get_by_aid(PyObject * self, PyObject *args, PyObject* kwar
if (rc == FM_ERR_ENTITY_NOT_FOUND) {
DEBUG_LOG("No alarm found for alarm id (%s)", alm_id);
Py_RETURN_NONE;
} else if (rc == FM_ERR_NOCONNECT) {
WARNING_LOG("Failed to connect to FM manager");
} else {
ERROR_LOG("Failed to get alarm list for alarm id (%s), error code: (%d)", alm_id, rc);
}
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwargs) {
@ -194,7 +196,7 @@ static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwar
memset(inst_id, 0 , sizeof(inst_id));
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|i", keywords, &eid, &max)) {
ERROR_LOG("Failed to parse args");
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
strncpy(inst_id, eid ,sizeof(inst_id)-1);
@ -202,7 +204,7 @@ static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwar
lst.resize(max);
} catch(...) {
ERROR_LOG("Failed to allocate memory");
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
unsigned int max_alarms_to_get = max;
EFmErrorT rc = fm_get_faults(&inst_id, &(lst[0]), &max_alarms_to_get);
@ -224,12 +226,13 @@ static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwar
if (rc == FM_ERR_ENTITY_NOT_FOUND) {
DEBUG_LOG("No alarm found for entity id (%s)", inst_id);
Py_RETURN_NONE;
} else if (rc == FM_ERR_NOCONNECT) {
WARNING_LOG("Failed to connect to FM manager");
} else {
ERROR_LOG("Failed to get alarm list for entity id (%s), error code: (%d)", inst_id, rc);
}
Py_RETURN_NONE;
Py_RETURN_FALSE;
}
static PyObject * _fm_clear(PyObject * self, PyObject *args) {