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 java.util.Optional;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.opendaylight.yangtools.yang.common.YangVersion;
14 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement;
19 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
22 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
23 import org.opendaylight.yangtools.yang.model.util.type.RestrictedTypes;
24 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 final class EnumTypeEffectiveStatementImpl extends DeclaredEffectiveStatementBase<String, TypeStatement>
29 implements TypeEffectiveStatement<TypeStatement> {
31 private final @NonNull EnumTypeDefinition typeDefinition;
33 EnumTypeEffectiveStatementImpl(
34 final StmtContext<String, TypeStatement, EffectiveStatement<String, TypeStatement>> ctx,
35 final EnumTypeDefinition baseType) {
38 final EnumerationTypeBuilder builder = RestrictedTypes.newEnumerationBuilder(baseType,
39 ctx.getSchemaPath().get());
41 final YangVersion yangVersion = ctx.getRootVersion();
42 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
43 if (stmt instanceof EnumEffectiveStatement) {
44 SourceException.throwIf(yangVersion != YangVersion.VERSION_1_1, ctx.getStatementSourceReference(),
45 "Restricted enumeration type is allowed only in YANG 1.1 version.");
47 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
48 final Optional<Integer> declaredValue =
49 enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
50 final int effectiveValue;
51 if (declaredValue.isEmpty()) {
52 effectiveValue = getBaseTypeEnumValue(enumSubStmt.getDeclared().rawArgument(), baseType, ctx);
54 effectiveValue = declaredValue.orElseThrow();
57 builder.addEnum(EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue));
58 } else if (stmt instanceof UnknownSchemaNode) {
59 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
63 typeDefinition = builder.build();
66 private static int getBaseTypeEnumValue(final String enumName, final EnumTypeDefinition baseType,
67 final StmtContext<?, ?, ?> ctx) {
68 for (EnumPair baseTypeEnumPair : baseType.getValues()) {
69 if (enumName.equals(baseTypeEnumPair.getName())) {
70 return baseTypeEnumPair.getValue();
74 throw new SourceException(ctx.getStatementSourceReference(),
75 "Enum '%s' is not a subset of its base enumeration type %s.", enumName, baseType.getQName());
79 public EnumTypeDefinition getTypeDefinition() {
80 return typeDefinition;