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 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;
45 LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
47 this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
49 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
50 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
51 "Leaf is missing a 'type' statement");
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);
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);
80 type = builder.build();
81 mandatory = findFirstEffectiveSubstatementArgument(MandatoryEffectiveStatement.class).orElse(Boolean.FALSE)
83 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
87 public boolean isMandatory() {
92 public Optional<LeafSchemaNode> getOriginal() {
93 return Optional.ofNullable(original);
97 public TypeDefinition<?> getType() {
102 public Collection<MustDefinition> getMustConstraints() {
103 return mustConstraints;
107 public int hashCode() {
108 final int prime = 31;
110 result = prime * result + Objects.hashCode(getQName());
111 result = prime * result + Objects.hashCode(getPath());
116 public boolean equals(final Object obj) {
120 if (!(obj instanceof LeafEffectiveStatementImpl)) {
123 final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
124 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
128 public String toString() {
129 return LeafEffectiveStatementImpl.class.getSimpleName() + "["
130 + "qname=" + getQName()
131 + ", path=" + getPath()