Convert length statement
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / length / LengthStatementSupport.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.length;
9
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.meta.DeclaredStatement;
18 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.LengthEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.LengthStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.UnresolvedNumber;
22 import org.opendaylight.yangtools.yang.model.api.stmt.ValueRange;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.ArgumentUtils;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29
30 public final class LengthStatementSupport
31         extends BaseStatementSupport<List<ValueRange>, LengthStatement, LengthEffectiveStatement> {
32     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(YangStmtMapping
33         .LENGTH)
34         .addOptional(YangStmtMapping.DESCRIPTION)
35         .addOptional(YangStmtMapping.ERROR_APP_TAG)
36         .addOptional(YangStmtMapping.ERROR_MESSAGE)
37         .addOptional(YangStmtMapping.REFERENCE)
38         .build();
39     private static final LengthStatementSupport INSTANCE = new LengthStatementSupport();
40
41     private LengthStatementSupport() {
42         super(YangStmtMapping.LENGTH, CopyPolicy.CONTEXT_INDEPENDENT);
43     }
44
45     public static LengthStatementSupport getInstance() {
46         return INSTANCE;
47     }
48
49     @Override
50     public ImmutableList<ValueRange> parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
51         final List<ValueRange> ranges = new ArrayList<>();
52
53         for (final String singleRange : ArgumentUtils.PIPE_SPLITTER.split(value)) {
54             final Iterator<String> boundaries = ArgumentUtils.TWO_DOTS_SPLITTER.split(singleRange).iterator();
55             final Number min = parseIntegerConstraintValue(ctx, boundaries.next());
56
57             final Number max;
58             if (boundaries.hasNext()) {
59                 max = parseIntegerConstraintValue(ctx, boundaries.next());
60
61                 // if min larger than max then error
62                 SourceException.throwIf(ArgumentUtils.compareNumbers(min, max) == 1, ctx.getStatementSourceReference(),
63                         "Length constraint %s has descending order of boundaries; should be ascending.", singleRange);
64                 SourceException.throwIf(boundaries.hasNext(), ctx.getStatementSourceReference(),
65                         "Wrong number of boundaries in length constraint %s.", singleRange);
66             } else {
67                 max = min;
68             }
69
70             // some of intervals overlapping
71             InferenceException.throwIf(ranges.size() > 1
72                 && ArgumentUtils.compareNumbers(min, Iterables.getLast(ranges).upperBound()) != 1,
73                         ctx.getStatementSourceReference(),  "Some of the length ranges in %s are not disjoint", value);
74             ranges.add(ValueRange.of(min, max));
75         }
76
77         return ImmutableList.copyOf(ranges);
78     }
79
80     @Override
81     protected SubstatementValidator getSubstatementValidator() {
82         return SUBSTATEMENT_VALIDATOR;
83     }
84
85     @Override
86     protected LengthStatement createDeclared(final StmtContext<List<ValueRange>, LengthStatement, ?> ctx,
87             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
88         return new RegularLengthStatement(ctx, substatements);
89     }
90
91     @Override
92     protected LengthStatement createEmptyDeclared(final StmtContext<List<ValueRange>, LengthStatement, ?> ctx) {
93         return new EmptyLengthStatement(ctx);
94     }
95
96     @Override
97     protected LengthEffectiveStatement createEffective(
98             final StmtContext<List<ValueRange>, LengthStatement, LengthEffectiveStatement> ctx,
99             final LengthStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
100         return new RegularLengthEffectiveStatement(declared, substatements);
101     }
102
103     @Override
104     protected LengthEffectiveStatement createEmptyEffective(
105             final StmtContext<List<ValueRange>, LengthStatement, LengthEffectiveStatement> ctx,
106             final LengthStatement declared) {
107         return new EmptyLengthEffectiveStatement(declared);
108     }
109
110     private static Number parseIntegerConstraintValue(final StmtContext<?, ?, ?> ctx, final String value) {
111         if ("max".equals(value)) {
112             return UnresolvedNumber.max();
113         }
114         if ("min".equals(value)) {
115             return UnresolvedNumber.min();
116         }
117
118         try {
119             return new BigInteger(value);
120         } catch (final NumberFormatException e) {
121             throw new SourceException(ctx.getStatementSourceReference(), e, "Value %s is not a valid integer", value);
122         }
123     }
124 }