Merge "Fix and refactor query across anchors (CPS-1664 #3)"
[cps.git] / cps-ri / src / main / java / org / onap / cps / spi / repository / FragmentQueryBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2023 TechMahindra Ltd.
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.spi.repository;
23
24 import java.util.HashMap;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Queue;
29 import javax.persistence.EntityManager;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.Query;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.cpspath.parser.CpsPathPrefixType;
35 import org.onap.cps.cpspath.parser.CpsPathQuery;
36 import org.onap.cps.spi.entities.AnchorEntity;
37 import org.onap.cps.spi.entities.DataspaceEntity;
38 import org.onap.cps.spi.entities.FragmentEntity;
39 import org.onap.cps.utils.JsonObjectMapper;
40 import org.springframework.stereotype.Component;
41
42 @RequiredArgsConstructor
43 @Slf4j
44 @Component
45 public class FragmentQueryBuilder {
46     private static final String REGEX_ABSOLUTE_PATH_PREFIX = "^";
47     private static final String REGEX_DESCENDANT_PATH_PREFIX = "^.*\\/";
48     private static final String REGEX_OPTIONAL_LIST_INDEX_POSTFIX = "(\\[@(?!.*\\[).*?])?$";
49     private static final String REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS = "(\\[@.*?])?(\\/.*)?$";
50     private static final AnchorEntity ACROSS_ALL_ANCHORS = null;
51
52     @PersistenceContext
53     private EntityManager entityManager;
54
55     private final JsonObjectMapper jsonObjectMapper;
56
57     /**
58      * Create a sql query to retrieve by anchor(id) and cps path.
59      *
60      * @param anchorEntity the anchor
61      * @param cpsPathQuery the cps path query to be transformed into a sql query
62      * @return a executable query object
63      */
64     public Query getQueryForAnchorAndCpsPath(final AnchorEntity anchorEntity, final CpsPathQuery cpsPathQuery) {
65         return getQueryForDataspaceOrAnchorAndCpsPath(anchorEntity.getDataspace(), anchorEntity, cpsPathQuery);
66     }
67
68     /**
69      * Create a sql query to retrieve by cps path.
70      *
71      * @param dataspaceEntity the dataspace
72      * @param cpsPathQuery the cps path query to be transformed into a sql query
73      * @return a executable query object
74      */
75     public Query getQueryForDataspaceAndCpsPath(final DataspaceEntity dataspaceEntity,
76                                                 final CpsPathQuery cpsPathQuery) {
77         return getQueryForDataspaceOrAnchorAndCpsPath(dataspaceEntity, ACROSS_ALL_ANCHORS, cpsPathQuery);
78     }
79
80     /**
81      * Create a regular expression (string) for matching xpaths based on the given cps path query.
82      *
83      * @param cpsPathQuery the cps path query to determine the required regular expression
84      * @return a string representing the required regular expression
85      */
86     public static String getXpathSqlRegex(final CpsPathQuery cpsPathQuery) {
87         final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
88         xpathRegexBuilder.append(REGEX_OPTIONAL_LIST_INDEX_POSTFIX);
89         return xpathRegexBuilder.toString();
90     }
91
92     /**
93      * Create a regular expression (string) for matching xpaths with (all) descendants
94      * based on the given cps path query.
95      *
96      * @param cpsPathQuery the cps path query to determine the required regular expression
97      * @return a string representing the required regular expression
98      */
99     public static String getXpathSqlRegexForQuickFindWithDescendants(final CpsPathQuery cpsPathQuery) {
100         final StringBuilder xpathRegexBuilder = getRegexStringBuilderWithPrefix(cpsPathQuery);
101         xpathRegexBuilder.append(REGEX_FOR_QUICK_FIND_WITH_DESCENDANTS);
102         return xpathRegexBuilder.toString();
103     }
104
105     private Query getQueryForDataspaceOrAnchorAndCpsPath(final DataspaceEntity dataspaceEntity,
106                                                          final AnchorEntity anchorEntity,
107                                                          final CpsPathQuery cpsPathQuery) {
108         final StringBuilder sqlStringBuilder = new StringBuilder();
109         final Map<String, Object> queryParameters = new HashMap<>();
110
111         sqlStringBuilder.append("SELECT * FROM fragment WHERE ");
112         addDataspaceOrAnchor(sqlStringBuilder, queryParameters, dataspaceEntity, anchorEntity);
113         addXpathSearch(cpsPathQuery, sqlStringBuilder, queryParameters);
114         addLeafConditions(cpsPathQuery, sqlStringBuilder);
115         addTextFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters);
116         addContainsFunctionCondition(cpsPathQuery, sqlStringBuilder, queryParameters);
117
118         final Query query = entityManager.createNativeQuery(sqlStringBuilder.toString(), FragmentEntity.class);
119         setQueryParameters(query, queryParameters);
120         return query;
121     }
122
123     private static void addDataspaceOrAnchor(final StringBuilder sqlStringBuilder,
124                                              final Map<String, Object> queryParameters,
125                                              final DataspaceEntity dataspaceEntity,
126                                              final AnchorEntity anchorEntity) {
127         if (anchorEntity == ACROSS_ALL_ANCHORS) {
128             sqlStringBuilder.append("dataspace_id = :dataspaceId");
129             queryParameters.put("dataspaceId", dataspaceEntity.getId());
130         } else {
131             sqlStringBuilder.append("anchor_id = :anchorId");
132             queryParameters.put("anchorId", anchorEntity.getId());
133         }
134     }
135
136     private static void addXpathSearch(final CpsPathQuery cpsPathQuery,
137                                        final StringBuilder sqlStringBuilder,
138                                        final Map<String, Object> queryParameters) {
139         sqlStringBuilder.append(" AND xpath ~ :xpathRegex");
140         final String xpathRegex = getXpathSqlRegex(cpsPathQuery);
141         queryParameters.put("xpathRegex", xpathRegex);
142     }
143
144     private static StringBuilder getRegexStringBuilderWithPrefix(final CpsPathQuery cpsPathQuery) {
145         final StringBuilder xpathRegexBuilder = new StringBuilder();
146         if (CpsPathPrefixType.ABSOLUTE.equals(cpsPathQuery.getCpsPathPrefixType())) {
147             xpathRegexBuilder.append(REGEX_ABSOLUTE_PATH_PREFIX);
148             xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getXpathPrefix()));
149             return xpathRegexBuilder;
150         }
151         xpathRegexBuilder.append(REGEX_DESCENDANT_PATH_PREFIX);
152         xpathRegexBuilder.append(escapeXpath(cpsPathQuery.getDescendantName()));
153         return xpathRegexBuilder;
154     }
155
156     private static String escapeXpath(final String xpath) {
157         // See https://jira.onap.org/browse/CPS-500 for limitations of this basic escape mechanism
158         return xpath.replace("[@", "\\[@");
159     }
160
161     private static Integer getTextValueAsInt(final CpsPathQuery cpsPathQuery) {
162         try {
163             return Integer.parseInt(cpsPathQuery.getTextFunctionConditionValue());
164         } catch (final NumberFormatException e) {
165             return null;
166         }
167     }
168
169     private void addLeafConditions(final CpsPathQuery cpsPathQuery, final StringBuilder sqlStringBuilder) {
170         if (cpsPathQuery.hasLeafConditions()) {
171             sqlStringBuilder.append(" AND (");
172             final List<String> queryBooleanOperatorsType = cpsPathQuery.getBooleanOperatorsType();
173             final Queue<String> booleanOperatorsQueue = (queryBooleanOperatorsType == null) ? null : new LinkedList<>(
174                 queryBooleanOperatorsType);
175             cpsPathQuery.getLeavesData().entrySet().forEach(entry -> {
176                 sqlStringBuilder.append(" attributes @> ");
177                 sqlStringBuilder.append("'");
178                 sqlStringBuilder.append(jsonObjectMapper.asJsonString(entry));
179                 sqlStringBuilder.append("'");
180                 if (!(booleanOperatorsQueue == null || booleanOperatorsQueue.isEmpty())) {
181                     sqlStringBuilder.append(" ");
182                     sqlStringBuilder.append(booleanOperatorsQueue.poll());
183                     sqlStringBuilder.append(" ");
184                 }
185             });
186             sqlStringBuilder.append(")");
187         }
188     }
189
190     private static void addTextFunctionCondition(final CpsPathQuery cpsPathQuery,
191                                                  final StringBuilder sqlStringBuilder,
192                                                  final Map<String, Object> queryParameters) {
193         if (cpsPathQuery.hasTextFunctionCondition()) {
194             sqlStringBuilder.append(" AND (");
195             sqlStringBuilder.append("attributes @> jsonb_build_object(:textLeafName, :textValue)");
196             sqlStringBuilder
197                 .append(" OR attributes @> jsonb_build_object(:textLeafName, json_build_array(:textValue))");
198             queryParameters.put("textLeafName", cpsPathQuery.getTextFunctionConditionLeafName());
199             queryParameters.put("textValue", cpsPathQuery.getTextFunctionConditionValue());
200             final Integer textValueAsInt = getTextValueAsInt(cpsPathQuery);
201             if (textValueAsInt != null) {
202                 sqlStringBuilder.append(" OR attributes @> jsonb_build_object(:textLeafName, :textValueAsInt)");
203                 sqlStringBuilder
204                     .append(" OR attributes @> jsonb_build_object(:textLeafName, json_build_array(:textValueAsInt))");
205                 queryParameters.put("textValueAsInt", textValueAsInt);
206             }
207             sqlStringBuilder.append(")");
208         }
209     }
210
211     private static void addContainsFunctionCondition(final CpsPathQuery cpsPathQuery,
212                                                      final StringBuilder sqlStringBuilder,
213                                                      final Map<String, Object> queryParameters) {
214         if (cpsPathQuery.hasContainsFunctionCondition()) {
215             sqlStringBuilder.append(" AND attributes ->> :containsLeafName LIKE CONCAT('%',:containsValue,'%') ");
216             queryParameters.put("containsLeafName", cpsPathQuery.getContainsFunctionConditionLeafName());
217             queryParameters.put("containsValue", cpsPathQuery.getContainsFunctionConditionValue());
218         }
219     }
220
221     private static void setQueryParameters(final Query query, final Map<String, Object> queryParameters) {
222         for (final Map.Entry<String, Object> queryParameter : queryParameters.entrySet()) {
223             query.setParameter(queryParameter.getKey(), queryParameter.getValue());
224         }
225     }
226
227 }