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.stmt.rfc6020.effective.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.rfc6020.util.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 public final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
26 implements TypeEffectiveStatement<TypeStatement> {
28 private final BitsTypeDefinition typeDefinition;
30 public BitsTypeEffectiveStatementImpl(
31 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
32 final BitsTypeDefinition baseType) {
35 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
37 final YangVersion yangVersion = ctx.getRootVersion();
38 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
39 if (stmt instanceof BitEffectiveStatementImpl) {
40 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
41 "Restricted bits type is allowed only in YANG 1.1 version.");
42 final BitEffectiveStatementImpl bitSubStmt = (BitEffectiveStatementImpl) stmt;
44 final long effectivePos;
45 if (bitSubStmt.getDeclaredPosition() == null) {
46 effectivePos = getBaseTypeBitPosition(bitSubStmt.getName(), baseType, ctx);
48 effectivePos = bitSubStmt.getDeclaredPosition();
51 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
52 } else if (stmt instanceof UnknownSchemaNode) {
53 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
57 typeDefinition = builder.build();
60 private static long getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
61 final StmtContext<?, ?, ?> ctx) {
62 for (Bit baseTypeBit : baseType.getBits()) {
63 if (bitName.equals(baseTypeBit.getName())) {
64 return baseTypeBit.getPosition();
68 throw new SourceException(ctx.getStatementSourceReference(),
69 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
74 public BitsTypeDefinition getTypeDefinition() {
75 return typeDefinition;