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 enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
154         normalizedXpathBuilder.append(OPEN_BRACKET);
155         if (processingAncestorAxis) {
156             normalizedAncestorPathBuilder.append(OPEN_BRACKET);
157         }
158     }
159
160     @Override
161     public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
162         normalizedXpathBuilder.append(CLOSE_BRACKET);
163         if (processingAncestorAxis) {
164             normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
165         }
166     }
167
168     CpsPathQuery build() {
169         cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
170         cpsPathQuery.setContainerNames(containerNames);
171         return cpsPathQuery;
172     }
173
174     private static String stripFirstAndLastCharacter(final String wrappedString) {
175         return wrappedString.substring(1, wrappedString.length() - 1);
176     }
177
178     @Override
179     public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
180         final String containerName = ctx.getText();
181         normalizedXpathBuilder.append("/")
182                 .append(containerName);
183         containerNames.add(containerName);
184         if (processingAncestorAxis) {
185             normalizedAncestorPathBuilder.append("/").append(containerName);
186         }
187     }
188
189     private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
190                                  final String booleanOperator, final Object value) {
191         final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
192         currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " " + booleanOperator + " ")
193                                     .append("@")
194                                     .append(name)
195                                     .append("='")
196                                     .append(value)
197                                     .append("'");
198     }
199 }