YANGTOOLS-706: Split out yang-parser-rfc7950
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EnumTypeEffectiveStatementImpl.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.rfc7950.stmt.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.EnumTypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
19 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
20 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.enum_.EnumEffectiveStatementImpl;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25
26 final class EnumTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27         implements TypeEffectiveStatement<TypeStatement> {
28
29     private final EnumTypeDefinition typeDefinition;
30
31     EnumTypeEffectiveStatementImpl(
32             final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
33             final EnumTypeDefinition baseType) {
34         super(ctx);
35
36         final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
37                 ctx.getSchemaPath().get());
38
39         final YangVersion yangVersion = ctx.getRootVersion();
40         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
41             if (stmt instanceof EnumEffectiveStatementImpl) {
42                 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
43                         "Restricted enumeration type is allowed only in YANG 1.1 version.");
44
45                 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
46
47                 final int effectiveValue;
48                 if (enumSubStmt.getDeclaredValue() == null) {
49                     effectiveValue = getBaseTypeEnumValue(enumSubStmt.getName(), baseType, ctx);
50                 } else {
51                     effectiveValue = enumSubStmt.getDeclaredValue();
52                 }
53
54                 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
55             } else if (stmt instanceof UnknownSchemaNode) {
56                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
57             }
58         }
59
60         typeDefinition = builder.build();
61     }
62
63     private static int getBaseTypeEnumValue(final String enumName, final EnumTypeDefinition baseType,
64             final StmtContext<?, ?, ?> ctx) {
65         for (EnumPair baseTypeEnumPair : baseType.getValues()) {
66             if (enumName.equals(baseTypeEnumPair.getName())) {
67                 return baseTypeEnumPair.getValue();
68             }
69         }
70
71         throw new SourceException(ctx.getStatementSourceReference(),
72                 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
73     }
74
75     @Nonnull
76     @Override
77     public EnumTypeDefinition getTypeDefinition() {
78         return typeDefinition;
79     }
80 }