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 org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
11 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
13 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
15 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
16 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
17 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
23 public final class EnumSpecificationEffectiveStatementImpl extends
24 DeclaredEffectiveStatementBase<String, EnumSpecification> implements
25 TypeEffectiveStatement<EnumSpecification> {
27 private final EnumTypeDefinition typeDefinition;
29 public EnumSpecificationEffectiveStatementImpl(
30 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
33 final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
34 Integer highestValue = null;
35 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
36 if (stmt instanceof EnumEffectiveStatementImpl) {
37 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
39 final int effectiveValue;
40 if (enumSubStmt.getDeclaredValue() == null) {
41 if (highestValue != null) {
42 SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
43 "Enum '%s' must have a value statement", enumSubStmt);
44 effectiveValue = highestValue + 1;
49 effectiveValue = enumSubStmt.getDeclaredValue();
52 final EnumPair p = EnumPairBuilder.create(enumSubStmt.getName(), effectiveValue)
53 .setDescription(enumSubStmt.getDescription()).setReference(enumSubStmt.getReference())
54 .setStatus(enumSubStmt.getStatus()).setUnknownSchemaNodes(enumSubStmt.getUnknownSchemaNodes())
57 if (highestValue == null || highestValue < p.getValue()) {
58 highestValue = p.getValue();
63 if (stmt instanceof UnknownEffectiveStatementImpl) {
64 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
68 typeDefinition = builder.build();
72 public EnumTypeDefinition getTypeDefinition() {
73 return typeDefinition;