f645b70d21444ae0d1bee21c515337492e4ac7c8
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / type / EnumSpecificationEffectiveStatement.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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 org.eclipse.jdt.annotation.NonNull;
11 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
12 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
14 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
15 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
16 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
17 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
18 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.DeclaredEffectiveStatementBase;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.enum_.EnumEffectiveStatementImpl;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23
24 final class EnumSpecificationEffectiveStatement extends DeclaredEffectiveStatementBase<String, EnumSpecification>
25         implements TypeEffectiveStatement<EnumSpecification> {
26
27     private final @NonNull EnumTypeDefinition typeDefinition;
28
29     EnumSpecificationEffectiveStatement(
30             final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
31         super(ctx);
32
33         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
34         Integer highestValue = null;
35         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
36             if (stmt instanceof EnumEffectiveStatementImpl) {
37                 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
38
39                 final int effectiveValue;
40                 if (enumSubStmt.getDeclaredValue() == null) {
41                     if (highestValue != null) {
42                         SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
43                                 "Enum '%s' must have a value statement", enumSubStmt);
44                         effectiveValue = highestValue + 1;
45                     } else {
46                         effectiveValue = 0;
47                     }
48                 } else {
49                     effectiveValue = enumSubStmt.getDeclaredValue();
50                 }
51
52                 final EnumPair pair = EffectiveTypeUtil.buildEnumPair(enumSubStmt, effectiveValue);
53                 if (highestValue == null || highestValue < pair.getValue()) {
54                     highestValue = pair.getValue();
55                 }
56
57                 builder.addEnum(pair);
58             }
59             if (stmt instanceof UnknownSchemaNode) {
60                 builder.addUnknownSchemaNode((UnknownSchemaNode) stmt);
61             }
62         }
63
64         typeDefinition = builder.build();
65     }
66
67     @Override
68     public EnumTypeDefinition getTypeDefinition() {
69         return typeDefinition;
70     }
71 }