From f0527c58c17963d940535d0ce0eb934c2b4c635c Mon Sep 17 00:00:00 2001 From: ToineSiebelink Date: Tue, 6 Jul 2021 13:03:03 +0100 Subject: Support text() condition - Added Antlr parsing of text() condition (as an optional additional to any query) - Implemented text-condition combined with descendants - Refactor descendants queries into using one more flexible Custom (native) Query builder - Refactor ALL cpsPath queries to now use FragmentRepositoryCpsPathQuery (custom query builder) - Refactor Antrl code to simply parsing of cpsPath and allow all combinations (no more query types, addresses CPS-436) - Minor clean up of some minor convention issues in CpsAdminServiceImplSpec.groovy (found during groovy demo) - Update .rst documentation of xPaths - Fixed incorrect matching of additional list indexes using more precise SIMILAR-TO regex in postgreSQL - Documented special chararter limitation (CPS-500) - Checked for consistent use of term 'CPS path' in documentation and error message - Included (updated) copyright in all .SQL test files Issue-ID: CPS-452 Issue-ID: CPS-436 Issue-ID: CPS-500 Signed-off-by: ToineSiebelink Change-Id: If422d25cafd2850d25c9a28dea16ba7a5f93dddb --- .../onap/cps/cpspath/parser/CpsPathBuilder.java | 52 ++++++++-------------- .../onap/cps/cpspath/parser/CpsPathPrefixType.java | 37 +++++++++++++++ .../org/onap/cps/cpspath/parser/CpsPathQuery.java | 28 ++++++++++-- .../onap/cps/cpspath/parser/CpsPathQueryType.java | 40 ----------------- 4 files changed, 80 insertions(+), 77 deletions(-) create mode 100644 cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathPrefixType.java delete mode 100644 cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQueryType.java (limited to 'cps-path-parser/src/main/java/org') 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 b9d0c25b1..ebf6fd3c9 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 @@ -20,18 +20,18 @@ package org.onap.cps.cpspath.parser; +import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT; + import java.util.HashMap; import java.util.Map; import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantAndLeafConditionsContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithSingleLeafConditionContext; +import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.DescendantContext; +import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.IncorrectPrefixContext; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleValueConditionsContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PostfixContext; +import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleLeafConditionsContext; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext; -import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.SingleValueConditionContext; +import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.TextFunctionConditionContext; public class CpsPathBuilder extends CpsPathBaseListener { @@ -45,8 +45,8 @@ public class CpsPathBuilder extends CpsPathBaseListener { } @Override - public void exitPostfix(final PostfixContext ctx) { - throw new IllegalStateException(String.format("Unsupported postfix %s encountered in CpsPath.", ctx.getText())); + public void exitIncorrectPrefix(final IncorrectPrefixContext ctx) { + throw new IllegalStateException("CPS path can only start with one or two slashes (/)"); } @Override @@ -64,38 +64,18 @@ public class CpsPathBuilder extends CpsPathBaseListener { } @Override - public void enterSingleValueCondition(final SingleValueConditionContext ctx) { - leavesData.clear(); + public void exitDescendant(final DescendantContext ctx) { + cpsPathQuery.setCpsPathPrefixType(DESCENDANT); + cpsPathQuery.setDescendantName(ctx.getText().substring(2)); } @Override - public void enterMultipleValueConditions(final MultipleValueConditionsContext ctx) { + public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) { leavesData.clear(); } @Override - public void exitSingleValueCondition(final SingleValueConditionContext ctx) { - final String leafName = ctx.leafCondition().leafName().getText(); - cpsPathQuery.setLeafName(leafName); - cpsPathQuery.setLeafValue(leavesData.get(leafName)); - } - - @Override - public void exitCpsPathWithSingleLeafCondition(final CpsPathWithSingleLeafConditionContext ctx) { - cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE); - } - - @Override - public void exitCpsPathWithDescendant(final CpsPathWithDescendantContext ctx) { - cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE); - cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1)); - } - - @Override - public void exitCpsPathWithDescendantAndLeafConditions( - final CpsPathWithDescendantAndLeafConditionsContext ctx) { - cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES); - cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1)); + public void exitMultipleLeafConditions(final MultipleLeafConditionsContext ctx) { cpsPathQuery.setLeavesData(leavesData); } @@ -104,6 +84,12 @@ public class CpsPathBuilder extends CpsPathBaseListener { cpsPathQuery.setAncestorSchemaNodeIdentifier(ctx.ancestorPath().getText()); } + @Override + public void exitTextFunctionCondition(final TextFunctionConditionContext ctx) { + cpsPathQuery.setTextFunctionConditionLeafName(ctx.leafName().getText()); + cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText())); + } + CpsPathQuery build() { return cpsPathQuery; } diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathPrefixType.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathPrefixType.java new file mode 100644 index 000000000..dfac9b0c8 --- /dev/null +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathPrefixType.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2021 Nordix Foundation + * Modifications Copyright (C) 2020-2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.cpspath.parser; + +/** + * The enum Cps path prefix type. + */ +public enum CpsPathPrefixType { + /** + * Fully qualified Xpath starting from root with single slash e.g. /parent/child . + */ + ABSOLUTE, + + /** + * Xpath descendant anywhere starting with double slash type e.g. //child/grandchild . + */ + DESCENDANT +} diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java index 107bfa3e3..de7adf2b7 100644 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java +++ b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQuery.java @@ -20,6 +20,8 @@ package org.onap.cps.cpspath.parser; +import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE; + import java.util.Map; import lombok.AccessLevel; import lombok.Getter; @@ -36,13 +38,13 @@ import org.onap.cps.cpspath.parser.antlr4.CpsPathParser; @Setter(AccessLevel.PACKAGE) public class CpsPathQuery { - private CpsPathQueryType cpsPathQueryType; private String xpathPrefix; - private String leafName; - private Object leafValue; + private CpsPathPrefixType cpsPathPrefixType = ABSOLUTE; private String descendantName; private Map leavesData; private String ancestorSchemaNodeIdentifier = ""; + private String textFunctionConditionLeafName; + private String textFunctionConditionValue; /** * Returns a cps path query. @@ -68,7 +70,7 @@ public class CpsPathQuery { } /** - * Has ancestor axis been populated. + * Has ancestor axis been included in cpsPath. * * @return boolean value. */ @@ -76,4 +78,22 @@ public class CpsPathQuery { return !(ancestorSchemaNodeIdentifier.isEmpty()); } + /** + * Have leaf value conditions been included in cpsPath. + * + * @return boolean value. + */ + public boolean hasLeafConditions() { + return leavesData != null; + } + + /** + * Has text function condition been included in cpsPath. + * + * @return boolean value. + */ + public boolean hasTextFunctionCondition() { + return textFunctionConditionLeafName != null; + } + } diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQueryType.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQueryType.java deleted file mode 100644 index ac3e31334..000000000 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathQueryType.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2021 Nordix Foundation - * Modifications Copyright (C) 2020-2021 Bell Canada. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * SPDX-License-Identifier: Apache-2.0 - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.cpspath.parser; - -/** - * The enum Cps path query type. - */ -public enum CpsPathQueryType { - /** - * Xpath descendant anywhere type e.g. //nodeName . - */ - XPATH_HAS_DESCENDANT_ANYWHERE, - /** - * Xpath descendant anywhere type e.g. //nodeName[@leafName="value"] . - */ - XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES, - /** - * Xpath leaf value cps path query type e.g. /cps-path[@leaf1="leafValue" and @leaf2=123] . - */ - XPATH_LEAF_VALUE -} -- cgit 1.2.3-korg