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.type;
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;
29 final class BitsSpecificationSupport extends AbstractTypeSupport<BitsSpecification> {
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
31 SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.BIT).build();
33 BitsSpecificationSupport(final YangParserConfiguration config) {
34 super(config, SUBSTATEMENT_VALIDATOR);
38 protected BitsSpecification createDeclared(final BoundStmtCtx<QName> ctx,
39 final ImmutableList<DeclaredStatement<?>> substatements) {
40 if (substatements.isEmpty()) {
43 return new BitsSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
47 protected BitsSpecification attachDeclarationReference(final BitsSpecification stmt,
48 final DeclarationReference reference) {
49 return new RefBitsSpecification(stmt, reference);
53 protected EffectiveStatement<QName, BitsSpecification> createEffective(
54 final Current<QName, BitsSpecification> stmt,
55 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
56 if (substatements.isEmpty()) {
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);
72 effectivePos = Uint32.ZERO;
75 effectivePos = declaredPosition.orElseThrow();
78 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
79 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
80 highestPosition = bit.getPosition();
87 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
90 private static SourceException noBits(final CommonStmtCtx stmt) {
92 * https://www.rfc-editor.org/rfc/rfc7950#section-9.7.4:
94 * The "bit" statement, which is a substatement to the "type" statement,
95 * MUST be present if the type is "bits".
97 return new SourceException("At least one bit statement has to be present", stmt);