2 * Copyright (c) 2015 Cisco Systems, Inc. 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 java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.common.Uint32;
13 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
20 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 final class BitsSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, BitsSpecification>
27 implements TypeEffectiveStatement<BitsSpecification> {
29 private final @NonNull BitsTypeDefinition typeDefinition;
31 BitsSpecificationEffectiveStatement(
32 final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
35 final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
36 Uint32 highestPosition = null;
37 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
38 if (stmt instanceof BitEffectiveStatement) {
39 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
41 final Optional<Uint32> declaredPosition = bitSubStmt.getDeclaredPosition();
42 final Uint32 effectivePos;
43 if (declaredPosition.isEmpty()) {
44 if (highestPosition != null) {
45 SourceException.throwIf(Uint32.MAX_VALUE.equals(highestPosition),
46 ctx.getStatementSourceReference(), "Bit %s must have a position statement", bitSubStmt);
47 effectivePos = Uint32.fromIntBits(highestPosition.intValue() + 1);
49 effectivePos = Uint32.ZERO;
52 effectivePos = declaredPosition.get();
55 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
56 if (highestPosition == null || highestPosition.compareTo(bit.getPosition()) < 0) {
57 highestPosition = bit.getPosition();
62 if (stmt instanceof UnknownSchemaNode) {
63 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
67 typeDefinition = builder.build();
71 public BitsTypeDefinition getTypeDefinition() {
72 return typeDefinition;