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.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;
26 final class BitsSpecificationSupport
27 extends BaseStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
28 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
30 .addMultiple(YangStmtMapping.BIT)
33 BitsSpecificationSupport() {
34 super(YangStmtMapping.TYPE);
38 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
43 protected SubstatementValidator getSubstatementValidator() {
44 return SUBSTATEMENT_VALIDATOR;
48 protected BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx,
49 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
50 return new BitsSpecificationImpl(ctx, substatements);
54 protected BitsSpecification createEmptyDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
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;
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);
76 effectivePos = Uint32.ZERO;
79 effectivePos = declaredPosition.get();
82 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
83 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
84 highestPosition = bit.getPosition();
91 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
95 protected EffectiveStatement<String, BitsSpecification> createEmptyEffective(
96 final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx,
97 final BitsSpecification declared) {
101 private static SourceException noBits(final StmtContext<?, ?, ?> ctx) {
103 * https://tools.ietf.org/html/rfc7950#section-9.7.4:
105 * The "bit" statement, which is a substatement to the "type" statement,
106 * MUST be present if the type is "bits".
108 return new SourceException("At least one bit statement has to be present", ctx.getStatementSourceReference());