Cleanup DocumentedNode
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / BitsSpecificationEffectiveStatementImpl.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
9
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;
23
24 public final class BitsSpecificationEffectiveStatementImpl extends
25         DeclaredEffectiveStatementBase<String, BitsSpecification> implements TypeEffectiveStatement<BitsSpecification> {
26
27     private final BitsTypeDefinition typeDefinition;
28
29     public BitsSpecificationEffectiveStatementImpl(
30             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
31         super(ctx);
32
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;
38
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;
45                     } else {
46                         effectivePos = 0L;
47                     }
48                 } else {
49                     effectivePos = bitSubStmt.getDeclaredPosition();
50                 }
51
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);
56
57                 final Bit bit = bitBuilder.build();
58                 SourceException.throwIf(bit.getPosition() < 0L && bit.getPosition() > 4294967295L,
59                         ctx.getStatementSourceReference(), "Bit %s has illegal position", bit);
60
61                 if (highestPosition == null || highestPosition < bit.getPosition()) {
62                     highestPosition = bit.getPosition();
63                 }
64
65                 builder.addBit(bit);
66             }
67             if (stmt instanceof UnknownEffectiveStatementImpl) {
68                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
69             }
70         }
71
72         typeDefinition = builder.build();
73     }
74
75     @Nonnull
76     @Override
77     public BitsTypeDefinition getTypeDefinition() {
78         return typeDefinition;
79     }
80 }