Add INF-320 support attribute-based filter
[pti/o2.git] / o2common / views / view.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
17 from o2common.helper import o2logging
18 logger = o2logging.get_logger(__name__)
19
20
21 def gen_filter(obj: ColumnElement, filter_str: str):
22     if filter_str == '':
23         return []
24     filter_without_space = filter_str.replace(" ", "")
25     items = filter_without_space.split(';')
26
27     filter_list = list()
28     for i in items:
29         if '(' in i:
30             i = i.replace("(", "")
31         if ')' in i:
32             i = i.replace(")", "")
33         filter_expr = i.split(',')
34         if len(filter_expr) < 3:
35             continue
36         filter_op = filter_expr[0]
37         filter_key = filter_expr[1]
38         filter_vals = filter_expr[2:]
39         filter_list.extend(toFilterArgs(
40             filter_op, obj, filter_key, filter_vals))
41     logger.info('Filter list length: %d' % len(filter_list))
42     return filter_list
43
44
45 def toFilterArgs(operation: str, obj: ColumnElement, key: str, values: list):
46     if not hasattr(obj, key):
47         logger.warning('Filter attrName %s not in Object %s.' %
48                        (key, str(obj)))
49         return []
50
51     ll = list()
52     if operation == 'eq':
53         for val in values:
54             if val.lower() == 'null':
55                 val = None
56             ll.append(getattr(obj, key) == val)
57     elif operation == 'neq':
58         for val in values:
59             if val.lower() == 'null':
60                 val = None
61             ll.append(getattr(obj, key) != val)
62     elif operation == 'gt':
63         for val in values:
64             ll.append(getattr(obj, key) > val)
65     elif operation == 'lt':
66         for val in values:
67             ll.append(getattr(obj, key) < val)
68     elif operation == 'gte':
69         for val in values:
70             ll.append(getattr(obj, key) >= val)
71     elif operation == 'lte':
72         for val in values:
73             ll.append(getattr(obj, key) <= val)
74     elif operation == 'in':
75         pass
76     elif operation == 'nin':
77         pass
78     elif operation == 'count':
79         pass
80     elif operation == 'ncount':
81         pass
82     else:
83         raise KeyError('Filter operation value not support.')
84
85     return ll