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.ImmutableList;
11 import com.google.common.collect.Iterables;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.Iterator;
15 import java.util.List;
16 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
17 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.LengthStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
20 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractStatementSupport;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 public final class LengthStatementSupport
29 extends AbstractStatementSupport<List<ValueRange>, LengthStatement, LengthEffectiveStatement> {
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
32 .addOptional(YangStmtMapping.DESCRIPTION)
33 .addOptional(YangStmtMapping.ERROR_APP_TAG)
34 .addOptional(YangStmtMapping.ERROR_MESSAGE)
35 .addOptional(YangStmtMapping.REFERENCE)
37 private static final LengthStatementSupport INSTANCE = new LengthStatementSupport();
39 private LengthStatementSupport() {
40 super(YangStmtMapping.LENGTH, CopyPolicy.CONTEXT_INDEPENDENT);
43 public static LengthStatementSupport getInstance() {
48 public List<ValueRange> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
49 final List<ValueRange> ranges = new ArrayList<>();
51 for (final String singleRange : ArgumentUtils.PIPE_SPLITTER.split(value)) {
52 final Iterator<String> boundaries = ArgumentUtils.TWO_DOTS_SPLITTER.split(singleRange).iterator();
53 final Number min = parseIntegerConstraintValue(ctx, boundaries.next());
56 if (boundaries.hasNext()) {
57 max = parseIntegerConstraintValue(ctx, boundaries.next());
59 // if min larger than max then error
60 SourceException.throwIf(ArgumentUtils.compareNumbers(min, max) == 1, ctx.getStatementSourceReference(),
61 "Length constraint %s has descending order of boundaries; should be ascending.", singleRange);
62 SourceException.throwIf(boundaries.hasNext(), ctx.getStatementSourceReference(),
63 "Wrong number of boundaries in length constraint %s.", singleRange);
68 // some of intervals overlapping
69 InferenceException.throwIf(ranges.size() > 1
70 && ArgumentUtils.compareNumbers(min, Iterables.getLast(ranges).upperBound()) != 1,
71 ctx.getStatementSourceReference(), "Some of the length ranges in %s are not disjoint", value);
72 ranges.add(ValueRange.of(min, max));
75 return ImmutableList.copyOf(ranges);
79 public LengthStatement createDeclared(final StmtContext<List<ValueRange>, LengthStatement, ?> ctx) {
80 return new LengthStatementImpl(ctx);
84 public LengthEffectiveStatement createEffective(
85 final StmtContext<List<ValueRange>, LengthStatement, LengthEffectiveStatement> ctx) {
86 return new LengthEffectiveStatementImpl(ctx);
90 protected SubstatementValidator getSubstatementValidator() {
91 return SUBSTATEMENT_VALIDATOR;
94 private static Number parseIntegerConstraintValue(final StmtContext<?, ?, ?> ctx, final String value) {
95 if ("max".equals(value)) {
96 return UnresolvedNumber.max();
98 if ("min".equals(value)) {
99 return UnresolvedNumber.min();
103 return new BigInteger(value);
104 } catch (final NumberFormatException e) {
105 throw new SourceException(ctx.getStatementSourceReference(), e, "Value %s is not a valid integer", value);