YANGTOOLS-706: Split up base utility classes into rfc6020.util
[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.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;
24
25 public final class BitsTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
26         implements TypeEffectiveStatement<TypeStatement> {
27
28     private final BitsTypeDefinition typeDefinition;
29
30     public BitsTypeEffectiveStatementImpl(
31             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
32             final BitsTypeDefinition baseType) {
33         super(ctx);
34
35         final BitsTypeBuilder builder = RestrictedTypes.newBitsBuilder(baseType, ctx.getSchemaPath().get());
36
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;
43
44                 final long effectivePos;
45                 if (bitSubStmt.getDeclaredPosition() == null) {
46                     effectivePos = getBaseTypeBitPosition(bitSubStmt.getName(), baseType, ctx);
47                 } else {
48                     effectivePos = bitSubStmt.getDeclaredPosition();
49                 }
50
51                 builder.addBit(EffectiveTypeUtil.buildBit(bitSubStmt, effectivePos));
52             } else if (stmt instanceof UnknownSchemaNode) {
53                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
54             }
55         }
56
57         typeDefinition = builder.build();
58     }
59
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();
65             }
66         }
67
68         throw new SourceException(ctx.getStatementSourceReference(),
69                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
70     }
71
72     @Nonnull
73     @Override
74     public BitsTypeDefinition getTypeDefinition() {
75         return typeDefinition;
76     }
77 }