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;
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 // FIXME: hide this class
39 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
40 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
42 private static final String ORDER_BY_USER_KEYWORD = "user";
44 private final TypeDefinition<?> type;
45 private final LeafListSchemaNode original;
46 private final boolean userOrdered;
47 private final Set<String> defaultValues;
48 private final Collection<MustDefinition> mustConstraints;
49 private final ElementCountConstraint elementCountConstraint;
51 LeafListEffectiveStatementImpl(
52 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
54 this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
56 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
57 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
58 "Leaf-list is missing a 'type' statement");
60 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
61 ctx.getSchemaPath().get());
62 final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
63 boolean isUserOrdered = false;
64 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
65 if (stmt instanceof OrderedByEffectiveStatement) {
66 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
69 if (stmt instanceof DefaultEffectiveStatement) {
70 defaultValuesBuilder.add(((DefaultEffectiveStatement) stmt).argument());
71 } else if (stmt instanceof DescriptionEffectiveStatement) {
72 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
73 } else if (stmt instanceof ReferenceEffectiveStatement) {
74 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
75 } else if (stmt instanceof StatusEffectiveStatement) {
76 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
77 } else if (stmt instanceof UnitsEffectiveStatement) {
78 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
82 // FIXME: We need to interpret the default value in terms of supplied element type
83 defaultValues = defaultValuesBuilder.build();
84 SourceException.throwIf(
85 EffectiveStmtUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
86 ctx.getStatementSourceReference(),
87 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
88 ctx.getStatementArgument(), defaultValues);
90 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
92 type = builder.build();
93 userOrdered = isUserOrdered;
94 elementCountConstraint = EffectiveStmtUtils.createElementCountConstraint(this).orElse(null);
95 mustConstraints = ImmutableSet.copyOf(allSubstatementsOfType(MustDefinition.class));
99 public Collection<String> getDefaults() {
100 return defaultValues;
104 public Optional<LeafListSchemaNode> getOriginal() {
105 return Optional.ofNullable(original);
109 public TypeDefinition<?> getType() {
114 public boolean isUserOrdered() {
119 public Optional<ElementCountConstraint> getElementCountConstraint() {
120 return Optional.ofNullable(elementCountConstraint);
124 public Collection<MustDefinition> getMustConstraints() {
125 return mustConstraints;
129 public int hashCode() {
130 final int prime = 31;
132 result = prime * result + Objects.hashCode(getQName());
133 result = prime * result + Objects.hashCode(getPath());
138 public boolean equals(final Object obj) {
145 if (getClass() != obj.getClass()) {
148 final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
149 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
153 public String toString() {
154 return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";