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