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;
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.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.MustDefinition;
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.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
36 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
37 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
39 private static final String ORDER_BY_USER_KEYWORD = "user";
41 private final TypeDefinition<?> type;
42 private final LeafListSchemaNode original;
43 private final boolean userOrdered;
44 private final Set<String> defaultValues;
45 private final Collection<MustDefinition> mustConstraints;
47 public LeafListEffectiveStatementImpl(
48 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
50 this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
52 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
53 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
54 "Leaf-list is missing a 'type' statement");
56 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
57 ctx.getSchemaPath().get());
58 final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
59 boolean isUserOrdered = false;
60 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
61 if (stmt instanceof OrderedByEffectiveStatement) {
62 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
65 if (stmt instanceof DefaultEffectiveStatement) {
66 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
67 } else if (stmt instanceof DescriptionEffectiveStatement) {
68 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
69 } else if (stmt instanceof ReferenceEffectiveStatement) {
70 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
71 } else if (stmt instanceof StatusEffectiveStatement) {
72 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
73 } else if (stmt instanceof UnitsEffectiveStatement) {
74 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
78 // FIXME: We need to interpret the default value in terms of supplied element type
79 defaultValues = defaultValuesBuilder.build();
80 SourceException.throwIf(
81 TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
82 ctx.getStatementSourceReference(),
83 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
84 ctx.getStatementArgument(), defaultValues);
86 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
88 type = builder.build();
89 userOrdered = isUserOrdered;
90 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
94 public Collection<String> getDefaults() {
99 public Optional<LeafListSchemaNode> getOriginal() {
100 return Optional.ofNullable(original);
104 public TypeDefinition<?> getType() {
109 public boolean isUserOrdered() {
114 public Collection<MustDefinition> getMustConstraints() {
115 return mustConstraints;
119 public int hashCode() {
120 final int prime = 31;
122 result = prime * result + Objects.hashCode(getQName());
123 result = prime * result + Objects.hashCode(getPath());
128 public boolean equals(final Object obj) {
135 if (getClass() != obj.getClass()) {
138 final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
139 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
143 public String toString() {
144 return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";