Merge branch 'master' of ../controller
[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 final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement>
37         implements LeafEffectiveStatement, LeafSchemaNode, DerivableSchemaNode {
38     private final ImmutableSet<MustDefinition> mustConstraints;
39     private final LeafSchemaNode original;
40     private final TypeDefinition<?> type;
41     private final String defaultStr;
42     private final String unitsStr;
43     private final boolean mandatory;
44
45     LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
46         super(ctx);
47         this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
48
49         final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
50                 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
51                 "Leaf is missing a 'type' statement");
52
53         String dflt = null;
54         String units = null;
55         final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
56             ctx.getSchemaPath().get());
57         for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
58             if (stmt instanceof DefaultEffectiveStatement) {
59                 dflt = ((DefaultEffectiveStatement)stmt).argument();
60                 builder.setDefaultValue(stmt.argument());
61             } else if (stmt instanceof DescriptionEffectiveStatement) {
62                 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
63             } else if (stmt instanceof ReferenceEffectiveStatement) {
64                 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
65             } else if (stmt instanceof StatusEffectiveStatement) {
66                 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
67             } else if (stmt instanceof UnitsEffectiveStatement) {
68                 units = ((UnitsEffectiveStatement)stmt).argument();
69                 builder.setUnits(units);
70             }
71         }
72
73         SourceException.throwIf(
74             EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
75             ctx.getStatementSourceReference(),
76             "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(), dflt);
77
78         defaultStr = dflt;
79         unitsStr = units;
80         type = builder.build();
81         mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
82                 .booleanValue();
83         mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
84     }
85
86     @Override
87     public boolean isMandatory() {
88         return mandatory;
89     }
90
91     @Override
92     public Optional<LeafSchemaNode> getOriginal() {
93         return Optional.ofNullable(original);
94     }
95
96     @Override
97     public TypeDefinition<?> getType() {
98         return type;
99     }
100
101     @Override
102     public Collection<MustDefinition> getMustConstraints() {
103         return mustConstraints;
104     }
105
106     @Override
107     public int hashCode() {
108         final int prime = 31;
109         int result = 1;
110         result = prime * result + Objects.hashCode(getQName());
111         result = prime * result + Objects.hashCode(getPath());
112         return result;
113     }
114
115     @Override
116     public boolean equals(final Object obj) {
117         if (this == obj) {
118             return true;
119         }
120         if (!(obj instanceof LeafEffectiveStatementImpl)) {
121             return false;
122         }
123         final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
124         return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
125     }
126
127     @Override
128     public String toString() {
129         return LeafEffectiveStatementImpl.class.getSimpleName() + "["
130                 + "qname=" + getQName()
131                 + ", path=" + getPath()
132                 + "]";
133     }
134 }