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.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
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.BitBuilder;
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.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
26 public final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27 implements TypeEffectiveStatement<TypeStatement> {
29 private final BitsTypeDefinition typeDefinition;
31 public 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 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 builder.addBit(bitBuilder.build());
57 } else if (stmt instanceof UnknownEffectiveStatementImpl) {
58 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
62 typeDefinition = builder.build();
65 private static long getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
66 final StmtContext<?, ?, ?> ctx) {
67 for (Bit baseTypeBit : baseType.getBits()) {
68 if (bitName.equals(baseTypeBit.getName())) {
69 return baseTypeBit.getPosition();
73 throw new SourceException(ctx.getStatementSourceReference(),
74 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
79 public BitsTypeDefinition getTypeDefinition() {
80 return typeDefinition;