Merge "Add cascade delete fragments to liquibase test config"
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021-2022 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.cpspath.parser;
23
24 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Queue;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
33 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
34 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
35 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext;
36 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext;
37 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
38 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext;
39 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
40 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext;
41
42 public class CpsPathBuilder extends CpsPathBaseListener {
43
44     private static final String OPEN_BRACKET = "[";
45
46     private static final String CLOSE_BRACKET = "]";
47
48     final CpsPathQuery cpsPathQuery = new CpsPathQuery();
49
50     final Map<String, Object> leavesData = new HashMap<>();
51
52     final StringBuilder normalizedXpathBuilder = new StringBuilder();
53
54     final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
55
56     boolean processingAncestorAxis = false;
57
58     private List<String> containerNames = new ArrayList<>();
59
60     final List<String> booleanOperators = new ArrayList<>();
61
62     final Queue<String> booleanOperatorsQueue = new LinkedList<>();
63
64     @Override
65     public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
66         throw new PathParsingException(ctx.getText());
67     }
68
69     @Override
70     public void exitPrefix(final PrefixContext ctx) {
71         cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
72     }
73
74     @Override
75     public void exitParent(final CpsPathParser.ParentContext ctx) {
76         cpsPathQuery.setNormalizedParentPath(normalizedXpathBuilder.toString());
77     }
78
79     @Override
80     public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
81         throw new PathParsingException("CPS path can only start with one or two slashes (/)");
82     }
83
84     @Override
85     public void exitLeafCondition(final LeafConditionContext ctx) {
86         Object comparisonValue = null;
87         if (ctx.IntegerLiteral() != null) {
88             comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
89         }
90         if (ctx.StringLiteral() != null) {
91             final boolean wasWrappedInDoubleQuote  = ctx.StringLiteral().getText().startsWith("\"");
92             comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
93             if (wasWrappedInDoubleQuote) {
94                 comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'");
95             }
96         } else if (comparisonValue == null) {
97             throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
98         }
99         leavesData.put(ctx.leafName().getText(), comparisonValue);
100         final String booleanOperator = booleanOperatorsQueue.poll();
101         appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue);
102         if (processingAncestorAxis) {
103             appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue);
104         }
105     }
106
107     @Override
108     public void exitBooleanOperators(final CpsPathParser.BooleanOperatorsContext ctx) {
109         final CpsPathBooleanOperatorType cpsPathBooleanOperatorType = CpsPathBooleanOperatorType.fromString(
110                 ctx.getText());
111         booleanOperators.add(cpsPathBooleanOperatorType.getValues());
112         booleanOperatorsQueue.add(cpsPathBooleanOperatorType.getValues());
113         cpsPathQuery.setBooleanOperatorsType(booleanOperators);
114     }
115
116     @Override
117     public void exitDescendant(final DescendantContext ctx) {
118         cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
119         cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
120         normalizedXpathBuilder.insert(0, "/");
121     }
122
123     @Override
124     public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx)  {
125         normalizedXpathBuilder.append(OPEN_BRACKET);
126         leavesData.clear();
127     }
128
129     @Override
130     public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
131         normalizedXpathBuilder.append(CLOSE_BRACKET);
132         cpsPathQuery.setLeavesData(leavesData);
133     }
134
135     @Override
136     public void enterAncestorAxis(final AncestorAxisContext ctx) {
137         processingAncestorAxis = true;
138     }
139
140     @Override
141     public void exitAncestorAxis(final AncestorAxisContext ctx) {
142         cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
143         processingAncestorAxis = false;
144     }
145
146     @Override
147     public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
148         cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
149         cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
150     }
151
152     @Override
153     public void exitContainsFunctionCondition(final CpsPathParser.ContainsFunctionConditionContext ctx) {
154         cpsPathQuery.setContainsFunctionConditionLeafName(ctx.leafName().getText());
155         cpsPathQuery.setContainsFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
156     }
157
158     @Override
159     public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
160         normalizedXpathBuilder.append(OPEN_BRACKET);
161         if (processingAncestorAxis) {
162             normalizedAncestorPathBuilder.append(OPEN_BRACKET);
163         }
164     }
165
166     @Override
167     public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
168         normalizedXpathBuilder.append(CLOSE_BRACKET);
169         if (processingAncestorAxis) {
170             normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
171         }
172     }
173
174     CpsPathQuery build() {
175         cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
176         cpsPathQuery.setContainerNames(containerNames);
177         return cpsPathQuery;
178     }
179
180     private static String stripFirstAndLastCharacter(final String wrappedString) {
181         return wrappedString.substring(1, wrappedString.length() - 1);
182     }
183
184     @Override
185     public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
186         final String containerName = ctx.getText();
187         normalizedXpathBuilder.append("/")
188                 .append(containerName);
189         containerNames.add(containerName);
190         if (processingAncestorAxis) {
191             normalizedAncestorPathBuilder.append("/").append(containerName);
192         }
193     }
194
195     private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
196                                  final String booleanOperator, final Object value) {
197         final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
198         currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " " + booleanOperator + " ")
199                                     .append("@")
200                                     .append(name)
201                                     .append("='")
202                                     .append(value)
203                                     .append("'");
204     }
205 }