Migrate get{PublicDefinition,StatementSource} callers
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / ArgumentUtils.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Splitter;
12 import java.math.BigDecimal;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.regex.Pattern;
16 import org.checkerframework.checker.regex.qual.Regex;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.yangtools.yang.common.QName;
19 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
20 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27
28 /**
29  * Utility class for dealing with arguments encountered by StatementSupport classes.
30  */
31 @Beta
32 public final class ArgumentUtils {
33     public static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();
34     public static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults();
35
36     @Regex
37     private static final String PATH_ABS_STR = "/[^/].*";
38     private static final Pattern PATH_ABS = Pattern.compile(PATH_ABS_STR);
39     private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings().trimResults();
40
41     // these objects are to compare whether range has MAX or MIN value
42     // none of these values should appear as Yang number according to spec so they are safe to use
43     private static final BigDecimal YANG_MIN_NUM = BigDecimal.valueOf(-Double.MAX_VALUE);
44     private static final BigDecimal YANG_MAX_NUM = BigDecimal.valueOf(Double.MAX_VALUE);
45
46     private ArgumentUtils() {
47         // Hidden on purpose
48     }
49
50     public static int compareNumbers(final Number n1, final Number n2) {
51         final BigDecimal num1 = yangConstraintToBigDecimal(n1);
52         final BigDecimal num2 = yangConstraintToBigDecimal(n2);
53         return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString()));
54     }
55
56     public static String internBoolean(final String input) {
57         if ("true".equals(input)) {
58             return "true";
59         } else if ("false".equals(input)) {
60             return "false";
61         } else {
62             return input;
63         }
64     }
65
66     public static @NonNull Boolean parseBoolean(final StmtContext<?, ?, ?> ctx, final String input) {
67         if ("true".equals(input)) {
68             return Boolean.TRUE;
69         } else if ("false".equals(input)) {
70             return Boolean.FALSE;
71         } else {
72             final StatementDefinition def = ctx.publicDefinition();
73             throw new SourceException(ctx.getStatementSourceReference(),
74                 "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
75                 def.getStatementName(), def.getArgumentDefinition().get().getArgumentName(), input);
76         }
77     }
78
79     public static boolean isAbsoluteXPath(final String path) {
80         return PATH_ABS.matcher(path).matches();
81     }
82
83     public static Absolute parseAbsoluteSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
84         // FIXME: this does accept check for a leading slash
85         return Absolute.of(parseNodeIdentifiers(ctx, str));
86     }
87
88     public static Descendant parseDescendantSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
89         // FIXME: this does accept a leading slash
90         return Descendant.of(parseNodeIdentifiers(ctx, str));
91     }
92
93     public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
94         final List<QName> qnames = parseNodeIdentifiers(ctx, path);
95         return PATH_ABS.matcher(path).matches() ? Absolute.of(qnames) : Descendant.of(qnames);
96     }
97
98     @SuppressWarnings("checkstyle:illegalCatch")
99     private static  List<QName> parseNodeIdentifiers(final StmtContext<?, ?, ?> ctx, final String path) {
100         // FIXME: is the path trimming really necessary??
101         final List<QName> qnames = new ArrayList<>();
102         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
103             try {
104                 qnames.add(StmtContextUtils.parseNodeIdentifier(ctx, nodeName));
105             } catch (final RuntimeException e) {
106                 throw new SourceException(ctx.getStatementSourceReference(), e,
107                         "Failed to parse node '%s' in path '%s'", nodeName, path);
108             }
109         }
110
111         if (qnames.isEmpty()) {
112             throw new SourceException("Schema node identifier must not be empty", ctx.getStatementSourceReference());
113         }
114         return qnames;
115     }
116
117     private static String trimSingleLastSlashFromXPath(final String path) {
118         return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
119     }
120
121     private static BigDecimal yangConstraintToBigDecimal(final Number number) {
122         if (UnresolvedNumber.max().equals(number)) {
123             return YANG_MAX_NUM;
124         }
125         if (UnresolvedNumber.min().equals(number)) {
126             return YANG_MIN_NUM;
127         }
128
129         return new BigDecimal(number.toString());
130     }
131 }