2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.meta;
10 import com.google.common.collect.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import java.math.BigDecimal;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.yangtools.yang.common.Uint64;
18 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.RangeEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.RangeStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
25 import org.opendaylight.yangtools.yang.model.ri.stmt.DeclaredStatements;
26 import org.opendaylight.yangtools.yang.model.ri.stmt.EffectiveStatements;
27 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
28 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 public final class RangeStatementSupport
36 extends AbstractStatementSupport<List<ValueRange>, RangeStatement, RangeEffectiveStatement> {
37 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
38 SubstatementValidator.builder(YangStmtMapping.RANGE)
39 .addOptional(YangStmtMapping.DESCRIPTION)
40 .addOptional(YangStmtMapping.ERROR_APP_TAG)
41 .addOptional(YangStmtMapping.ERROR_MESSAGE)
42 .addOptional(YangStmtMapping.REFERENCE)
44 private static final RangeStatementSupport INSTANCE = new RangeStatementSupport();
46 private RangeStatementSupport() {
47 super(YangStmtMapping.RANGE, StatementPolicy.contextIndependent());
50 public static RangeStatementSupport getInstance() {
55 public ImmutableList<ValueRange> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String rangeArgument) {
56 final List<ValueRange> ranges = new ArrayList<>();
58 for (final String singleRange : ArgumentUtils.PIPE_SPLITTER.split(rangeArgument)) {
59 final Iterator<String> boundaries = ArgumentUtils.TWO_DOTS_SPLITTER.split(singleRange).iterator();
60 final Number min = parseDecimalConstraintValue(ctx, boundaries.next());
63 if (boundaries.hasNext()) {
64 max = parseDecimalConstraintValue(ctx, boundaries.next());
66 // if min larger than max then error
67 SourceException.throwIf(ArgumentUtils.compareNumbers(min, max) == 1, ctx,
68 "Range constraint %s has descending order of boundaries; should be ascending", singleRange);
69 SourceException.throwIf(boundaries.hasNext(), ctx,
70 "Wrong number of boundaries in range constraint %s", singleRange);
75 // some of intervals overlapping
76 InferenceException.throwIf(
77 ranges.size() > 1 && ArgumentUtils.compareNumbers(min, Iterables.getLast(ranges).upperBound()) != 1,
78 ctx, "Some of the value ranges in %s are not disjoint", rangeArgument);
79 ranges.add(ValueRange.of(min, max));
82 return ImmutableList.copyOf(ranges);
86 protected RangeStatement createDeclared(final StmtContext<List<ValueRange>, RangeStatement, ?> ctx,
87 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
88 return DeclaredStatements.createRange(ctx.getRawArgument(), ctx.getArgument(), substatements);
92 protected RangeEffectiveStatement createEffective(final Current<List<ValueRange>, RangeStatement> stmt,
93 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
94 return EffectiveStatements.createRange(stmt.declared(), substatements);
98 protected SubstatementValidator getSubstatementValidator() {
99 return SUBSTATEMENT_VALIDATOR;
102 private static @NonNull Number parseDecimalConstraintValue(final @NonNull StmtContext<?, ?, ?> ctx,
103 final @NonNull String value) {
104 if ("max".equals(value)) {
105 return UnresolvedNumber.max();
107 if ("min".equals(value)) {
108 return UnresolvedNumber.min();
110 // Deal with decimal64, i.e. 'decimal-value' production of the RFC6020 ABNF
111 if (value.indexOf('.') != -1) {
113 // FIXME: YANGTOOLS-556: Use Decimal64 here
114 return new BigDecimal(value);
115 } catch (NumberFormatException e) {
116 throw new SourceException(ctx, e, "Value %s is not a valid decimal number", value);
120 // This has to be an 'integer-value' production of the RFC6020 ABNF. We also clamp allowed range to Long/Uint,
121 // as that is the effectively-valid range of allowed values. For Uint64 we also try to intern the value.
123 return value.startsWith("-") ? Long.valueOf(value) : Uint64.valueOf(value).intern();
124 } catch (IllegalArgumentException e) {
125 throw new SourceException(ctx, e, "Value %s is not a valid integral range number", value);