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.length;
10 import com.google.common.collect.Iterables;
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.Iterator;
14 import java.util.List;
15 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.LengthStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 public final class LengthStatementSupport extends AbstractStatementSupport<List<ValueRange>, LengthStatement,
28 EffectiveStatement<List<ValueRange>, LengthStatement>> {
29 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
31 .addOptional(YangStmtMapping.DESCRIPTION)
32 .addOptional(YangStmtMapping.ERROR_APP_TAG)
33 .addOptional(YangStmtMapping.ERROR_MESSAGE)
34 .addOptional(YangStmtMapping.REFERENCE)
37 public LengthStatementSupport() {
38 super(YangStmtMapping.LENGTH);
42 public List<ValueRange> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43 final List<ValueRange> ranges = new ArrayList<>();
45 for (final String singleRange : ArgumentUtils.PIPE_SPLITTER.split(value)) {
46 final Iterator<String> boundaries = ArgumentUtils.TWO_DOTS_SPLITTER.split(singleRange).iterator();
47 final Number min = parseIntegerConstraintValue(ctx, boundaries.next());
50 if (boundaries.hasNext()) {
51 max = parseIntegerConstraintValue(ctx, boundaries.next());
53 // if min larger than max then error
54 SourceException.throwIf(ArgumentUtils.compareNumbers(min, max) == 1, ctx.getStatementSourceReference(),
55 "Length constraint %s has descending order of boundaries; should be ascending.", singleRange);
56 SourceException.throwIf(boundaries.hasNext(), ctx.getStatementSourceReference(),
57 "Wrong number of boundaries in length constraint %s.", singleRange);
62 // some of intervals overlapping
63 InferenceException.throwIf(ranges.size() > 1
64 && ArgumentUtils.compareNumbers(min, Iterables.getLast(ranges).upperBound()) != 1,
65 ctx.getStatementSourceReference(), "Some of the length ranges in %s are not disjoint", value);
66 ranges.add(ValueRange.of(min, max));
73 public LengthStatement createDeclared(final StmtContext<List<ValueRange>, LengthStatement, ?> ctx) {
74 return new LengthStatementImpl(ctx);
78 public EffectiveStatement<List<ValueRange>, LengthStatement> createEffective(
79 final StmtContext<List<ValueRange>, LengthStatement, EffectiveStatement<List<ValueRange>,
80 LengthStatement>> ctx) {
81 return new LengthEffectiveStatementImpl(ctx);
85 protected SubstatementValidator getSubstatementValidator() {
86 return SUBSTATEMENT_VALIDATOR;
89 private static Number parseIntegerConstraintValue(final StmtContext<?, ?, ?> ctx, final String value) {
90 if ("max".equals(value)) {
91 return UnresolvedNumber.max();
93 if ("min".equals(value)) {
94 return UnresolvedNumber.min();
98 return new BigInteger(value);
99 } catch (final NumberFormatException e) {
100 throw new SourceException(ctx.getStatementSourceReference(), e, "Value %s is not a valid integer", value);