Adjust test suite parser update to conform with API changes
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / leaf / LeafEffectiveStatementImpl.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.leaf;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.Collection;
12 import java.util.Objects;
13 import java.util.Optional;
14 import org.opendaylight.yangtools.yang.common.QName;
15 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
30 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35
36 // FIXME: hide this class
37 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement>
38         implements LeafEffectiveStatement, LeafSchemaNode, DerivableSchemaNode {
39     private final Collection<MustDefinition> mustConstraints;
40     private final LeafSchemaNode original;
41     private final TypeDefinition<?> type;
42     private final String defaultStr;
43     private final String unitsStr;
44     private final boolean mandatory;
45
46     LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
47         super(ctx);
48         this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
49
50         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
51                 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
52                 "Leaf is missing a 'type' statement");
53
54         String dflt = null;
55         String units = null;
56         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
57             ctx.getSchemaPath().get());
58         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
59             if (stmt instanceof DefaultEffectiveStatement) {
60                 dflt = ((DefaultEffectiveStatement)stmt).argument();
61                 builder.setDefaultValue(stmt.argument());
62             } else if (stmt instanceof DescriptionEffectiveStatement) {
63                 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
64             } else if (stmt instanceof ReferenceEffectiveStatement) {
65                 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
66             } else if (stmt instanceof StatusEffectiveStatement) {
67                 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
68             } else if (stmt instanceof UnitsEffectiveStatement) {
69                 units = ((UnitsEffectiveStatement)stmt).argument();
70                 builder.setUnits(units);
71             }
72         }
73
74         SourceException.throwIf(
75             EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
76             ctx.getStatementSourceReference(),
77             "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(), dflt);
78
79         defaultStr = dflt;
80         unitsStr = units;
81         type = builder.build();
82         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
83                 .booleanValue();
84         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
85     }
86
87     @Override
88     public boolean isMandatory() {
89         return mandatory;
90     }
91
92     @Override
93     public Optional<LeafSchemaNode> getOriginal() {
94         return Optional.ofNullable(original);
95     }
96
97     @Override
98     public TypeDefinition<?> getType() {
99         return type;
100     }
101
102     @Override
103     public Collection<MustDefinition> getMustConstraints() {
104         return mustConstraints;
105     }
106
107     @Override
108     public int hashCode() {
109         final int prime = 31;
110         int result = 1;
111         result = prime * result + Objects.hashCode(getQName());
112         result = prime * result + Objects.hashCode(getPath());
113         return result;
114     }
115
116     @Override
117     public boolean equals(final Object obj) {
118         if (this == obj) {
119             return true;
120         }
121         if (!(obj instanceof LeafEffectiveStatementImpl)) {
122             return false;
123         }
124         final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
125         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
126     }
127
128     @Override
129     public String toString() {
130         return LeafEffectiveStatementImpl.class.getSimpleName() + "["
131                 + "qname=" + getQName()
132                 + ", path=" + getPath()
133                 + "]";
134     }
135 }