Merge "Add cascade delete fragments to liquibase test config"
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathBuilder.java
index 7183120..854450c 100644 (file)
@@ -1,6 +1,7 @@
 /*
  *  ============LICENSE_START=======================================================
  *  Copyright (C) 2021-2022 Nordix Foundation
+ *  Modifications 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.
@@ -22,8 +23,12 @@ package org.onap.cps.cpspath.parser;
 
 import static org.onap.cps.cpspath.parser.CpsPathPrefixType.DESCENDANT;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
 import java.util.Map;
+import java.util.Queue;
 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;
@@ -50,6 +55,12 @@ public class CpsPathBuilder extends CpsPathBaseListener {
 
     boolean processingAncestorAxis = false;
 
+    private List<String> containerNames = new ArrayList<>();
+
+    final List<String> booleanOperators = new ArrayList<>();
+
+    final Queue<String> booleanOperatorsQueue = new LinkedList<>();
+
     @Override
     public void exitInvalidPostFix(final CpsPathParser.InvalidPostFixContext ctx) {
         throw new PathParsingException(ctx.getText());
@@ -86,12 +97,22 @@ public class CpsPathBuilder extends CpsPathBaseListener {
             throw new PathParsingException("Unsupported comparison value encountered in expression" + ctx.getText());
         }
         leavesData.put(ctx.leafName().getText(), comparisonValue);
-        appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), comparisonValue);
+        final String booleanOperator = booleanOperatorsQueue.poll();
+        appendCondition(normalizedXpathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue);
         if (processingAncestorAxis) {
-            appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), comparisonValue);
+            appendCondition(normalizedAncestorPathBuilder, ctx.leafName().getText(), booleanOperator, comparisonValue);
         }
     }
 
+    @Override
+    public void exitBooleanOperators(final CpsPathParser.BooleanOperatorsContext ctx) {
+        final CpsPathBooleanOperatorType cpsPathBooleanOperatorType = CpsPathBooleanOperatorType.fromString(
+                ctx.getText());
+        booleanOperators.add(cpsPathBooleanOperatorType.getValues());
+        booleanOperatorsQueue.add(cpsPathBooleanOperatorType.getValues());
+        cpsPathQuery.setBooleanOperatorsType(booleanOperators);
+    }
+
     @Override
     public void exitDescendant(final DescendantContext ctx) {
         cpsPathQuery.setCpsPathPrefixType(DESCENDANT);
@@ -128,6 +149,12 @@ public class CpsPathBuilder extends CpsPathBaseListener {
         cpsPathQuery.setTextFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
     }
 
+    @Override
+    public void exitContainsFunctionCondition(final CpsPathParser.ContainsFunctionConditionContext ctx) {
+        cpsPathQuery.setContainsFunctionConditionLeafName(ctx.leafName().getText());
+        cpsPathQuery.setContainsFunctionConditionValue(stripFirstAndLastCharacter(ctx.StringLiteral().getText()));
+    }
+
     @Override
     public void enterListElementRef(final CpsPathParser.ListElementRefContext ctx) {
         normalizedXpathBuilder.append(OPEN_BRACKET);
@@ -146,6 +173,7 @@ public class CpsPathBuilder extends CpsPathBaseListener {
 
     CpsPathQuery build() {
         cpsPathQuery.setNormalizedXpath(normalizedXpathBuilder.toString());
+        cpsPathQuery.setContainerNames(containerNames);
         return cpsPathQuery;
     }
 
@@ -155,21 +183,23 @@ public class CpsPathBuilder extends CpsPathBaseListener {
 
     @Override
     public void exitContainerName(final CpsPathParser.ContainerNameContext ctx) {
+        final String containerName = ctx.getText();
         normalizedXpathBuilder.append("/")
-                .append(ctx.getText());
+                .append(containerName);
+        containerNames.add(containerName);
         if (processingAncestorAxis) {
-            normalizedAncestorPathBuilder.append("/").append(ctx.getText());
+            normalizedAncestorPathBuilder.append("/").append(containerName);
         }
     }
 
     private void appendCondition(final StringBuilder currentNormalizedPathBuilder, final String name,
-                                final Object value) {
+                                 final String booleanOperator, final Object value) {
         final char lastCharacter = currentNormalizedPathBuilder.charAt(currentNormalizedPathBuilder.length() - 1);
-        currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " and ")
-                .append("@")
-                .append(name)
-                .append("='")
-                .append(value)
-                .append("'");
+        currentNormalizedPathBuilder.append(lastCharacter == '[' ? "" : " " + booleanOperator + " ")
+                                    .append("@")
+                                    .append(name)
+                                    .append("='")
+                                    .append(value)
+                                    .append("'");
     }
 }