2b3f9f3facadb1e961bfaa77fe6f6a78eaa9bf19
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.query;
23
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.stream.Collectors;
29 import org.eclipse.jdt.annotation.Nullable;
30 import org.onap.ccsdk.features.sdnr.wt.dataprovider.database.sqldb.database.SqlDBMapper;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.EntityInput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.SortOrder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Filter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.FilterKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Pagination;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.Sortorder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.data.provider.rev201110.entity.input.SortorderKey;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42 public class SelectQuery implements SqlQuery {
43
44     private static final Logger LOG = LoggerFactory.getLogger(SelectQuery.class);
45
46     private static final long DEFAULT_PAGESIZE = 20;
47     private static final long DEFAULT_PAGE = 1;
48     private final String tableName;
49     private final List<Filter> filters;
50     private final List<String> sortExpressions;
51     private long page;
52     private long pageSize;
53     private final List<String> fields;
54     private final List<String> groups;
55
56     public SelectQuery(String tableName) {
57         this(tableName, Arrays.asList("*"), null);
58     }
59
60     public SelectQuery(String tableName, List<String> fields, String controllerId) {
61         this.tableName = tableName;
62         this.fields = fields;
63         this.filters = new ArrayList<>();
64         this.sortExpressions = new ArrayList<>();
65         this.groups = new ArrayList<>();
66         this.page = DEFAULT_PAGE;
67         this.pageSize = DEFAULT_PAGESIZE;
68         if (controllerId != null) {
69             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
70         }
71     }
72
73     public SelectQuery(String tableName, String field, String controllerId) {
74         this(tableName, Arrays.asList(field), controllerId);
75     }
76
77     public SelectQuery(String tableName, EntityInput input) {
78         this(tableName, input, null);
79     }
80
81     public SelectQuery(String tableName, EntityInput input, String controllerId) {
82         this(tableName);
83         Map<FilterKey, Filter> filter = input != null ? input.getFilter() : null;
84         if (filter != null && filter.size() > 0) {
85             for (Filter f : filter.values()) {
86                 this.addFilter(f);
87             }
88         }
89         if (controllerId != null) {
90             this.addFilter(SqlDBMapper.ODLID_DBCOL, controllerId);
91         }
92
93         Map<SortorderKey, Sortorder> so = input != null ? input.getSortorder() : null;
94         if (so != null && !so.isEmpty()) {
95             for (Sortorder s : so.values()) {
96                 this.addSortOrder(s.getProperty(), s.getSortorder() == SortOrder.Ascending ? "ASC" : "DESC");
97             }
98         }
99         Pagination pagination = input != null ? input.getPagination() : null;
100         if (pagination != null) {
101             this.setPagination(pagination.getPage().longValue(), pagination.getSize().longValue());
102         } else {
103             this.setPagination(1, 30);
104         }
105
106     }
107
108     public void addFilter(String property, String filtervalue) {
109         this.addFilter(new FilterBuilder().setProperty(property).setFiltervalue(filtervalue).build());
110     }
111
112     private static Filter cleanFilter(Filter filter) {
113         if (filter.getFiltervalue() != null
114                 && (filter.getFiltervalues() == null || filter.getFiltervalues().isEmpty())) {
115             return "*".equals(filter.getFiltervalue()) ? null : filter;
116         } else {
117             List<String> list = new ArrayList<>(filter.getFiltervalues());
118             if (filter.getFiltervalue() != null && !filter.getFiltervalue().isEmpty()) {
119                 list.add(filter.getFiltervalue());
120             }
121             if (list.size() == 1 && "*".equals(list.get(0))) {
122                 return null;
123             } ;
124             return new FilterBuilder().setProperty(filter.getProperty()).setFiltervalue(filter.getFiltervalue())
125                     .setFiltervalues(
126                             filter.getFiltervalues().stream().filter(e -> !"*".equals(e)).collect(Collectors.toList()))
127                     .build();
128         }
129     }
130
131     public void addFilter(Filter filter) {
132         Filter tmp = cleanFilter(filter);
133         if (tmp == null) {
134             LOG.debug("ignore unneccessary filter for {}", filter);
135         } else {
136             this.filters.add(tmp);
137         }
138     }
139
140     public void addSortOrder(String col, String order) {
141         this.sortExpressions.add(String.format("`%s` %s", col, order));
142     }
143
144     public void setPagination(long page, long pageSize) {
145         this.page = page;
146         this.pageSize = pageSize;
147     }
148
149     public void setPagination(@Nullable Pagination pagination) {
150         long page = DEFAULT_PAGE;
151         long pageSize = DEFAULT_PAGESIZE;
152         if (pagination != null) {
153             if (pagination.getPage() != null) {
154                 page = pagination.getPage().longValue();
155             }
156             if (pagination.getSize() != null) {
157                 pageSize = pagination.getSize().longValue();
158             }
159         }
160         this.setPagination(page, pageSize);
161
162     }
163
164     @Override
165     public String toSql() {
166         StringBuilder sb = new StringBuilder();
167         if (this.fields.size() == 1 && this.fields.contains("*")) {
168             sb.append(String.format("SELECT * FROM `%s`", this.tableName));
169         } else {
170             sb.append(String.format("SELECT `%s` FROM `%s`", String.join("`,`", this.fields), this.tableName));
171         }
172         sb.append(SqlQuery.getWhereExpression(this.filters));
173         if (this.groups.size() > 0) {
174             sb.append(String.format(" GROUP BY `%s`", String.join("`,`", this.groups)));
175         }
176         if (this.sortExpressions.size() > 0) {
177             sb.append(" ORDER BY " + String.join(",", this.sortExpressions));
178         }
179         sb.append(String.format(" LIMIT %d,%d;", (this.page - 1) * this.pageSize, this.pageSize));
180         return sb.toString();
181     }
182
183     public long getPage() {
184         return this.page;
185     }
186
187     public long getPageSize() {
188         return this.pageSize;
189     }
190
191     public SelectQuery groupBy(String group) {
192         this.groups.add(group);
193         return this;
194     }
195
196 }