66fc7d0d9577ba7829c8a4caa228ef97ec48d833
[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 org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
11 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
12 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.EnumSpecification;
13 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
14 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
15 import org.opendaylight.yangtools.yang.model.util.EnumPairImpl;
16 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
17 import org.opendaylight.yangtools.yang.model.util.type.EnumerationTypeBuilder;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.DeclaredEffectiveStatementBase;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.UnknownEffectiveStatementImpl;
22
23 public final class EnumSpecificationEffectiveStatementImpl extends
24         DeclaredEffectiveStatementBase<String, EnumSpecification> implements
25         TypeEffectiveStatement<EnumSpecification> {
26
27     private final EnumTypeDefinition typeDefinition;
28
29     public EnumSpecificationEffectiveStatementImpl(final StmtContext<String, EnumSpecification, EffectiveStatement<String, EnumSpecification>> ctx) {
30         super(ctx);
31
32         final EnumerationTypeBuilder builder = BaseTypes.enumerationTypeBuilder(ctx.getSchemaPath().get());
33         Integer highestValue = null;
34         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
35             if (stmt instanceof EnumPair) {
36                 EnumPair p = (EnumPair) stmt;
37
38                 if (p.getValue() == null) {
39                     final Integer newValue;
40                     if (highestValue == null) {
41                         newValue = 0;
42                     } else if (highestValue != 2147483647) {
43                         newValue = highestValue + 1;
44                     } else {
45                         throw new SourceException("Enum " + p + " must have a value statement",
46                             ctx.getStatementSourceReference());
47                     }
48
49                     p = new EnumPairImpl(p.getName(), newValue, p.getPath(), p.getDescription(), p.getReference(),
50                         p.getStatus(), p.getUnknownSchemaNodes());
51                 }
52
53                 if (highestValue == null || highestValue < p.getValue()) {
54                     highestValue = p.getValue();
55                 }
56
57                 builder.addEnum(p);
58             }
59             if (stmt instanceof UnknownEffectiveStatementImpl) {
60                 builder.addUnknownSchemaNode((UnknownEffectiveStatementImpl)stmt);
61             }
62         }
63
64         typeDefinition = builder.build();
65     }
66
67     @Override
68     public EnumTypeDefinition getTypeDefinition() {
69         return typeDefinition;
70     }
71 }