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