Migrate EnumSpecificationSupport
[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.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;
25
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();
30
31     EnumSpecificationSupport() {
32         super(YangStmtMapping.TYPE);
33     }
34
35     @Override
36     public String parseArgumentValue(final StmtContext<?, ?, ?> ctx, final String value) {
37         return value;
38     }
39
40     @Override
41     protected SubstatementValidator getSubstatementValidator() {
42         return SUBSTATEMENT_VALIDATOR;
43     }
44
45     @Override
46     protected EnumSpecification createDeclared(final StmtContext<String, EnumSpecification, ?> ctx,
47             final ImmutableList<? extends DeclaredStatement<?>> substatements) {
48         return new EnumSpecificationImpl(ctx, substatements);
49     }
50
51     @Override
52     protected EnumSpecification createEmptyDeclared(final StmtContext<String, EnumSpecification, ?> ctx) {
53         throw noEnum(ctx);
54     }
55
56     @Override
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;
65
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;
74                     } else {
75                         effectiveValue = 0;
76                     }
77                 } else {
78                     effectiveValue = declaredValue.orElseThrow();
79                 }
80
81                 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
82                 if (highestValue == null || highestValue < pair.getValue()) {
83                     highestValue = pair.getValue();
84                 }
85
86                 builder.addEnum(pair);
87             }
88         }
89
90         return new TypeEffectiveStatementImpl<>(declared, substatements, builder);
91     }
92
93     @Override
94     protected EffectiveStatement<String, EnumSpecification> createEmptyEffective(
95             final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx,
96             final EnumSpecification declared) {
97         throw noEnum(ctx);
98     }
99
100     private static SourceException noEnum(final StmtContext<?, ?, ?> ctx) {
101         /*
102          *  https://tools.ietf.org/html/rfc7950#section-9.6.4
103          *
104          *     The "enum" statement, which is a substatement to the "type"
105          *     statement, MUST be present if the type is "enumeration".
106          */
107         return new SourceException("At least one enum statement has to be present", ctx.getStatementSourceReference());
108     }
109
110 }