Fixup SchemaNodeIdentifier design
[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.RevisionAwareXPath;
20 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
21 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
22 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
23 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Descendant;
24 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28
29 /**
30  * Utility class for dealing with arguments encountered by StatementSupport classes.
31  */
32 @Beta
33 public final class ArgumentUtils {
34     public static final Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();
35     public static final Splitter TWO_DOTS_SPLITTER = Splitter.on("..").trimResults();
36
37     @Regex
38     private static final String PATH_ABS_STR = "/[^/].*";
39     private static final Pattern PATH_ABS = Pattern.compile(PATH_ABS_STR);
40     private static final Splitter SLASH_SPLITTER = Splitter.on('/').omitEmptyStrings().trimResults();
41
42     // these objects are to compare whether range has MAX or MIN value
43     // none of these values should appear as Yang number according to spec so they are safe to use
44     private static final BigDecimal YANG_MIN_NUM = BigDecimal.valueOf(-Double.MAX_VALUE);
45     private static final BigDecimal YANG_MAX_NUM = BigDecimal.valueOf(Double.MAX_VALUE);
46
47     private ArgumentUtils() {
48         // Hidden on purpose
49     }
50
51     public static int compareNumbers(final Number n1, final Number n2) {
52         final BigDecimal num1 = yangConstraintToBigDecimal(n1);
53         final BigDecimal num2 = yangConstraintToBigDecimal(n2);
54         return new BigDecimal(num1.toString()).compareTo(new BigDecimal(num2.toString()));
55     }
56
57     public static String internBoolean(final String input) {
58         if ("true".equals(input)) {
59             return "true";
60         } else if ("false".equals(input)) {
61             return "false";
62         } else {
63             return input;
64         }
65     }
66
67     public static @NonNull Boolean parseBoolean(final StmtContext<?, ?, ?> ctx, final String input) {
68         if ("true".equals(input)) {
69             return Boolean.TRUE;
70         } else if ("false".equals(input)) {
71             return Boolean.FALSE;
72         } else {
73             final StatementDefinition def = ctx.getPublicDefinition();
74             throw new SourceException(ctx.getStatementSourceReference(),
75                 "Invalid '%s' statement %s '%s', it can be either 'true' or 'false'",
76                 def.getStatementName(), def.getArgumentDefinition().get().getArgumentName(), input);
77         }
78     }
79
80     public static RevisionAwareXPath parseXPath(final StmtContext<?, ?, ?> ctx, final String path) {
81         return XPathSupport.parseXPath(ctx, path);
82     }
83
84     public static boolean isAbsoluteXPath(final String path) {
85         return PATH_ABS.matcher(path).matches();
86     }
87
88     @SuppressWarnings("checkstyle:illegalCatch")
89     public static SchemaNodeIdentifier nodeIdentifierFromPath(final StmtContext<?, ?, ?> ctx, final String path) {
90         // FIXME: is the path trimming really necessary??
91         final List<QName> qNames = new ArrayList<>();
92         for (final String nodeName : SLASH_SPLITTER.split(trimSingleLastSlashFromXPath(path))) {
93             try {
94                 qNames.add(StmtContextUtils.parseNodeIdentifier(ctx, nodeName));
95             } catch (final RuntimeException e) {
96                 throw new SourceException(ctx.getStatementSourceReference(), e,
97                         "Failed to parse node '%s' in path '%s'", nodeName, path);
98             }
99         }
100
101         if (qNames.isEmpty()) {
102             throw new SourceException("Schema node identifier must not be empty", ctx.getStatementSourceReference());
103         }
104
105         return PATH_ABS.matcher(path).matches() ? Absolute.of(qNames) : Descendant.of(qNames);
106     }
107
108     private static String trimSingleLastSlashFromXPath(final String path) {
109         return path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
110     }
111
112     private static BigDecimal yangConstraintToBigDecimal(final Number number) {
113         if (UnresolvedNumber.max().equals(number)) {
114             return YANG_MAX_NUM;
115         }
116         if (UnresolvedNumber.min().equals(number)) {
117             return YANG_MIN_NUM;
118         }
119
120         return new BigDecimal(number.toString());
121     }
122 }