2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.type;
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;
25 public final class EnumTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
26 implements TypeEffectiveStatement<TypeStatement> {
28 private final EnumTypeDefinition typeDefinition;
30 public EnumTypeEffectiveStatementImpl(
31 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
32 final EnumTypeDefinition baseType) {
35 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
36 ctx.getSchemaPath().get());
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.");
44 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
46 final int effectiveValue;
47 if (enumSubStmt.getDeclaredValue() == null) {
48 effectiveValue = getBaseTypeEnumValue(enumSubStmt.getName(), baseType, ctx);
50 effectiveValue = enumSubStmt.getDeclaredValue();
53 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
54 } else if (stmt instanceof UnknownSchemaNode) {
55 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
59 typeDefinition = builder.build();
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();
70 throw new SourceException(ctx.getStatementSourceReference(),
71 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
76 public EnumTypeDefinition getTypeDefinition() {
77 return typeDefinition;