Bug 6887: [Yang 1.1] Allow enumerations and bits to be subtyped
[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 Bit b = BitBuilder.create(bitSubStmt.getPath(), effectivePos)
53                         .setDescription(bitSubStmt.getDescription()).setReference(bitSubStmt.getReference())
54                         .setStatus(bitSubStmt.getStatus()).setUnknownSchemaNodes(bitSubStmt.getUnknownSchemaNodes())
55                         .build();
56
57                 builder.addBit(b);
58             } else if (stmt instanceof UnknownEffectiveStatementImpl) {
59                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
60             }
61         }
62
63         typeDefinition = builder.build();
64     }
65
66     private static long getBaseTypeBitPosition(final String bitName, final BitsTypeDefinition baseType,
67             final StmtContext<?, ?, ?> ctx) {
68         for (Bit baseTypeBit : baseType.getBits()) {
69             if (bitName.equals(baseTypeBit.getName())) {
70                 return baseTypeBit.getPosition();
71             }
72         }
73
74         throw new SourceException(ctx.getStatementSourceReference(),
75                 "Bit '%s' is not a subset of its base bits type %s.", bitName, baseType.getQName());
76     }
77
78     @Nonnull
79     @Override
80     public BitsTypeDefinition getTypeDefinition() {
81         return typeDefinition;
82     }
83 }