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.base.Optional;
11 import java.util.Objects;
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.LeafStatement;
18 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
19 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
20 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
21 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
22 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
23 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
25 public final class LeafEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafStatement> implements
26 LeafSchemaNode, DerivableSchemaNode {
27 private final LeafSchemaNode original;
28 private final TypeDefinition<?> type;
29 private final String defaultStr;
30 private final String unitsStr;
32 public LeafEffectiveStatementImpl(final StmtContext<QName, LeafStatement, EffectiveStatement<QName, LeafStatement>> ctx) {
34 this.original = (LeafSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
36 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
37 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
38 "Leaf is missing a 'type' statement");
42 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
43 ctx.getSchemaPath().get());
44 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
45 if (stmt instanceof DefaultEffectiveStatementImpl) {
46 dflt = ((DefaultEffectiveStatementImpl)stmt).argument();
47 builder.setDefaultValue(stmt.argument());
48 } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
49 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
50 } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
51 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
52 } else if (stmt instanceof StatusEffectiveStatementImpl) {
53 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
54 } else if (stmt instanceof UnitsEffectiveStatementImpl) {
55 units = ((UnitsEffectiveStatementImpl)stmt).argument();
56 builder.setUnits(units);
61 SourceException.throwIf(TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, dflt),
62 ctx.getStatementSourceReference(),
63 "Leaf '%s' has default value '%s' marked with an if-feature statement.",
64 ctx.getStatementArgument(), dflt);
65 } catch (final IllegalStateException e) {
66 throw new SourceException(ctx.getStatementSourceReference(), e,
67 "Unable to find a default value for leaf '%s'", ctx.getStatementArgument());
72 type = builder.build();
76 public Optional<LeafSchemaNode> getOriginal() {
77 return Optional.fromNullable(original);
81 public TypeDefinition<?> getType() {
86 public String getDefault() {
91 public String getUnits() {
96 public int hashCode() {
99 result = prime * result + Objects.hashCode(getQName());
100 result = prime * result + Objects.hashCode(getPath());
105 public boolean equals(final Object obj) {
109 if (!(obj instanceof LeafEffectiveStatementImpl)) {
112 final LeafEffectiveStatementImpl other = (LeafEffectiveStatementImpl) obj;
113 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
117 public String toString() {
118 return LeafEffectiveStatementImpl.class.getSimpleName() + "[" +
119 "qname=" + getQName() +
120 ", path=" + getPath() +