2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.leaf;
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;
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;
46 LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
48 this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
50 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
51 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
52 "Leaf is missing a 'type' statement");
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);
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);
81 type = builder.build();
82 mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
84 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
88 public boolean isMandatory() {
93 public Optional<LeafSchemaNode> getOriginal() {
94 return Optional.ofNullable(original);
98 public TypeDefinition<?> getType() {
103 public Collection<MustDefinition> getMustConstraints() {
104 return mustConstraints;
108 public int hashCode() {
109 final int prime = 31;
111 result = prime * result + Objects.hashCode(getQName());
112 result = prime * result + Objects.hashCode(getPath());
117 public boolean equals(final Object obj) {
121 if (!(obj instanceof LeafEffectiveStatementImpl)) {
124 final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
125 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
129 public String toString() {
130 return LeafEffectiveStatementImpl.class.getSimpleName() + "["
131 + "qname=" + getQName()
132 + ", path=" + getPath()