Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / BitsSpecificationEffectiveStatement.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.rfc7950.stmt.type;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
16 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
18 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
19 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.bit.BitEffectiveStatementImpl;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
24
25 @VisibleForTesting
26 // FIXME: hide this class
27 public final class BitsSpecificationEffectiveStatement extends
28         DeclaredEffectiveStatementBase<String, BitsSpecification> implements TypeEffectiveStatement<BitsSpecification> {
29
30     private final BitsTypeDefinition typeDefinition;
31
32     BitsSpecificationEffectiveStatement(
33             final StmtContext<String, BitsSpecification, EffectiveStatement<String, BitsSpecification>> ctx) {
34         super(ctx);
35
36         final BitsTypeBuilder builder = BaseTypes.bitsTypeBuilder(ctx.getSchemaPath().get());
37         Long highestPosition = null;
38         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
39             if (stmt instanceof BitEffectiveStatementImpl) {
40                 final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
41
42                 final long effectivePos;
43                 if (bitSubStmt.getDeclaredPosition() == null) {
44                     if (highestPosition != null) {
45                         SourceException.throwIf(highestPosition == 4294967295L, ctx.getStatementSourceReference(),
46                                 "Bit %s must have a position statement", bitSubStmt);
47                         effectivePos = highestPosition + 1;
48                     } else {
49                         effectivePos = 0L;
50                     }
51                 } else {
52                     effectivePos = bitSubStmt.getDeclaredPosition();
53                 }
54
55                 final Bit bit = EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos);
56                 SourceException.throwIf(bit.getPosition() < 0L && bit.getPosition() > 4294967295L,
57                         ctx.getStatementSourceReference(), "Bit %s has illegal position", bit);
58
59                 if (highestPosition == null || highestPosition < bit.getPosition()) {
60                     highestPosition = bit.getPosition();
61                 }
62
63                 builder.addBit(bit);
64             }
65             if (stmt instanceof UnknownSchemaNode) {
66                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
67             }
68         }
69
70         typeDefinition = builder.build();
71     }
72
73     @Nonnull
74     @Override
75     public BitsTypeDefinition getTypeDefinition() {
76         return typeDefinition;
77     }
78 }