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.stmt.rfc6020.effective.type;
10 import javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
14 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
24 public final class EnumSpecificationEffectiveStatementImpl extends
25 DeclaredEffectiveStatementBase<String, EnumSpecification> implements
26 TypeEffectiveStatement<EnumSpecification> {
28 private final EnumTypeDefinition typeDefinition;
30 public EnumSpecificationEffectiveStatementImpl(
31 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
34 final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
35 Integer highestValue = null;
36 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
37 if (stmt instanceof EnumEffectiveStatementImpl) {
38 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
40 final int effectiveValue;
41 if (enumSubStmt.getDeclaredValue() == null) {
42 if (highestValue != null) {
43 SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
44 "Enum '%s' must have a value statement", enumSubStmt);
45 effectiveValue = highestValue + 1;
50 effectiveValue = enumSubStmt.getDeclaredValue();
53 final EnumPairBuilder pairBuilder = EnumPairBuilder.create(enumSubStmt.getName(), effectiveValue)
54 .setStatus(enumSubStmt.getStatus()).setUnknownSchemaNodes(enumSubStmt.getUnknownSchemaNodes());
55 enumSubStmt.getDescription().ifPresent(pairBuilder::setDescription);
56 enumSubStmt.getReference().ifPresent(pairBuilder::setReference);
58 final EnumPair pair = pairBuilder.build();
59 if (highestValue == null || highestValue < pair.getValue()) {
60 highestValue = pair.getValue();
63 builder.addEnum(pair);
65 if (stmt instanceof UnknownEffectiveStatementImpl) {
66 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
70 typeDefinition = builder.build();
75 public EnumTypeDefinition getTypeDefinition() {
76 return typeDefinition;