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.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
20 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
24 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
26 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
29 public final class LeafListEffectiveStatementImpl extends AbstractEffectiveDataSchemaNode<LeafListStatement>
30 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode {
32 private static final String ORDER_BY_USER_KEYWORD = "user";
34 private final TypeDefinition<?> type;
35 private final LeafListSchemaNode original;
36 private final boolean userOrdered;
37 private final Set<String> defaultValues;
39 public LeafListEffectiveStatementImpl(
40 final StmtContext<QName, LeafListStatement, EffectiveStatement<QName, LeafListStatement>> ctx) {
42 this.original = (LeafListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective).orElse(null);
44 final TypeEffectiveStatement<?> typeStmt = SourceException.throwIfNull(
45 firstSubstatementOfType(TypeEffectiveStatement.class), ctx.getStatementSourceReference(),
46 "Leaf-list is missing a 'type' statement");
48 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
49 ctx.getSchemaPath().get());
50 final ImmutableSet.Builder<String> defaultValuesBuilder = ImmutableSet.builder();
51 boolean isUserOrdered = false;
52 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
53 if (stmt instanceof OrderedByEffectiveStatementImpl) {
54 isUserOrdered = ORDER_BY_USER_KEYWORD.equals(stmt.argument());
57 if (stmt instanceof DefaultEffectiveStatementImpl) {
58 defaultValuesBuilder.add(((DefaultEffectiveStatementImpl) stmt).argument());
59 } else if (stmt instanceof DescriptionEffectiveStatementImpl) {
60 builder.setDescription(((DescriptionEffectiveStatementImpl)stmt).argument());
61 } else if (stmt instanceof ReferenceEffectiveStatementImpl) {
62 builder.setReference(((ReferenceEffectiveStatementImpl)stmt).argument());
63 } else if (stmt instanceof StatusEffectiveStatementImpl) {
64 builder.setStatus(((StatusEffectiveStatementImpl)stmt).argument());
65 } else if (stmt instanceof UnitsEffectiveStatementImpl) {
66 builder.setUnits(((UnitsEffectiveStatementImpl)stmt).argument());
70 // FIXME: We need to interpret the default value in terms of supplied element type
71 defaultValues = defaultValuesBuilder.build();
72 SourceException.throwIf(
73 TypeUtils.hasDefaultValueMarkedWithIfFeature(ctx.getRootVersion(), typeStmt, defaultValues),
74 ctx.getStatementSourceReference(),
75 "Leaf-list '%s' has one of its default values '%s' marked with an if-feature statement.",
76 ctx.getStatementArgument(), defaultValues);
78 // FIXME: RFC7950 section 7.7.4: we need to check for min-elements and defaultValues conflict
80 type = builder.build();
81 userOrdered = isUserOrdered;
85 public Collection<String> getDefaults() {
90 public Optional<LeafListSchemaNode> getOriginal() {
91 return Optional.ofNullable(original);
95 public TypeDefinition<?> getType() {
100 public boolean isUserOrdered() {
105 public int hashCode() {
106 final int prime = 31;
108 result = prime * result + Objects.hashCode(getQName());
109 result = prime * result + Objects.hashCode(getPath());
114 public boolean equals(final Object obj) {
121 if (getClass() != obj.getClass()) {
124 final LeafListEffectiveStatementImpl other = (LeafListEffectiveStatementImpl) obj;
125 return Objects.equals(getQName(), other.getQName()) && Objects.equals(getPath(), other.getPath());
129 public String toString() {
130 return LeafListEffectiveStatementImpl.class.getSimpleName() + "[" + getQName() + "]";