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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
10 import com.google.common.annotations.VisibleForTesting;
11 import javax.annotation.Nonnull;
12 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
13 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
16 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
17 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
18 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
19 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.enum_.EnumEffectiveStatementImpl;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 // FIXME: hide this class
27 public final class EnumSpecificationEffectiveStatement extends
28 DeclaredEffectiveStatementBase<String, EnumSpecification> implements
29 TypeEffectiveStatement<EnumSpecification> {
31 private final EnumTypeDefinition typeDefinition;
33 EnumSpecificationEffectiveStatement(
34 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
37 final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
38 Integer highestValue = null;
39 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
40 if (stmt instanceof EnumEffectiveStatementImpl) {
41 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
43 final int effectiveValue;
44 if (enumSubStmt.getDeclaredValue() == null) {
45 if (highestValue != null) {
46 SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
47 "Enum '%s' must have a value statement", enumSubStmt);
48 effectiveValue = highestValue + 1;
53 effectiveValue = enumSubStmt.getDeclaredValue();
56 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
57 if (highestValue == null || highestValue < pair.getValue()) {
58 highestValue = pair.getValue();
61 builder.addEnum(pair);
63 if (stmt instanceof UnknownSchemaNode) {
64 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
68 typeDefinition = builder.build();
73 public EnumTypeDefinition getTypeDefinition() {
74 return typeDefinition;