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
9 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
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;
26 final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27 implements TypeEffectiveStatement<TypeStatement> {
29 private final BitsTypeDefinition typeDefinition;
31 BitsTypeEffectiveStatementImpl(
32 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
33 final BitsTypeDefinition baseType) {
36 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
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;
45 final long effectivePos;
46 if (bitSubStmt.getDeclaredPosition() == null) {
47 effectivePos = getBaseTypeBitPosition(bitSubStmt.getName(), baseType, ctx);
49 effectivePos = bitSubStmt.getDeclaredPosition();
52 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
53 } else if (stmt instanceof UnknownSchemaNode) {
54 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
58 typeDefinition = builder.build();
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();
69 throw new SourceException(ctx.getStatementSourceReference(),
70 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
75 public BitsTypeDefinition getTypeDefinition() {
76 return typeDefinition;