From 51b371f14f46196e0cfea60264361364e3cc9f57 Mon Sep 17 00:00:00 2001 From: "Zhang Rong(Jon)" Date: Mon, 21 Nov 2022 19:53:08 +0800 Subject: [PATCH] Fix INF-364 alarmEventRecord does not comply with spec Issue-ID: INF-364 Signed-off-by: Zhang Rong(Jon) Change-Id: Ie924b63b0cb7a16d91ed1a91fc6699da1f52432b --- o2common/domain/filter.py | 20 ++++++++++++++++++++ o2common/views/route.py | 39 ++++++++++++++------------------------- o2common/views/view.py | 4 +++- o2ims/views/alarm_dto.py | 10 ++++++---- 4 files changed, 43 insertions(+), 30 deletions(-) diff --git a/o2common/domain/filter.py b/o2common/domain/filter.py index de89cea..a84fdb8 100644 --- a/o2common/domain/filter.py +++ b/o2common/domain/filter.py @@ -15,6 +15,9 @@ from sqlalchemy.sql.elements import ColumnElement from sqlalchemy import or_ +from o2ims.domain.alarm_obj import AlarmEventRecord +from o2ims.domain.ocloud import Ocloud + from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -62,6 +65,7 @@ def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list): # format(operation)) # else: # raise KeyError('Filter operation {} not support'.format(operation)) + key = transfer_filter_attr_name_in_special(obj, key) ll = list() if operation == 'eq': @@ -101,3 +105,19 @@ def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list): val_list.append(getattr(obj, key).contains(val)) ll.append(~or_(*val_list)) return ll + + +def transfer_filter_attr_name_in_special(obj: ColumnElement, filter_key: str): + if obj == AlarmEventRecord: + if filter_key == 'resourceTypeID': + filter_key = 'resourceTypeId' + elif filter_key == 'resourceID': + filter_key = 'resourceId' + elif filter_key == 'alarmDefinitionID': + filter_key = 'alarmDefinitionId' + elif filter_key == 'probableCauseID': + filter_key = 'probableCauseId' + elif obj == Ocloud: + if filter_key == 'globalcloudId': + filter_key = 'globalCloudId' + return filter_key diff --git a/o2common/views/route.py b/o2common/views/route.py index c6fc56e..868ff70 100644 --- a/o2common/views/route.py +++ b/o2common/views/route.py @@ -129,50 +129,31 @@ class o2_marshal_with(marshal_with): def _gen_mask_from_selector(self, **kwargs) -> str: mask_val = '' if 'all_fields' in kwargs: - all_fields_without_space = kwargs['all_fields'].replace(" ", "") + all_fields_without_space = kwargs['all_fields'].strip() logger.debug('all_fields selector value is {}'.format( all_fields_without_space)) - # all_fields = all_fields_without_space.lower() - # if 'true' == all_fields: selector = self.__gen_selector_from_model_with_value( self.fields) mask_val = self.__gen_mask_from_selector(selector) elif 'fields' in kwargs and kwargs['fields'] != '': - fields_without_space = kwargs['fields'].replace(" ", "") - - # filters = fields_without_space.split(',') - - # mask_val_list = [] - # for f in filters: - # if '/' in f: - # a = self.__gen_mask_tree(f) - # mask_val_list.append(a) - # continue - # mask_val_list.append(f) - # mask_val = '{%s}' % ','.join(mask_val_list) + fields_without_space = kwargs['fields'].strip() selector = {} - self.__update_selector_value(selector, fields_without_space, True) self.__set_default_mask(selector) - mask_val = self.__gen_mask_from_selector(selector) elif 'exclude_fields' in kwargs and kwargs['exclude_fields'] != '': - exclude_fields_without_space = kwargs['exclude_fields'].replace( - " ", "") - + exclude_fields_without_space = kwargs['exclude_fields'].strip() selector = self.__gen_selector_from_model_with_value( self.fields) - self.__update_selector_value( selector, exclude_fields_without_space, False) self.__set_default_mask(selector) - mask_val = self.__gen_mask_from_selector(selector) + elif 'exclude_default' in kwargs and kwargs['exclude_default'] != '': - exclude_default_without_space = kwargs['exclude_default'].replace( - " ", "") + exclude_default_without_space = kwargs['exclude_default'].strip() exclude_default = exclude_default_without_space.lower() if 'true' == exclude_default: mask_val = '{}' @@ -212,6 +193,7 @@ class o2_marshal_with(marshal_with): val: bool): fields = filter.split(',') for f in fields: + f = f.strip() if '/' in f: self.__update_selector_tree_value(selector, f, val) continue @@ -243,5 +225,12 @@ class o2_marshal_with(marshal_with): return '{%s}' % ','.join(mask_li) def __set_default_mask(self, selector: dict, val: bool = True): - default_selector = str(getattr(self.fields, "__mask__"))[1:-1] + mask = getattr(self.fields, "__mask__") + if not mask: + selector_all = self.__gen_selector_from_model_with_value( + self.fields) + for s in selector_all: + selector[s] = val + return + default_selector = str(mask).split('()') self.__update_selector_value(selector, default_selector, val) diff --git a/o2common/views/view.py b/o2common/views/view.py index d390634..558384a 100644 --- a/o2common/views/view.py +++ b/o2common/views/view.py @@ -16,7 +16,8 @@ from sqlalchemy.sql.elements import ColumnElement from o2common.views.route_exception import BadRequestException -from o2common.domain.filter import gen_orm_filter +from o2common.domain.filter import gen_orm_filter, \ + transfer_filter_attr_name_in_special from o2common.helper import o2logging logger = o2logging.get_logger(__name__) @@ -87,6 +88,7 @@ def check_filter_attribute(obj: ColumnElement, filter_str: str): ) else: pass + filter_key = transfer_filter_attr_name_in_special(obj, filter_key) if not hasattr(obj, filter_key): raise BadRequestException( 'Filter attrName {} is invalid'.format(filter_key)) diff --git a/o2ims/views/alarm_dto.py b/o2ims/views/alarm_dto.py index 6697f20..781bfb0 100644 --- a/o2ims/views/alarm_dto.py +++ b/o2ims/views/alarm_dto.py @@ -29,19 +29,21 @@ class AlarmDTO: 'resourceTypeID': fields.String(attribute='resourceTypeId'), 'resourceId': fields.String, 'resourceID': fields.String(attribute='resourceId'), - 'alarmEventRecordID': - fields.String(attribute='alarmEventRecordId'), 'alarmDefinitionId': fields.String, 'alarmDefinitionID': fields.String(attribute='alarmDefinitionId'), 'probableCauseId': fields.String, 'probableCauseID': fields.String(attribute='probableCauseId'), 'alarmRaisedTime': fields.String, - 'perceivedSeverity': fields.String, 'alarmChangedTime': fields.String, 'alarmAcknowledgeTime': fields.String, 'alarmAcknowledged': fields.Boolean, + 'perceivedSeverity': fields.String, 'extensions': fields.Raw(attribute='extensions'), - } + }, + mask='{alarmEventRecordId,resourceTypeID,resourceID,' + + 'alarmDefinitionID,probableCauseID,' + + 'alarmRaisedTime,perceivedSeverity,alarmChangedTime,' + + 'alarmAcknowledgeTime,alarmAcknowledged,extensions}' ) -- 2.16.6