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.stmt.rfc6020.effective;
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.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
35 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement>
36 implements LeafEffectiveStatement, LeafSchemaNode, DerivableSchemaNode {
37 private final Collection<MustDefinition> mustConstraints;
38 private final LeafSchemaNode original;
39 private final TypeDefinition<?> type;
40 private final String defaultStr;
41 private final String unitsStr;
42 private final boolean mandatory;
44 public LeafEffectiveStatementImpl(
45 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(TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
74 ctx.getStatementSourceReference(),
75 "Leaf '%s' has default value '%s' marked with an if-feature statement.", ctx.getStatementArgument(),
80 type = builder.build();
81 final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
82 mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
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()