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