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