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