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