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