2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableList;
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.LeafListSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.DescriptionEffectiveStatement;
22 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListEffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.stmt.LeafListStatement;
24 import org.opendaylight.yangtools.yang.model.api.stmt.ReferenceEffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
27 import org.opendaylight.yangtools.yang.model.api.stmt.UnitsEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypeBuilder;
29 import org.opendaylight.yangtools.yang.model.util.type.ConcreteTypes;
30 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.AbstractDeclaredEffectiveStatement;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.DataSchemaNodeMixin;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.MustConstraintMixin;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.UserOrderedMixin;
35 abstract class AbstractLeafListEffectiveStatement
36 extends AbstractDeclaredEffectiveStatement.Default<QName, LeafListStatement>
37 implements LeafListEffectiveStatement, LeafListSchemaNode, DerivableSchemaNode,
38 UserOrderedMixin<QName, LeafListStatement>, DataSchemaNodeMixin<QName, LeafListStatement>,
39 MustConstraintMixin<QName, LeafListStatement> {
40 // Variable: either a single substatement or an ImmutableList
41 private final @NonNull Object substatements;
42 private final @NonNull SchemaPath path;
43 private final @NonNull TypeDefinition<?> type;
44 private final int flags;
46 AbstractLeafListEffectiveStatement(final LeafListStatement declared, final SchemaPath path, final int flags,
47 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
49 this.substatements = substatements.size() == 1 ? substatements.get(0) : substatements;
50 this.path = requireNonNull(path);
52 // TODO: lazy instantiation?
53 this.type = buildType();
57 @SuppressWarnings("unchecked")
58 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
59 if (substatements instanceof ImmutableList) {
60 return (ImmutableList<? extends EffectiveStatement<?, ?>>) substatements;
62 verify(substatements instanceof EffectiveStatement, "Unexpected substatement %s", substatements);
63 return ImmutableList.of((EffectiveStatement<?, ?>) substatements);
67 public final int flags() {
72 public final @NonNull QName argument() {
77 public final @NonNull SchemaPath getPath() {
82 public final TypeDefinition<?> getType() {
87 public final boolean isUserOrdered() {
92 public final String toString() {
93 return getClass().getSimpleName() + "[" + getQName() + "]";
96 private TypeDefinition<?> buildType() {
97 final TypeEffectiveStatement<?> typeStmt = findFirstEffectiveSubstatement(TypeEffectiveStatement.class).get();
98 final ConcreteTypeBuilder<?> builder = ConcreteTypes.concreteTypeBuilder(typeStmt.getTypeDefinition(),
100 for (final EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
101 // NOTE: 'default' is ommitted here on purpose
102 if (stmt instanceof DescriptionEffectiveStatement) {
103 builder.setDescription(((DescriptionEffectiveStatement)stmt).argument());
104 } else if (stmt instanceof ReferenceEffectiveStatement) {
105 builder.setReference(((ReferenceEffectiveStatement)stmt).argument());
106 } else if (stmt instanceof StatusEffectiveStatement) {
107 builder.setStatus(((StatusEffectiveStatement)stmt).argument());
108 } else if (stmt instanceof UnitsEffectiveStatement) {
109 builder.setUnits(((UnitsEffectiveStatement)stmt).argument());
112 return builder.build();