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 java.util.Optional;
11 import org.eclipse.jdt.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.EnumEffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
17 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
18 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
20 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
21 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
22 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 final class EnumSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, EnumSpecification>
27 implements TypeEffectiveStatement<EnumSpecification> {
29 private final @NonNull EnumTypeDefinition typeDefinition;
31 EnumSpecificationEffectiveStatement(
32 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
35 final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
36 Integer highestValue = null;
37 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
38 if (stmt instanceof EnumEffectiveStatement) {
39 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
41 final Optional<Integer> declaredValue =
42 enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
43 final int effectiveValue;
44 if (declaredValue.isEmpty()) {
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 = declaredValue.orElseThrow();
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();
72 public EnumTypeDefinition getTypeDefinition() {
73 return typeDefinition;