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_list;
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.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.yangtools.yang.common.QName;
16 import org.opendaylight.yangtools.yang.model.api.DerivableSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
18 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
19 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
31 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveMustConstraintAwareDataSchemaNode;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
35 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 * Internal implementation of LeafListEffectiveStatement.
40 * @deprecated This class is visible only for historical purposes and is going to be hidden.
42 // FIXME: 4.0.0: hide this class
44 public final class LeafListEffectiveStatementImpl
45 extends AbstractEffectiveMustConstraintAwareDataSchemaNode<LeafListStatement>
46 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
48 private static final String ORDER_BY_USER_KEYWORD = "user";
50 private final TypeDefinition<?> type;
51 private final LeafListSchemaNode original;
52 private final boolean userOrdered;
53 private final @NonNull ImmutableSet<String> defaultValues;
54 private final ElementCountConstraint elementCountConstraint;
56 LeafListEffectiveStatementImpl(
57 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
59 this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
61 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
62 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
63 "Leaf-list is missing a 'type' statement");
65 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
66 ctx.getSchemaPath().get());
67 final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
68 boolean isUserOrdered = false;
69 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
70 if (stmt instanceof OrderedByEffectiveStatement) {
71 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
74 if (stmt instanceof DefaultEffectiveStatement) {
75 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
76 } else if (stmt instanceof DescriptionEffectiveStatement) {
77 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
78 } else if (stmt instanceof ReferenceEffectiveStatement) {
79 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
80 } else if (stmt instanceof StatusEffectiveStatement) {
81 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
82 } else if (stmt instanceof UnitsEffectiveStatement) {
83 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
87 // FIXME: We need to interpret the default value in terms of supplied element type
88 defaultValues = defaultValuesBuilder.build();
89 SourceException.throwIf(
90 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
91 ctx.getStatementSourceReference(),
92 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
93 ctx.getStatementArgument(), defaultValues);
95 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
97 type = builder.build();
98 userOrdered = isUserOrdered;
99 elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
103 public Collection<String> getDefaults() {
104 return defaultValues;
108 public Optional<LeafListSchemaNode> getOriginal() {
109 return Optional.ofNullable(original);
113 public TypeDefinition<?> getType() {
118 public boolean isUserOrdered() {
123 public Optional<ElementCountConstraint> getElementCountConstraint() {
124 return Optional.ofNullable(elementCountConstraint);
128 public int hashCode() {
129 final int prime = 31;
131 result = prime * result + Objects.hashCode(getQName());
132 result = prime * result + Objects.hashCode(getPath());
137 public boolean equals(final Object obj) {
144 if (getClass() != obj.getClass()) {
147 final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
148 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
152 public String toString() {
153 return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";