Migrate RFC references to rfc-editor.org
[yangtools.git] / parser / 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 org.opendaylight.yangtools.yang.common.QName;
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.DeclarationReference;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
16 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
20 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.ri.type.BitsTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.BoundStmtCtx;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28
29 final class BitsSpecificationSupport extends AbstractTypeSupport<BitsSpecification> {
30     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
31         SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.BIT).build();
32
33     BitsSpecificationSupport(final YangParserConfiguration config) {
34         super(config, SUBSTATEMENT_VALIDATOR);
35     }
36
37     @Override
38     protected BitsSpecification createDeclared(final BoundStmtCtx<QName> ctx,
39             final ImmutableList<DeclaredStatement<?>> substatements) {
40         if (substatements.isEmpty()) {
41             throw noBits(ctx);
42         }
43         return new BitsSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
44     }
45
46     @Override
47     protected BitsSpecification attachDeclarationReference(final BitsSpecification stmt,
48             final DeclarationReference reference) {
49         return new RefBitsSpecification(stmt, reference);
50     }
51
52     @Override
53     protected EffectiveStatement<QName, BitsSpecification> createEffective(
54             final Current<QName, BitsSpecification> stmt,
55             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
56         if (substatements.isEmpty()) {
57             throw noBits(stmt);
58         }
59
60         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(stmt.argumentAsTypeQName());
61         Uint32 highestPosition = null;
62         for (var subStmt : substatements) {
63             if (subStmt instanceof BitEffectiveStatement bitSubStmt) {
64                 var declaredPosition = bitSubStmt.findDeclaredPosition();
65                 final Uint32 effectivePos;
66                 if (declaredPosition.isEmpty()) {
67                     if (highestPosition != null) {
68                         SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition), stmt,
69                             "Bit %s must have a position statement", bitSubStmt);
70                         effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
71                     } else {
72                         effectivePos = Uint32.ZERO;
73                     }
74                 } else {
75                     effectivePos = declaredPosition.orElseThrow();
76                 }
77
78                 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
79                 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
80                     highestPosition = bit.getPosition();
81                 }
82
83                 builder.addBit(bit);
84             }
85         }
86
87         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
88     }
89
90     private static SourceException noBits(final CommonStmtCtx stmt) {
91         /*
92          *  https://www.rfc-editor.org/rfc/rfc7950#section-9.7.4:
93          *
94          *     The "bit" statement, which is a substatement to the "type" statement,
95          *     MUST be present if the type is "bits".
96          */
97         return new SourceException("At least one bit statement has to be present", stmt);
98     }
99 }