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.rfc7950.stmt.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.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;
26 final class EnumTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
27 implements TypeEffectiveStatement<TypeStatement> {
29 private final EnumTypeDefinition typeDefinition;
31 EnumTypeEffectiveStatementImpl(
32 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
33 final EnumTypeDefinition baseType) {
36 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
37 ctx.getSchemaPath().get());
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.");
45 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
47 final int effectiveValue;
48 if (enumSubStmt.getDeclaredValue() == null) {
49 effectiveValue = getBaseTypeEnumValue(enumSubStmt.getName(), baseType, ctx);
51 effectiveValue = enumSubStmt.getDeclaredValue();
54 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
55 } else if (stmt instanceof UnknownSchemaNode) {
56 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
60 typeDefinition = builder.build();
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();
71 throw new SourceException(ctx.getStatementSourceReference(),
72 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
77 public EnumTypeDefinition getTypeDefinition() {
78 return typeDefinition;