Fix INF-378 inventory subscription filter upgrade
[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     logger.debug(filter_str)
27     if not filter_str:
28         return []
29     filter_without_space = filter_str.strip()
30     items = filter_without_space.split(';')
31
32     filter_list = list()
33     for i in items:
34         item = i.strip(' ()')
35         filter_expr = item.split(',')
36         if len(filter_expr) < 3:
37             continue
38         filter_op = filter_expr[0].strip()
39         filter_key = filter_expr[1].strip()
40         if filter_key == 'objectType':
41             continue
42         filter_vals = filter_expr[2:]
43         filter_list.extend(toFilterArgs(
44             filter_op, obj, filter_key, filter_vals))
45     logger.debug('Filter list length: %d' % len(filter_list))
46     return filter_list
47
48
49 def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list):
50     # if not hasattr(obj, key):
51     #     logger.warning('Filter attrName %s not in Object %s' %
52     #                    (key, str(obj)))
53     #     raise KeyError(
54     #         'Filter attrName {} not in the Object'.format(key))
55
56     # if operation in ['eq', 'neq', 'gt', 'lt', 'gte', 'lte']:
57     #     if len(values) != 1:
58     #         raise KeyError(
59     #             'Filter operation one {} is only support one value'.
60     #             format(operation))
61     # elif operation in ['in', 'nin', 'cont', 'ncont']:
62     #     if len(values) == 0:
63     #         raise KeyError('Filter operation {} value is needed'.
64     #                        format(operation))
65     # else:
66     #     raise KeyError('Filter operation {} not support'.format(operation))
67     key = transfer_filter_attr_name_in_special(obj, key)
68
69     ll = list()
70     if operation == 'eq':
71         val = values[0]
72         if val.lower() == 'null':
73             val = None
74         ll.append(getattr(obj, key) == val)
75     elif operation == 'neq':
76         val = values[0]
77         if val.lower() == 'null':
78             val = None
79         ll.append(getattr(obj, key) != val)
80     elif operation == 'gt':
81         val = values[0]
82         ll.append(getattr(obj, key) > val)
83     elif operation == 'lt':
84         val = values[0]
85         ll.append(getattr(obj, key) < val)
86     elif operation == 'gte':
87         val = values[0]
88         ll.append(getattr(obj, key) >= val)
89     elif operation == 'lte':
90         val = values[0]
91         ll.append(getattr(obj, key) <= val)
92     elif operation == 'in':
93         ll.append(getattr(obj, key).in_(values))
94     elif operation == 'nin':
95         ll.append(~getattr(obj, key).in_(values))
96     elif operation == 'cont':
97         val_list = list()
98         for val in values:
99             val_list.append(getattr(obj, key).contains(val))
100         ll.append(or_(*val_list))
101     elif operation == 'ncont':
102         val_list = list()
103         for val in values:
104             val_list.append(getattr(obj, key).contains(val))
105         ll.append(~or_(*val_list))
106     return ll
107
108
109 def transfer_filter_attr_name_in_special(obj: ColumnElement, filter_key: str):
110     if obj == AlarmEventRecord:
111         if filter_key == 'resourceTypeID':
112             filter_key = 'resourceTypeId'
113         elif filter_key == 'resourceID':
114             filter_key = 'resourceId'
115         elif filter_key == 'alarmDefinitionID':
116             filter_key = 'alarmDefinitionId'
117         elif filter_key == 'probableCauseID':
118             filter_key = 'probableCauseId'
119     elif obj == Ocloud:
120         if filter_key == 'globalcloudId':
121             filter_key = 'globalCloudId'
122     return filter_key