Cleanup DocumentedNode
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / rfc6020 / effective / type / EnumSpecificationEffectiveStatementImpl.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.stmt.rfc6020.effective.type;
9
10 import javax.annotation.Nonnull;
11 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
13 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
14 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
15 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.EnumPairBuilder;
18 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
23
24 public final class EnumSpecificationEffectiveStatementImpl extends
25         DeclaredEffectiveStatementBase<String, EnumSpecification> implements
26         TypeEffectiveStatement<EnumSpecification> {
27
28     private final EnumTypeDefinition typeDefinition;
29
30     public EnumSpecificationEffectiveStatementImpl(
31             final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
32         super(ctx);
33
34         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
35         Integer highestValue = null;
36         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
37             if (stmt instanceof EnumEffectiveStatementImpl) {
38                 final EnumEffectiveStatementImpl enumSubStmt = (EnumEffectiveStatementImpl) stmt;
39
40                 final int effectiveValue;
41                 if (enumSubStmt.getDeclaredValue() == null) {
42                     if (highestValue != null) {
43                         SourceException.throwIf(highestValue == 2147483647, ctx.getStatementSourceReference(),
44                                 "Enum '%s' must have a value statement", enumSubStmt);
45                         effectiveValue = highestValue + 1;
46                     } else {
47                         effectiveValue = 0;
48                     }
49                 } else {
50                     effectiveValue = enumSubStmt.getDeclaredValue();
51                 }
52
53                 final EnumPairBuilder pairBuilder = EnumPairBuilder.create(enumSubStmt.getName(), effectiveValue)
54                         .setStatus(enumSubStmt.getStatus()).setUnknownSchemaNodes(enumSubStmt.getUnknownSchemaNodes());
55                 enumSubStmt.getDescription().ifPresent(pairBuilder::setDescription);
56                 enumSubStmt.getReference().ifPresent(pairBuilder::setReference);
57
58                 final EnumPair pair = pairBuilder.build();
59                 if (highestValue == null || highestValue < pair.getValue()) {
60                     highestValue = pair.getValue();
61                 }
62
63                 builder.addEnum(pair);
64             }
65             if (stmt instanceof UnknownEffectiveStatementImpl) {
66                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl) stmt);
67             }
68         }
69
70         typeDefinition = builder.build();
71     }
72
73     @Nonnull
74     @Override
75     public EnumTypeDefinition getTypeDefinition() {
76         return typeDefinition;
77     }
78 }