Merge IdentityStatementSupport
[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, "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
74                 def.getStatementName(), def.getArgumentDefinition().get().getArgumentName(), input);
75         }
76     }
77
78     public static boolean isAbsoluteXPath(final String path) {
79         return PATH_ABS.matcher(path).matches();
80     }
81
82     public static Absolute parseAbsoluteSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
83         // FIXME: this does accept check for a leading slash
84         return Absolute.of(parseNodeIdentifiers(ctx, str));
85     }
86
87     public static Descendant parseDescendantSchemaNodeIdentifier(final StmtContext<?, ?, ?> ctx, final String str) {
88         // FIXME: this does accept a leading slash
89         return Descendant.of(parseNodeIdentifiers(ctx, str));
90     }
91
92     public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
93         final List<QName> qnames = parseNodeIdentifiers(ctx, path);
94         return PATH_ABS.matcher(path).matches() ? Absolute.of(qnames) : Descendant.of(qnames);
95     }
96
97     @SuppressWarnings("checkstyle:illegalCatch")
98     private static  List<QName> parseNodeIdentifiers(final StmtContext<?, ?, ?> ctx, final String path) {
99         // FIXME: is the path trimming really necessary??
100         final List<QName> qnames = new ArrayList<>();
101         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
102             try {
103                 qnames.add(StmtContextUtils.parseNodeIdentifier(ctx, nodeName));
104             } catch (final RuntimeException e) {
105                 throw new SourceException(ctx, e, "Failed to parse node '%s' in path '%s'", nodeName, path);
106             }
107         }
108
109         SourceException.throwIf(qnames.isEmpty(), ctx, "Schema node identifier must not be empty");
110         return qnames;
111     }
112
113     private static String trimSingleLastSlashFromXPath(final String path) {
114         return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
115     }
116
117     private static BigDecimal yangConstraintToBigDecimal(final Number number) {
118         if (UnresolvedNumber.max().equals(number)) {
119             return YANG_MAX_NUM;
120         }
121         if (UnresolvedNumber.min().equals(number)) {
122             return YANG_MIN_NUM;
123         }
124
125         return new BigDecimal(number.toString());
126     }
127 }