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