summaryrefslogtreecommitdiffstats
path: root/cps-path-parser/src/main
diff options
context:
space:
mode:
authordanielhanrahan <daniel.hanrahan@est.tech>2024-09-04 18:15:17 +0100
committerdanielhanrahan <daniel.hanrahan@est.tech>2024-09-18 16:03:15 +0100
commit44cb5c4d60cd73147e469338cfd7a14ba13857b3 (patch)
tree834e91b820d5fe0f4c21fb05a23e09de0039e5a5 /cps-path-parser/src/main
parent9d967bc4a63238d1bd147155b4c3abf95c2ac7c5 (diff)
[Cps Path Parser] Fixes for parent path & normalization
This commit fixes issues with Cps Path Parser related to path normalization and parent path generation when using descendant paths and ancestor axis. Issue-ID: CPS-2365 Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech> Change-Id: I728fc379b134bd62c39a7085650930450c8a8597
Diffstat (limited to 'cps-path-parser/src/main')
-rw-r--r--cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g417
-rw-r--r--cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java42
2 files changed, 33 insertions, 26 deletions
diff --git a/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4 b/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4
index be8d968fcb..444702db48 100644
--- a/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4
+++ b/cps-path-parser/src/main/antlr4/org/onap/cps/cpspath/parser/antlr4/CpsPath.g4
@@ -29,19 +29,21 @@ grammar CpsPath ;
cpsPath : ( prefix | descendant ) multipleLeafConditions? textFunctionCondition? containsFunctionCondition? ancestorAxis? EOF ;
-ancestorAxis : SLASH KW_ANCESTOR COLONCOLON ancestorPath ;
+slash : SLASH ;
-ancestorPath : yangElement ( SLASH yangElement)* ;
+ancestorAxis : KW_ANCESTOR_AXIS_PREFIX ancestorPath ;
-textFunctionCondition : SLASH leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ;
+ancestorPath : yangElement ( slash yangElement)* ;
+
+textFunctionCondition : slash leafName OB KW_TEXT_FUNCTION EQ StringLiteral CB ;
containsFunctionCondition : OB KW_CONTAINS_FUNCTION OP AT leafName COMMA StringLiteral CP CB ;
-parent : ( SLASH yangElement)* ;
+parent : ( slash yangElement)* ;
-prefix : parent SLASH containerName ;
+prefix : parent slash containerName ;
-descendant : SLASH prefix ;
+descendant : slash prefix ;
yangElement : containerName listElementRef? ;
@@ -85,7 +87,8 @@ KW_ANCESTOR : 'ancestor' ;
KW_AND : 'and' ;
KW_TEXT_FUNCTION: 'text()' ;
KW_OR : 'or' ;
-KW_CONTAINS_FUNCTION: 'contains' ;
+KW_CONTAINS_FUNCTION : 'contains' ;
+KW_ANCESTOR_AXIS_PREFIX : SLASH KW_ANCESTOR COLONCOLON ;
IntegerLiteral : FragDigits ;
// Add below type definitions for leafvalue comparision in https://jira.onap.org/browse/CPS-440
diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java
index ac535f5efc..ed7dbecc18 100644
--- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java
+++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java
@@ -36,6 +36,8 @@ import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionCon
public class CpsPathBuilder extends CpsPathBaseListener {
+ private static final String NO_PARENT_PATH = "";
+
private static final String OPEN_BRACKET = "[";
private static final String CLOSE_BRACKET = "]";
@@ -46,7 +48,7 @@ public class CpsPathBuilder extends CpsPathBaseListener {
private final StringBuilder normalizedXpathBuilder = new StringBuilder();
- private final StringBuilder normalizedAncestorPathBuilder = new StringBuilder();
+ private int startIndexOfAncestorSchemaNodeIdentifier = 0;
private boolean processingAncestorAxis = false;
@@ -55,13 +57,24 @@ public class CpsPathBuilder extends CpsPathBaseListener {
private final List<String> booleanOperators = new ArrayList<>();
@Override
+ public void exitSlash(final CpsPathParser.SlashContext ctx) {
+ normalizedXpathBuilder.append("/");
+ }
+
+ @Override
public void exitPrefix(final PrefixContext ctx) {
cpsPathQuery.setXpathPrefix(normalizedXpathBuilder.toString());
}
@Override
public void exitParent(final CpsPathParser.ParentContext ctx) {
- cpsPathQuery.setNormalizedParentPath(normalizedXpathBuilder.toString());
+ final String normalizedParentPath;
+ if (normalizedXpathBuilder.toString().equals("/")) {
+ normalizedParentPath = NO_PARENT_PATH;
+ } else {
+ normalizedParentPath = normalizedXpathBuilder.toString();
+ }
+ cpsPathQuery.setNormalizedParentPath(normalizedParentPath);
}
@Override
@@ -87,8 +100,7 @@ public class CpsPathBuilder extends CpsPathBaseListener {
@Override
public void exitDescendant(final DescendantContext ctx) {
cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
- cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(1));
- normalizedXpathBuilder.insert(0, "/");
+ cpsPathQuery.setDescendantName(normalizedXpathBuilder.substring(2));
}
@Override
@@ -107,12 +119,15 @@ public class CpsPathBuilder extends CpsPathBaseListener {
@Override
public void enterAncestorAxis(final AncestorAxisContext ctx) {
processingAncestorAxis = true;
+ normalizedXpathBuilder.append("/ancestor::");
+ startIndexOfAncestorSchemaNodeIdentifier = normalizedXpathBuilder.length();
}
@Override
public void exitAncestorAxis(final AncestorAxisContext ctx) {
- cpsPathQuery.setAncestorSchemaNodeIdentifier(normalizedAncestorPathBuilder.substring(1));
processingAncestorAxis = false;
+ cpsPathQuery.setAncestorSchemaNodeIdentifier(
+ normalizedXpathBuilder.substring(startIndexOfAncestorSchemaNodeIdentifier));
}
@Override
@@ -130,17 +145,11 @@ public class CpsPathBuilder extends CpsPathBaseListener {
@Override
public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
normalizedXpathBuilder.append(OPEN_BRACKET);
- if (processingAncestorAxis) {
- normalizedAncestorPathBuilder.append(OPEN_BRACKET);
- }
}
@Override
public void exitListElementRef(final CpsPathParser.ListElementRefContext ctx) {
normalizedXpathBuilder.append(CLOSE_BRACKET);
- if (processingAncestorAxis) {
- normalizedAncestorPathBuilder.append(CLOSE_BRACKET);
- }
}
CpsPathQuery build() {
@@ -153,20 +162,15 @@ public class CpsPathBuilder extends CpsPathBaseListener {
@Override
public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
final String containerName = ctx.getText();
- normalizedXpathBuilder.append("/")
- .append(containerName);
- containerNames.add(containerName);
- if (processingAncestorAxis) {
- normalizedAncestorPathBuilder.append("/").append(containerName);
+ normalizedXpathBuilder.append(containerName);
+ if (!processingAncestorAxis) {
+ containerNames.add(containerName);
}
}
private void leafContext(final String leafName, final String operator, final Object comparisonValue) {
leafConditions.add(new CpsPathQuery.LeafCondition(leafName, operator, comparisonValue));
appendCondition(normalizedXpathBuilder, leafName, operator, comparisonValue);
- if (processingAncestorAxis) {
- appendCondition(normalizedAncestorPathBuilder, leafName, operator, comparisonValue);
- }
}
private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,