Migrate BitsSpecificationSupport
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / BitsSpecificationSupport.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.type;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.Uint32;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
19 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
20 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 final class BitsSpecificationSupport
27         extends BaseStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
28     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
29         YangStmtMapping.TYPE)
30         .addMultiple(YangStmtMapping.BIT)
31         .build();
32
33     BitsSpecificationSupport() {
34         super(YangStmtMapping.TYPE);
35     }
36
37     @Override
38     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
39         return value;
40     }
41
42     @Override
43     protected SubstatementValidator getSubstatementValidator() {
44         return SUBSTATEMENT_VALIDATOR;
45     }
46
47     @Override
48     protected BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx,
49             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
50         return new BitsSpecificationImpl(ctx, substatements);
51     }
52
53     @Override
54     protected BitsSpecification createEmptyDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
55         throw noBits(ctx);
56     }
57
58     @Override
59     protected EffectiveStatement<String, BitsSpecification> createEffective(
60             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
61             final BitsSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
63         Uint32 highestPosition = null;
64         for (final EffectiveStatement<?, ?> stmt : substatements) {
65             if (stmt instanceof BitEffectiveStatement) {
66                 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
67
68                 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
69                 final Uint32 effectivePos;
70                 if (declaredPosition.isEmpty()) {
71                     if (highestPosition != null) {
72                         SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
73                             ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
74                         effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
75                     } else {
76                         effectivePos = Uint32.ZERO;
77                     }
78                 } else {
79                     effectivePos = declaredPosition.get();
80                 }
81
82                 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
83                 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
84                     highestPosition = bit.getPosition();
85                 }
86
87                 builder.addBit(bit);
88             }
89         }
90
91         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
92     }
93
94     @Override
95     protected EffectiveStatement<String, BitsSpecification> createEmptyEffective(
96             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
97             final BitsSpecification declared) {
98         throw noBits(ctx);
99     }
100
101     private static SourceException noBits(final StmtContext<?, ?, ?> ctx) {
102         /*
103          *  https://tools.ietf.org/html/rfc7950#section-9.7.4:
104          *
105          *     The "bit" statement, which is a substatement to the "type" statement,
106          *     MUST be present if the type is "bits".
107          */
108         return new SourceException("At least one bit statement has to be present", ctx.getStatementSourceReference());
109     }
110 }