Cleanup DocumentedNode
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / 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.stmt.rfc6020.effective.type;
10
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;
25
26 public final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27         implements TypeEffectiveStatement<TypeStatement> {
28
29     private final BitsTypeDefinition typeDefinition;
30
31     public 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                 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);
59             }
60         }
61
62         typeDefinition = builder.build();
63     }
64
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();
70             }
71         }
72
73         throw new SourceException(ctx.getStatementSourceReference(),
74                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
75     }
76
77     @Nonnull
78     @Override
79     public BitsTypeDefinition getTypeDefinition() {
80         return typeDefinition;
81     }
82 }