From ea71bef75b22c31a4b40a3cacbf5da2ddecb544b Mon Sep 17 00:00:00 2001 From: danielhanrahan Date: Fri, 14 Jul 2023 13:09:41 +0100 Subject: Allow duplicate leaf names in Cps Path leaf condition Presently, a query using the same leaf name twice will fail: //books[@price > 10 and @price < 20] It is caused by storing data leaves in a Map. This is fixed by storing data leaves in a List. Issue-ID: CPS-1779 Signed-off-by: danielhanrahan Change-Id: Ie6990ea5e622cf37e986b720a0a07fb69ce4f03b --- .../cpspath/parser/CpsPathBooleanOperatorType.java | 51 ----------------- .../onap/cps/cpspath/parser/CpsPathBuilder.java | 32 +++++------ .../cpspath/parser/CpsPathComparativeOperator.java | 64 ---------------------- .../org/onap/cps/cpspath/parser/CpsPathQuery.java | 12 +++- 4 files changed, 24 insertions(+), 135 deletions(-) delete mode 100644 cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBooleanOperatorType.java delete mode 100644 cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java (limited to 'cps-path-parser/src/main/java/org') diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBooleanOperatorType.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBooleanOperatorType.java deleted file mode 100644 index b2f1dddb19..0000000000 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBooleanOperatorType.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 TechMahindra Ltd - * ================================================================================ - * 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; - -public enum CpsPathBooleanOperatorType { - AND("and"), - OR("or"); - - private final String operatorValue; - - CpsPathBooleanOperatorType(final String operatorValue) { - this.operatorValue = operatorValue; - } - - public String getValues() { - return this.operatorValue; - } - - /** - * Finds the value of the given enumeration. - * - * @param operatorValue value of the enum - * @return a booleanOperatorType - */ - public static CpsPathBooleanOperatorType fromString(final String operatorValue) { - for (final CpsPathBooleanOperatorType booleanOperatorType : CpsPathBooleanOperatorType.values()) { - if (booleanOperatorType.operatorValue.equalsIgnoreCase(operatorValue)) { - return booleanOperatorType; - } - } - return null; - } -} 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 5c47127375..99135962f8 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 @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2021-2022 Nordix Foundation + * Copyright (C) 2021-2023 Nordix Foundation * Modifications Copyright (C) 2023 TechMahindra Ltd * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); @@ -24,9 +24,7 @@ package org.onap.cps.cpspath.parser; import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT; import java.util.ArrayList; -import java.util.LinkedHashMap; import java.util.List; -import java.util.Map; import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser; import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext; @@ -43,21 +41,21 @@ public class CpsPathBuilder extends CpsPathBaseListener { private static final String CLOSE_BRACKET = "]"; - final CpsPathQuery cpsPathQuery = new CpsPathQuery(); + private final CpsPathQuery cpsPathQuery = new CpsPathQuery(); - final Map leavesData = new LinkedHashMap<>(); + private final List leavesData = new ArrayList<>(); - final StringBuilder normalizedXpathBuilder = new StringBuilder(); + private final StringBuilder normalizedXpathBuilder = new StringBuilder(); - final StringBuilder normalizedAncestorPathBuilder = new StringBuilder(); + private final StringBuilder normalizedAncestorPathBuilder = new StringBuilder(); - boolean processingAncestorAxis = false; + private boolean processingAncestorAxis = false; - private List containerNames = new ArrayList<>(); + private final List containerNames = new ArrayList<>(); - final List booleanOperators = new ArrayList<>(); + private final List booleanOperators = new ArrayList<>(); - final List comparativeOperators = new ArrayList<>(); + private final List comparativeOperators = new ArrayList<>(); @Override public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) { @@ -99,16 +97,12 @@ public class CpsPathBuilder extends CpsPathBaseListener { @Override public void exitBooleanOperators(final CpsPathParser.BooleanOperatorsContext ctx) { - final CpsPathBooleanOperatorType cpsPathBooleanOperatorType = CpsPathBooleanOperatorType.fromString( - ctx.getText()); - booleanOperators.add(cpsPathBooleanOperatorType.getValues()); + booleanOperators.add(ctx.getText()); } @Override public void exitComparativeOperators(final CpsPathParser.ComparativeOperatorsContext ctx) { - final CpsPathComparativeOperator cpsPathComparativeOperator = CpsPathComparativeOperator.fromString( - ctx.getText()); - comparativeOperators.add(cpsPathComparativeOperator.getLabel()); + comparativeOperators.add(ctx.getText()); } @Override @@ -122,6 +116,8 @@ public class CpsPathBuilder extends CpsPathBaseListener { public void enterMultipleLeafConditions(final MultipleLeafConditionsContext ctx) { normalizedXpathBuilder.append(OPEN_BRACKET); leavesData.clear(); + booleanOperators.clear(); + comparativeOperators.clear(); } @Override @@ -193,7 +189,7 @@ public class CpsPathBuilder extends CpsPathBaseListener { } private void leafContext(final CpsPathParser.LeafNameContext ctx, final Object comparisonValue) { - leavesData.put(ctx.getText(), comparisonValue); + leavesData.add(new CpsPathQuery.DataLeaf(ctx.getText(), comparisonValue)); appendCondition(normalizedXpathBuilder, ctx.getText(), comparisonValue); if (processingAncestorAxis) { appendCondition(normalizedAncestorPathBuilder, ctx.getText(), comparisonValue); diff --git a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java b/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java deleted file mode 100644 index c7ffd0d7ec..0000000000 --- a/cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathComparativeOperator.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2023 Tech Mahindra Ltd - * ================================================================================ - * 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; - -import java.util.HashMap; -import java.util.Map; - -public enum CpsPathComparativeOperator { - EQ("="), - GT(">"), - LT("<"), - GE(">="), - LE("<="); - - private final String label; - - CpsPathComparativeOperator(final String label) { - this.label = label; - } - - public final String getLabel() { - return this.label; - } - - private static final Map cpsPathComparativeOperatorPerLabel = new HashMap<>(); - - static { - for (final CpsPathComparativeOperator cpsPathComparativeOperator : CpsPathComparativeOperator.values()) { - cpsPathComparativeOperatorPerLabel.put(cpsPathComparativeOperator.label, cpsPathComparativeOperator); - } - } - - /** - * Finds the value of the given enumeration. - * - * @param label value of the enum - * @return a comparativeOperatorType - */ - public static CpsPathComparativeOperator fromString(final String label) { - if (!cpsPathComparativeOperatorPerLabel.containsKey(label)) { - throw new PathParsingException("Incomplete leaf condition (no operator)"); - } - return cpsPathComparativeOperatorPerLabel.get(label); - } -} - 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 3c3cbccf7e..f98df05a28 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 @@ -24,8 +24,9 @@ package org.onap.cps.cpspath.parser; import static org.onap.cps.cpspath.parser.CpsPathPrefixType.ABSOLUTE; import java.util.List; -import java.util.Map; import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.Setter; @@ -39,7 +40,7 @@ public class CpsPathQuery { private List containerNames; private CpsPathPrefixType cpsPathPrefixType = ABSOLUTE; private String descendantName; - private Map leavesData; + private List leavesData; private String ancestorSchemaNodeIdentifier = ""; private String textFunctionConditionLeafName; private String textFunctionConditionValue; @@ -103,4 +104,11 @@ public class CpsPathQuery { return cpsPathPrefixType == ABSOLUTE && hasLeafConditions(); } + @Getter + @EqualsAndHashCode + @AllArgsConstructor + public static class DataLeaf { + private final String name; + private final Object value; + } } -- cgit 1.2.3-korg