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