Fix Absolute Path to list with Integer/String key
[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  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.cpspath.parser;
22
23 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
28 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
33 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext;
34 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
35 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext;
36
37 public class CpsPathBuilder extends CpsPathBaseListener {
38
39     private static final String OPEN_BRACKET = "[";
40
41     private static final String CLOSE_BRACKET = "]";
42
43     final CpsPathQuery cpsPathQuery = new CpsPathQuery();
44
45     final Map<String, Object> leavesData = new HashMap<>();
46
47     final StringBuilder normalizedXpathBuilder = new StringBuilder();
48
49     final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
50
51     boolean processingAncestorAxis = false;
52
53     @Override
54     public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
55         throw new PathParsingException(ctx.getText());
56     }
57
58     @Override
59     public void exitPrefix(final PrefixContext ctx) {
60         cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
61     }
62
63     @Override
64     public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) {
65         throw new PathParsingException("CPS path can only start with one or two slashes (/)");
66     }
67
68     @Override
69     public void exitLeafCondition(final LeafConditionContext ctx) {
70         Object comparisonValue = null;
71         if (ctx.IntegerLiteral() != null) {
72             comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
73         }
74         if (ctx.StringLiteral() != null) {
75             final boolean wasWrappedInDoubleQuote  = ctx.StringLiteral().getText().startsWith("\"");
76             comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
77             if (wasWrappedInDoubleQuote) {
78                 comparisonValue = String.valueOf(comparisonValue).replace("'", "\\'");
79             }
80         } else if (comparisonValue == null) {
81             throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
82         }
83         leavesData.put(ctx.leafName().getText(), comparisonValue);
84         appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
85         if (processingAncestorAxis) {
86             appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
87         }
88     }
89
90     @Override
91     public void exitDescendant(final DescendantContext ctx) {
92         cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
93         cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
94         normalizedXpathBuilder.insert(0, "/");
95     }
96
97     @Override
98     public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx)  {
99         normalizedXpathBuilder.append(OPEN_BRACKET);
100         leavesData.clear();
101     }
102
103     @Override
104     public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) {
105         normalizedXpathBuilder.append(CLOSE_BRACKET);
106         cpsPathQuery.setLeavesData(leavesData);
107     }
108
109     @Override
110     public void enterAncestorAxis(final AncestorAxisContext ctx) {
111         processingAncestorAxis = true;
112     }
113
114     @Override
115     public void exitAncestorAxis(final AncestorAxisContext ctx) {
116         cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
117         processingAncestorAxis = false;
118     }
119
120     @Override
121     public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) {
122         cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText());
123         cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
124     }
125
126     @Override
127     public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
128         normalizedXpathBuilder.append(OPEN_BRACKET);
129         if (processingAncestorAxis) {
130             normalizedAncestorPathBuilder.append(OPEN_BRACKET);
131         }
132     }
133
134     @Override
135     public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
136         normalizedXpathBuilder.append(CLOSE_BRACKET);
137         if (processingAncestorAxis) {
138             normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
139         }
140     }
141
142     CpsPathQuery build() {
143         cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
144         return cpsPathQuery;
145     }
146
147     private static String stripFirstAndLastCharacter(final String wrappedString) {
148         return wrappedString.substring(1, wrappedString.length() - 1);
149     }
150
151     @Override
152     public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
153         normalizedXpathBuilder.append("/")
154                 .append(ctx.getText());
155         if (processingAncestorAxis) {
156             normalizedAncestorPathBuilder.append("/").append(ctx.getText());
157         }
158     }
159
160     private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
161                                 final Object value) {
162         final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
163         currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " and ")
164                 .append("@")
165                 .append(name)
166                 .append("='")
167                 .append(value)
168                 .append("'");
169     }
170 }