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 java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.common.Uint32;
14 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
15 import org.opendaylight.yangtools.yang.model.api.meta.DeclarationReference;
16 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
17 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
21 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
22 import org.opendaylight.yangtools.yang.model.ri.type.BitsTypeBuilder;
23 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
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.StmtContext;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
30 final class BitsSpecificationSupport extends AbstractTypeSupport<BitsSpecification> {
31 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
32 SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.BIT).build();
34 BitsSpecificationSupport(final YangParserConfiguration config) {
39 protected SubstatementValidator getSubstatementValidator() {
40 return SUBSTATEMENT_VALIDATOR;
44 protected BitsSpecification createDeclared(final StmtContext<QName, BitsSpecification, ?> ctx,
45 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
46 if (substatements.isEmpty()) {
49 return new BitsSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
53 protected BitsSpecification attachDeclarationReference(final BitsSpecification stmt,
54 final DeclarationReference reference) {
55 return new RefBitsSpecification(stmt, reference);
59 protected EffectiveStatement<QName, BitsSpecification> createEffective(
60 final Current<QName, BitsSpecification> stmt,
61 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
62 if (substatements.isEmpty()) {
66 final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(stmt.argumentAsTypeQName());
67 Uint32 highestPosition = null;
68 for (final EffectiveStatement<?, ?> subStmt : substatements) {
69 if (subStmt instanceof BitEffectiveStatement) {
70 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) subStmt;
72 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
73 final Uint32 effectivePos;
74 if (declaredPosition.isEmpty()) {
75 if (highestPosition != null) {
76 SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition), stmt,
77 "Bit %s must have a position statement", bitSubStmt);
78 effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
80 effectivePos = Uint32.ZERO;
83 effectivePos = declaredPosition.get();
86 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
87 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
88 highestPosition = bit.getPosition();
95 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
98 private static SourceException noBits(final CommonStmtCtx stmt) {
100 * https://tools.ietf.org/html/rfc7950#section-9.7.4:
102 * The "bit" statement, which is a substatement to the "type" statement,
103 * MUST be present if the type is "bits".
105 return new SourceException("At least one bit statement has to be present", stmt);