Fix INF-364 alarmEventRecord does not comply with spec
[pti/o2.git] / o2common / domain / filter.py
1 # Copyright (C) 2021-2022 Wind River Systems, Inc.
2 #
3 #  Licensed under the Apache License, Version 2.0 (the "License");
4 #  you may not use this file except in compliance with the License.
5 #  You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 #  Unless required by applicable law or agreed to in writing, software
10 #  distributed under the License is distributed on an "AS IS" BASIS,
11 #  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 #  See the License for the specific language governing permissions and
13 #  limitations under the License.
14
15 from sqlalchemy.sql.elements import ColumnElement
16 from sqlalchemy import or_
17
18 from o2ims.domain.alarm_obj import AlarmEventRecord
19 from o2ims.domain.ocloud import Ocloud
20
21 from o2common.helper import o2logging
22 logger = o2logging.get_logger(__name__)
23
24
25 def gen_orm_filter(obj: ColumnElement, filter_str: str):
26     if not filter_str:
27         return []
28     # filter_without_space = filter_str.replace(" ", "")
29     filter_without_space = filter_str.strip(' ()')
30     items = filter_without_space.split(';')
31
32     filter_list = list()
33     for i in items:
34         # if '(' in i:
35         #     i = i.replace("(", "")
36         # if ')' in i:
37         #     i = i.replace(")", "")
38         filter_expr = i.split(',')
39         if len(filter_expr) < 3:
40             continue
41         filter_op = filter_expr[0].strip()
42         filter_key = filter_expr[1].strip()
43         filter_vals = filter_expr[2:]
44         filter_list.extend(toFilterArgs(
45             filter_op, obj, filter_key, filter_vals))
46     logger.debug('Filter list length: %d' % len(filter_list))
47     return filter_list
48
49
50 def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list):
51     # if not hasattr(obj, key):
52     #     logger.warning('Filter attrName %s not in Object %s' %
53     #                    (key, str(obj)))
54     #     raise KeyError(
55     #         'Filter attrName {} not in the Object'.format(key))
56
57     # if operation in ['eq', 'neq', 'gt', 'lt', 'gte', 'lte']:
58     #     if len(values) != 1:
59     #         raise KeyError(
60     #             'Filter operation one {} is only support one value'.
61     #             format(operation))
62     # elif operation in ['in', 'nin', 'cont', 'ncont']:
63     #     if len(values) == 0:
64     #         raise KeyError('Filter operation {} value is needed'.
65     #                        format(operation))
66     # else:
67     #     raise KeyError('Filter operation {} not support'.format(operation))
68     key = transfer_filter_attr_name_in_special(obj, key)
69
70     ll = list()
71     if operation == 'eq':
72         val = values[0]
73         if val.lower() == 'null':
74             val = None
75         ll.append(getattr(obj, key) == val)
76     elif operation == 'neq':
77         val = values[0]
78         if val.lower() == 'null':
79             val = None
80         ll.append(getattr(obj, key) != val)
81     elif operation == 'gt':
82         val = values[0]
83         ll.append(getattr(obj, key) > val)
84     elif operation == 'lt':
85         val = values[0]
86         ll.append(getattr(obj, key) < val)
87     elif operation == 'gte':
88         val = values[0]
89         ll.append(getattr(obj, key) >= val)
90     elif operation == 'lte':
91         val = values[0]
92         ll.append(getattr(obj, key) <= val)
93     elif operation == 'in':
94         ll.append(getattr(obj, key).in_(values))
95     elif operation == 'nin':
96         ll.append(~getattr(obj, key).in_(values))
97     elif operation == 'cont':
98         val_list = list()
99         for val in values:
100             val_list.append(getattr(obj, key).contains(val))
101         ll.append(or_(*val_list))
102     elif operation == 'ncont':
103         val_list = list()
104         for val in values:
105             val_list.append(getattr(obj, key).contains(val))
106         ll.append(~or_(*val_list))
107     return ll
108
109
110 def transfer_filter_attr_name_in_special(obj: ColumnElement, filter_key: str):
111     if obj == AlarmEventRecord:
112         if filter_key == 'resourceTypeID':
113             filter_key = 'resourceTypeId'
114         elif filter_key == 'resourceID':
115             filter_key = 'resourceId'
116         elif filter_key == 'alarmDefinitionID':
117             filter_key = 'alarmDefinitionId'
118         elif filter_key == 'probableCauseID':
119             filter_key = 'probableCauseId'
120     elif obj == Ocloud:
121         if filter_key == 'globalcloudId':
122             filter_key = 'globalCloudId'
123     return filter_key