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 / BitsTypeEffectiveStatementImpl.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
9 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.common.YangVersion;
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.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
17 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
19 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
20 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.bit.BitEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27         implements TypeEffectiveStatement<TypeStatement> {
28
29     private final BitsTypeDefinition typeDefinition;
30
31     BitsTypeEffectiveStatementImpl(
32             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
33             final BitsTypeDefinition baseType) {
34         super(ctx);
35
36         final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
37
38         final YangVersion yangVersion = ctx.getRootVersion();
39         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
40             if (stmt instanceof BitEffectiveStatementImpl) {
41                 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
42                         "Restricted bits type is allowed only in YANG 1.1 version.");
43                 final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
44
45                 final long effectivePos;
46                 if (bitSubStmt.getDeclaredPosition() == null) {
47                     effectivePos = getBaseTypeBitPosition(bitSubStmt.getName(), baseType, ctx);
48                 } else {
49                     effectivePos = bitSubStmt.getDeclaredPosition();
50                 }
51
52                 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
53             } else if (stmt instanceof UnknownSchemaNode) {
54                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
55             }
56         }
57
58         typeDefinition = builder.build();
59     }
60
61     private static long getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
62             final StmtContext<?, ?, ?> ctx) {
63         for (Bit baseTypeBit : baseType.getBits()) {
64             if (bitName.equals(baseTypeBit.getName())) {
65                 return baseTypeBit.getPosition();
66             }
67         }
68
69         throw new SourceException(ctx.getStatementSourceReference(),
70                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
71     }
72
73     @Nonnull
74     @Override
75     public BitsTypeDefinition getTypeDefinition() {
76         return typeDefinition;
77     }
78 }