fce5ad094b79f659830e8b7daf6bb8c9b63482bd
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EnumSpecificationSupport.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.type;
9
10 import com.google.common.collect.ImmutableList;
11 import java.util.Optional;
12 import org.opendaylight.yangtools.yang.common.QName;
13 import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
14 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
15 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
16 import org.opendaylight.yangtools.yang.model.api.stmt.EnumEffectiveStatement;
17 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
18 import org.opendaylight.yangtools.yang.model.api.stmt.ValueEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangParserConfiguration;
21 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
22 import org.opendaylight.yangtools.yang.model.ri.type.EnumerationTypeBuilder;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.CommonStmtCtx;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.EffectiveStmtCtx.Current;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.SubstatementValidator;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28
29 final class EnumSpecificationSupport extends AbstractTypeSupport<EnumSpecification> {
30     private static final SubstatementValidator SUBSTATEMENT_VALIDATOR =
31             SubstatementValidator.builder(YangStmtMapping.TYPE).addMultiple(YangStmtMapping.ENUM).build();
32
33     EnumSpecificationSupport(final YangParserConfiguration config) {
34         super(config);
35     }
36
37     @Override
38     protected SubstatementValidator getSubstatementValidator() {
39         return SUBSTATEMENT_VALIDATOR;
40     }
41
42     @Override
43     protected EnumSpecification createDeclared(final StmtContext<QName, EnumSpecification, ?> ctx,
44             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
45         if (substatements.isEmpty()) {
46             throw noEnum(ctx);
47         }
48         return new EnumSpecificationImpl(ctx.getRawArgument(), ctx.getArgument(), substatements);
49     }
50
51     @Override
52     protected EffectiveStatement<QName, EnumSpecification> createEffective(
53             final Current<QName, EnumSpecification> stmt,
54             final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
55         if (substatements.isEmpty()) {
56             throw noEnum(stmt);
57         }
58
59         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(stmt.argumentAsTypeQName());
60         Integer highestValue = null;
61         for (final EffectiveStatement<?, ?> subStmt : substatements) {
62             if (subStmt instanceof EnumEffectiveStatement) {
63                 final EnumEffectiveStatement enumSubStmt = (EnumEffectiveStatement) subStmt;
64
65                 final Optional<Integer> declaredValue =
66                         enumSubStmt.findFirstEffectiveSubstatementArgument(ValueEffectiveStatement.class);
67                 final int effectiveValue;
68                 if (declaredValue.isEmpty()) {
69                     if (highestValue != null) {
70                         SourceException.throwIf(highestValue == 2147483647, stmt,
71                             "Enum '%s' must have a value statement", enumSubStmt);
72                         effectiveValue = highestValue + 1;
73                     } else {
74                         effectiveValue = 0;
75                     }
76                 } else {
77                     effectiveValue = declaredValue.orElseThrow();
78                 }
79
80                 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
81                 if (highestValue == null || highestValue < pair.getValue()) {
82                     highestValue = pair.getValue();
83                 }
84
85                 builder.addEnum(pair);
86             }
87         }
88
89         return new TypeEffectiveStatementImpl<>(stmt.declared(), substatements, builder);
90     }
91
92     private static SourceException noEnum(final CommonStmtCtx stmt) {
93         /*
94          *  https://tools.ietf.org/html/rfc7950#section-9.6.4
95          *
96          *     The "enum" statement, which is a substatement to the "type"
97          *     statement, MUST be present if the type is "enumeration".
98          */
99         return new SourceException("At least one enum statement has to be present", stmt);
100     }
101
102 }