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.EffectiveStmtCtx.Current;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
28 final class BitsSpecificationSupport
29 extends BaseStatementSupport<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> {
30 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR = SubstatementValidator.builder(
32 .addMultiple(YangStmtMapping.BIT)
35 BitsSpecificationSupport() {
36 super(YangStmtMapping.TYPE);
40 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
45 protected SubstatementValidator getSubstatementValidator() {
46 return SUBSTATEMENT_VALIDATOR;
50 protected BitsSpecification createDeclared(final StmtContext<String, BitsSpecification, ?> ctx,
51 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
52 return new BitsSpecificationImpl(ctx.coerceRawStatementArgument(), substatements);
56 protected BitsSpecification createEmptyDeclared(final StmtContext<String, BitsSpecification, ?> ctx) {
57 throw noBits(ctx.getStatementSourceReference());
61 protected EffectiveStatement<String, BitsSpecification> createEffective(
62 final Current<String, BitsSpecification> stmt,
63 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
64 if (substatements.isEmpty()) {
65 throw noBits(stmt.sourceReference());
68 final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(stmt.getSchemaPath());
69 Uint32 highestPosition = null;
70 for (final EffectiveStatement<?, ?> subStmt : substatements) {
71 if (subStmt instanceof BitEffectiveStatement) {
72 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) subStmt;
74 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
75 final Uint32 effectivePos;
76 if (declaredPosition.isEmpty()) {
77 if (highestPosition != null) {
78 SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition), stmt.sourceReference(),
79 "Bit %s must have a position statement", bitSubStmt);
80 effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
82 effectivePos = Uint32.ZERO;
85 effectivePos = declaredPosition.get();
88 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
89 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
90 highestPosition = bit.getPosition();
97 return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
100 private static SourceException noBits(final StatementSourceReference ref) {
102 * https://tools.ietf.org/html/rfc7950#section-9.7.4:
104 * The "bit" statement, which is a substatement to the "type" statement,
105 * MUST be present if the type is "bits".
107 return new SourceException("At least one bit statement has to be present", ref);