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 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;
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;
35 public LeafEffectiveStatementImpl(
36 final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
38 this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
40 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
41 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
42 "Leaf is missing a 'type' statement");
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);
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(),
71 type = builder.build();
72 final MandatoryEffectiveStatement mandatoryStmt = firstEffective(MandatoryEffectiveStatement.class);
73 mandatory = mandatoryStmt == null ? false : mandatoryStmt.argument().booleanValue();
77 public boolean isMandatory() {
82 public Optional<LeafSchemaNode> getOriginal() {
83 return Optional.ofNullable(original);
87 public TypeDefinition<?> getType() {
92 public int hashCode() {
95 result = prime * result + Objects.hashCode(getQName());
96 result = prime * result + Objects.hashCode(getPath());
101 public boolean equals(final Object obj) {
105 if (!(obj instanceof LeafEffectiveStatementImpl)) {
108 final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
109 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
113 public String toString() {
114 return LeafEffectiveStatementImpl.class.getSimpleName() + "["
115 + "qname=" + getQName()
116 + ", path=" + getPath()