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.StatementSource;
27 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement;
28 import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
32 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
35 * Base stateless superclass for statements which (logically) always have an associated {@link DeclaredStatement}. This
36 * is notably not true for all {@code case} statements, some of which may actually be implied.
39 * Note implementations are not strictly required to make the declared statement available, they are free to throw
40 * {@link UnsupportedOperationException} from {@link #getDeclared()}, rendering any services relying on declared
41 * statement to be not available.
43 * @param <A> Argument type ({@link Void} if statement does not have argument.)
44 * @param <D> Class representing declared version of this statement.
47 public abstract class AbstractDeclaredEffectiveStatement<A, D extends DeclaredStatement<A>>
48 extends AbstractEffectiveStatement<A, D> {
50 public final StatementSource getStatementSource() {
51 return StatementSource.DECLARATION;
55 public abstract @NonNull D getDeclared();
58 * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
59 * tree namespace based of effective substatements.
61 * @param <A> Argument type ({@link Void} if statement does not have argument.)
62 * @param <D> Class representing declared version of this statement.
63 * @param <E> Class representing effective version of this statement.
65 public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
66 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
68 @SuppressWarnings("unchecked")
69 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
70 final Class<N> namespace) {
71 if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
72 return Optional.of((Map<K, V>) schemaTreeNamespace());
74 return super.getNamespaceContents(namespace);
78 * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
80 protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
81 // Only DataNodeContainer subclasses should be calling this method
82 verify(this instanceof DataNodeContainer);
83 final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(name));
84 return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
87 protected abstract Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
91 * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
92 * namespace based of effective substatements.
94 * @param <A> Argument type ({@link Void} if statement does not have argument.)
95 * @param <D> Class representing declared version of this statement.
96 * @param <E> Class representing effective version of this statement.
98 public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
99 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
101 @SuppressWarnings("unchecked")
102 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
103 final Class<N> namespace) {
104 if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
105 return Optional.of((Map<K, V>) dataTreeNamespace());
107 return super.getNamespaceContents(namespace);
110 protected abstract Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
114 * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
117 * @param <A> Argument type ({@link Void} if statement does not have argument.)
118 * @param <D> Class representing declared version of this statement.
120 public abstract static class Default<A, D extends DeclaredStatement<A>>
121 extends AbstractDeclaredEffectiveStatement<A, D> {
122 private final @NonNull D declared;
124 protected Default(final D declared) {
125 this.declared = requireNonNull(declared);
129 public final D getDeclared() {
135 * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement.
136 * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in
137 * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via
138 * {@code uses} or similar.
140 * @param <A> Argument type ({@link Void} if statement does not have argument.)
141 * @param <D> Class representing declared version of this statement.
143 public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
144 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
145 extends DefaultArgument<A, D> {
146 private final @NonNull Object substatements;
148 protected WithSubstatements(final D declared,
149 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
151 this.substatements = maskList(substatements);
155 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
156 return unmaskList(substatements);
160 protected DefaultArgument(final D declared) {
165 public final @Nullable A argument() {
166 return getDeclared().argument();
171 * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
173 * @param <A> Argument type ({@link Void} if statement does not have argument.)
174 * @param <D> Class representing declared version of this statement.
175 * @param <E> Class representing effective version of this statement.
177 public abstract static class DefaultWithSchemaTree<A, D extends DeclaredStatement<A>,
178 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
179 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
180 private final @NonNull D declared;
182 protected DefaultWithSchemaTree(final D declared, final StmtContext<?, ?, ?> ctx,
183 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
184 this.declared = requireNonNull(declared);
185 this.schemaTree = ImmutableMap.copyOf(AbstractSchemaEffectiveDocumentedNode.createSchemaTreeNamespace(
186 ctx.getStatementSourceReference(), substatements));
190 public final D getDeclared() {
195 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
201 * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated
204 * @param <A> Argument type ({@link Void} if statement does not have argument.)
205 * @param <D> Class representing declared version of this statement.
206 * @param <E> Class representing effective version of this statement.
208 public abstract static class DefaultWithDataTree<A, D extends DeclaredStatement<A>,
209 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithDataTree<A, D, E> {
210 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
211 private final @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTree;
212 private final @NonNull D declared;
214 protected DefaultWithDataTree(final D declared, final StmtContext<?, ?, ?> ctx,
215 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
216 this.declared = requireNonNull(declared);
217 final StatementSourceReference ref = ctx.getStatementSourceReference();
218 final Map<QName, SchemaTreeEffectiveStatement<?>> schema =
219 AbstractSchemaEffectiveDocumentedNode.createSchemaTreeNamespace(ref, substatements);
220 this.schemaTree = ImmutableMap.copyOf(schema);
221 this.dataTree = AbstractSchemaEffectiveDocumentedNode.createDataTreeNamespace(ref, schema.values(),
226 public final D getDeclared() {
231 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
236 protected final Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {