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.stmt.rfc6020.effective.type;
10 import javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
14 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.BitBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
24 public final class BitsSpecificationEffectiveStatementImpl extends
25 DeclaredEffectiveStatementBase<String, BitsSpecification> implements TypeEffectiveStatement<BitsSpecification> {
27 private final BitsTypeDefinition typeDefinition;
29 public BitsSpecificationEffectiveStatementImpl(
30 final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
33 final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
34 Long highestPosition = null;
35 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
36 if (stmt instanceof BitEffectiveStatementImpl) {
37 final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
39 final long effectivePos;
40 if (bitSubStmt.getDeclaredPosition() == null) {
41 if (highestPosition != null) {
42 SourceException.throwIf(highestPosition == 4294967295L, ctx.getStatementSourceReference(),
43 "Bit %s must have a position statement", bitSubStmt);
44 effectivePos = highestPosition + 1;
49 effectivePos = bitSubStmt.getDeclaredPosition();
52 final BitBuilder bitBuilder = BitBuilder.create(bitSubStmt.getPath(), effectivePos)
53 .setStatus(bitSubStmt.getStatus());
54 bitSubStmt.getDescription().ifPresent(bitBuilder::setDescription);
55 bitSubStmt.getReference().ifPresent(bitBuilder::setReference);
57 final Bit bit = bitBuilder.build();
58 SourceException.throwIf(bit.getPosition() < 0L && bit.getPosition() > 4294967295L,
59 ctx.getStatementSourceReference(), "Bit %s has illegal position", bit);
61 if (highestPosition == null || highestPosition < bit.getPosition()) {
62 highestPosition = bit.getPosition();
67 if (stmt instanceof UnknownEffectiveStatementImpl) {
68 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
72 typeDefinition = builder.build();
77 public BitsTypeDefinition getTypeDefinition() {
78 return typeDefinition;