2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.collect.ImmutableList;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
13 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
14 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
15 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
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.EnumPair;
19 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
20 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
21 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseStatementSupport;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
26 final class EnumSpecificationSupport
27 extends BaseStatementSupport<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> {
28 private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
29 SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.ENUM).build();
31 EnumSpecificationSupport() {
32 super(YangStmtMapping.TYPE);
36 public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
41 protected SubstatementValidator getSubstatementValidator() {
42 return SUBSTATEMENT_VALIDATOR;
46 protected EnumSpecification createDeclared(final StmtContext<String, EnumSpecification, ?> ctx,
47 final ImmutableList<? extends DeclaredStatement<?>> substatements) {
48 return new EnumSpecificationImpl(ctx, substatements);
52 protected EnumSpecification createEmptyDeclared(final StmtContext<String, EnumSpecification, ?> ctx) {
57 protected EffectiveStatement<String, EnumSpecification> createEffective(
58 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
59 final EnumSpecification declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
60 final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
61 Integer highestValue = null;
62 for (final EffectiveStatement<?, ?> stmt : substatements) {
63 if (stmt instanceof EnumEffectiveStatement) {
64 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) stmt;
66 final Optional<Integer> declaredValue =
67 enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
68 final int effectiveValue;
69 if (declaredValue.isEmpty()) {
70 if (highestValue != null) {
71 SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
72 "Enum '%s' must have a value statement", enumSubStmt);
73 effectiveValue = highestValue + 1;
78 effectiveValue = declaredValue.orElseThrow();
81 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
82 if (highestValue == null || highestValue < pair.getValue()) {
83 highestValue = pair.getValue();
86 builder.addEnum(pair);
90 return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
94 protected EffectiveStatement<String, EnumSpecification> createEmptyEffective(
95 final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
96 final EnumSpecification declared) {
100 private static SourceException noEnum(final StmtContext<?, ?, ?> ctx) {
102 * https://tools.ietf.org/html/rfc7950#section-9.6.4
104 * The "enum" statement, which is a substatement to the "type"
105 * statement, MUST be present if the type is "enumeration".
107 return new SourceException("At least one enum statement has to be present", ctx.getStatementSourceReference());