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.MustDefinition;
20 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.DefaultEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.DescriptionEffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
32 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractEffectiveDataSchemaNode;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
36 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
38 final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
39 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
41 private static final String ORDER_BY_USER_KEYWORD = "user";
43 private final TypeDefinition<?> type;
44 private final LeafListSchemaNode original;
45 private final boolean userOrdered;
46 private final @NonNull ImmutableSet<String> defaultValues;
47 private final ImmutableSet<MustDefinition> mustConstraints;
48 private final ElementCountConstraint elementCountConstraint;
50 LeafListEffectiveStatementImpl(
51 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
53 this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
55 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
56 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
57 "Leaf-list is missing a 'type' statement");
59 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
60 ctx.getSchemaPath().get());
61 final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
62 boolean isUserOrdered = false;
63 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
64 if (stmt instanceof OrderedByEffectiveStatement) {
65 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
68 if (stmt instanceof DefaultEffectiveStatement) {
69 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
70 } else if (stmt instanceof DescriptionEffectiveStatement) {
71 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
72 } else if (stmt instanceof ReferenceEffectiveStatement) {
73 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
74 } else if (stmt instanceof StatusEffectiveStatement) {
75 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
76 } else if (stmt instanceof UnitsEffectiveStatement) {
77 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
81 // FIXME: We need to interpret the default value in terms of supplied element type
82 defaultValues = defaultValuesBuilder.build();
83 SourceException.throwIf(
84 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
85 ctx.getStatementSourceReference(),
86 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
87 ctx.getStatementArgument(), defaultValues);
89 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
91 type = builder.build();
92 userOrdered = isUserOrdered;
93 elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
94 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
98 public Collection<String> getDefaults() {
103 public Optional<LeafListSchemaNode> getOriginal() {
104 return Optional.ofNullable(original);
108 public TypeDefinition<?> getType() {
113 public boolean isUserOrdered() {
118 public Optional<ElementCountConstraint> getElementCountConstraint() {
119 return Optional.ofNullable(elementCountConstraint);
123 public Collection<MustDefinition> getMustConstraints() {
124 return mustConstraints;
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() + "]";