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