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;
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.annotations.Beta;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
17 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
28 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
31 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
36 * Base stateless superclass for statements which (logically) always have an associated {@link DeclaredStatement}. This
37 * is notably not true for all {@code case} statements, some of which may actually be implied.
40 * Note implementations are not strictly required to make the declared statement available, they are free to throw
41 * {@link UnsupportedOperationException} from {@link #getDeclared()}, rendering any services relying on declared
42 * statement to be not available.
44 * @param <A> Argument type ({@link Void} if statement does not have argument.)
45 * @param <D> Class representing declared version of this statement.
48 public abstract class AbstractDeclaredEffectiveStatement<A, D extends DeclaredStatement<A>>
49 extends AbstractEffectiveStatement<A, D> {
51 public final StatementSource getStatementSource() {
52 return StatementSource.DECLARATION;
56 public final StatementDefinition statementDefinition() {
57 return getDeclared().statementDefinition();
61 public abstract @NonNull D getDeclared();
64 * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
65 * tree namespace based of effective substatements.
67 * @param <A> Argument type ({@link Void} if statement does not have argument.)
68 * @param <D> Class representing declared version of this statement.
69 * @param <E> Class representing effective version of this statement.
71 public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
72 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
74 @SuppressWarnings("unchecked")
75 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
76 final Class<N> namespace) {
77 if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
78 return Optional.of((Map<K, V>) schemaTreeNamespace());
80 return super.getNamespaceContents(namespace);
84 * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
86 protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
87 // Only DataNodeContainer subclasses should be calling this method
88 verify(this instanceof DataNodeContainer);
89 final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(name));
90 return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
93 protected abstract ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
97 * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
98 * namespace based of effective substatements.
100 * @param <A> Argument type ({@link Void} if statement does not have argument.)
101 * @param <D> Class representing declared version of this statement.
102 * @param <E> Class representing effective version of this statement.
104 public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
105 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
107 @SuppressWarnings("unchecked")
108 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
109 final Class<N> namespace) {
110 if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
111 return Optional.of((Map<K, V>) dataTreeNamespace());
113 return super.getNamespaceContents(namespace);
116 protected abstract ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
120 * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
123 * @param <A> Argument type ({@link Void} if statement does not have argument.)
124 * @param <D> Class representing declared version of this statement.
126 public abstract static class Default<A, D extends DeclaredStatement<A>>
127 extends AbstractDeclaredEffectiveStatement<A, D> {
128 private final @NonNull D declared;
130 protected Default(final D declared) {
131 this.declared = requireNonNull(declared);
135 public final D getDeclared() {
141 * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement.
142 * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in
143 * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via
144 * {@code uses} or similar.
146 * @param <A> Argument type ({@link Void} if statement does not have argument.)
147 * @param <D> Class representing declared version of this statement.
149 public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
150 protected DefaultArgument(final D declared) {
155 public final @Nullable A argument() {
156 return getDeclared().argument();
161 * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
163 * @param <A> Argument type ({@link Void} if statement does not have argument.)
164 * @param <D> Class representing declared version of this statement.
165 * @param <E> Class representing effective version of this statement.
167 public abstract static class DefaultWithSchemaTree<A, D extends DeclaredStatement<A>,
168 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
169 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
170 private final @NonNull D declared;
172 protected DefaultWithSchemaTree(final D declared, final StmtContext<?, ?, ?> ctx,
173 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
174 this.declared = requireNonNull(declared);
175 this.schemaTree = AbstractSchemaEffectiveDocumentedNode.createSchemaTreeNamespace(
176 ctx.getStatementSourceReference(), substatements);
180 public final D getDeclared() {
185 protected final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
191 * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated
194 * @param <A> Argument type ({@link Void} if statement does not have argument.)
195 * @param <D> Class representing declared version of this statement.
196 * @param <E> Class representing effective version of this statement.
198 public abstract static class DefaultWithDataTree<A, D extends DeclaredStatement<A>,
199 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithDataTree<A, D, E> {
200 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
201 private final @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTree;
202 private final @NonNull D declared;
204 protected DefaultWithDataTree(final D declared, final StmtContext<?, ?, ?> ctx,
205 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
206 this.declared = requireNonNull(declared);
207 final StatementSourceReference ref = ctx.getStatementSourceReference();
208 this.schemaTree = AbstractSchemaEffectiveDocumentedNode.createSchemaTreeNamespace(ref, substatements);
209 this.dataTree = AbstractSchemaEffectiveDocumentedNode.createDataTreeNamespace(ref, schemaTree);
213 public final D getDeclared() {
218 protected final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
223 protected final ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {