a67cbeb870100a91c2f9ccbee05fb2d2239307b5
[cps.git] / cps-path-parser / src / main / java / org / onap / cps / cpspath / parser / CpsPathBuilder.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.onap.cps.cpspath.parser;
21
22 import java.util.HashMap;
23 import java.util.Map;
24 import org.onap.cps.cpspath.parser.antlr4.CpsPathBaseListener;
25 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.AncestorAxisContext;
26 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantAndLeafConditionsContext;
27 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithDescendantContext;
28 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.CpsPathWithSingleLeafConditionContext;
29 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.LeafConditionContext;
30 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.MultipleValueConditionsContext;
31 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.PrefixContext;
32 import org.onap.cps.cpspath.parser.antlr4.CpsPathParser.SingleValueConditionContext;
33
34 public class CpsPathBuilder extends CpsPathBaseListener {
35
36     final CpsPathQuery cpsPathQuery = new CpsPathQuery();
37
38     final Map<String, Object> leavesData = new HashMap<>();
39
40     @Override
41     public void exitPrefix(final PrefixContext ctx) {
42         cpsPathQuery.setXpathPrefix(ctx.getText());
43     }
44
45     @Override
46     public void exitLeafCondition(final LeafConditionContext ctx) {
47         Object comparisonValue = null;
48         if (ctx.IntegerLiteral() != null) {
49             comparisonValue = Integer.valueOf(ctx.IntegerLiteral().getText());
50         }
51         if (ctx.StringLiteral() != null) {
52             comparisonValue = stripFirstAndLastCharacter(ctx.StringLiteral().getText());
53         } else if (comparisonValue == null) {
54             throw new IllegalStateException("Unsupported comparison value encountered in expression" + ctx.getText());
55         }
56         leavesData.put(ctx.leafName().getText(), comparisonValue);
57     }
58
59     @Override
60     public void enterSingleValueCondition(final SingleValueConditionContext ctx) {
61         leavesData.clear();
62     }
63
64     @Override
65     public void enterMultipleValueConditions(final MultipleValueConditionsContext ctx) {
66         leavesData.clear();
67     }
68
69     @Override
70     public void exitSingleValueCondition(final SingleValueConditionContext ctx) {
71         final String leafName = ctx.leafCondition().leafName().getText();
72         cpsPathQuery.setLeafName(leafName);
73         cpsPathQuery.setLeafValue(leavesData.get(leafName));
74     }
75
76     @Override
77     public void exitCpsPathWithSingleLeafCondition(final CpsPathWithSingleLeafConditionContext ctx) {
78         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_LEAF_VALUE);
79     }
80
81     @Override
82     public void exitCpsPathWithDescendant(final CpsPathWithDescendantContext ctx) {
83         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_ANYWHERE);
84         cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
85     }
86
87     @Override
88     public void exitCpsPathWithDescendantAndLeafConditions(
89         final CpsPathWithDescendantAndLeafConditionsContext ctx) {
90         cpsPathQuery.setCpsPathQueryType(CpsPathQueryType.XPATH_HAS_DESCENDANT_WITH_LEAF_VALUES);
91         cpsPathQuery.setDescendantName(cpsPathQuery.getXpathPrefix().substring(1));
92         cpsPathQuery.setLeavesData(leavesData);
93     }
94
95     @Override
96     public void exitAncestorAxis(final AncestorAxisContext ctx) {
97         cpsPathQuery.setAncestorSchemaNodeIdentifier(ctx.ancestorPath().getText());
98     }
99
100     CpsPathQuery build() {
101         return cpsPathQuery;
102     }
103
104     private static String stripFirstAndLastCharacter(final String wrappedString) {
105         return wrappedString.substring(1, wrappedString.length() - 1);
106     }
107
108 }