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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import java.util.Optional;
11 import org.eclipse.jdt.annotation.NonNull;
12 import org.opendaylight.yangtools.yang.common.Uint32;
13 import org.opendaylight.yangtools.yang.common.YangVersion;
14 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.BitEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
21 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
22 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
23 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
25 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
28 implements TypeEffectiveStatement<TypeStatement> {
30 private final @NonNull BitsTypeDefinition typeDefinition;
32 BitsTypeEffectiveStatementImpl(
33 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
34 final BitsTypeDefinition baseType) {
37 final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
39 final YangVersion yangVersion = ctx.getRootVersion();
40 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
41 if (stmt instanceof BitEffectiveStatement) {
42 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
43 "Restricted bits type is allowed only in YANG 1.1 version.");
44 final BitEffectiveStatement bitSubStmt = (BitEffectiveStatement) stmt;
46 // FIXME: this looks like a duplicate of BitsSpecificationEffectiveStatement
47 final Optional<Uint32> declared = bitSubStmt.getDeclaredPosition();
48 final Uint32 effectivePos;
49 if (declared.isEmpty()) {
50 effectivePos = getBaseTypeBitPosition(bitSubStmt.argument(), baseType, ctx);
52 effectivePos = declared.get();
55 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
56 } else if (stmt instanceof UnknownSchemaNode) {
57 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
61 typeDefinition = builder.build();
64 private static Uint32 getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
65 final StmtContext<?, ?, ?> ctx) {
66 for (Bit baseTypeBit : baseType.getBits()) {
67 if (bitName.equals(baseTypeBit.getName())) {
68 return baseTypeBit.getPosition();
72 throw new SourceException(ctx.getStatementSourceReference(),
73 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
77 public BitsTypeDefinition getTypeDefinition() {
78 return typeDefinition;