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;
16 import java.util.HashMap;
18 import java.util.Optional;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
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.rfc7950.stmt.EffectiveStatementMixins.DataNodeContainerMixin;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
38 * Base stateless superclass for statements which (logically) always have an associated {@link DeclaredStatement}. This
39 * is notably not true for all {@code case} statements, some of which may actually be implied.
42 * Note implementations are not strictly required to make the declared statement available, they are free to throw
43 * {@link UnsupportedOperationException} from {@link #getDeclared()}, rendering any services relying on declared
44 * statement to be not available.
46 * @param <A> Argument type ({@link Void} if statement does not have argument.)
47 * @param <D> Class representing declared version of this statement.
50 public abstract class AbstractDeclaredEffectiveStatement<A, D extends DeclaredStatement<A>>
51 extends AbstractEffectiveStatement<A, D> {
53 public final StatementSource getStatementSource() {
54 return StatementSource.DECLARATION;
58 public abstract @NonNull D getDeclared();
61 * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
62 * tree namespace based of effective substatements.
64 * @param <A> Argument type ({@link Void} if statement does not have argument.)
65 * @param <D> Class representing declared version of this statement.
66 * @param <E> Class representing effective version of this statement.
68 public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
69 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
71 @SuppressWarnings("unchecked")
72 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
73 final Class<N> namespace) {
74 if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
75 return Optional.of((Map<K, V>) schemaTreeNamespace());
77 return super.getNamespaceContents(namespace);
81 * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
83 protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
84 // Only DataNodeContainer subclasses should be calling this method
85 verify(this instanceof DataNodeContainer);
86 final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(name));
87 return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
90 protected abstract Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
94 * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
95 * namespace based of effective substatements.
97 * @param <A> Argument type ({@link Void} if statement does not have argument.)
98 * @param <D> Class representing declared version of this statement.
99 * @param <E> Class representing effective version of this statement.
101 public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
102 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
104 @SuppressWarnings("unchecked")
105 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
106 final Class<N> namespace) {
107 if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
108 return Optional.of((Map<K, V>) dataTreeNamespace());
110 return super.getNamespaceContents(namespace);
113 protected abstract Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
117 * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
120 * @param <A> Argument type ({@link Void} if statement does not have argument.)
121 * @param <D> Class representing declared version of this statement.
123 public abstract static class Default<A, D extends DeclaredStatement<A>>
124 extends AbstractDeclaredEffectiveStatement<A, D> {
125 private final @NonNull D declared;
127 protected Default(final D declared) {
128 this.declared = requireNonNull(declared);
132 public final D getDeclared() {
138 * Utility class for implementing DataNodeContainer-type statements.
140 public abstract static class DefaultDataNodeContainer<A, D extends DeclaredStatement<A>> extends Default<A, D>
141 implements DataNodeContainerMixin<A, D> {
142 private final @NonNull ImmutableMap<QName, DataSchemaNode> dataChildren;
143 private final @NonNull Object substatements;
145 protected DefaultDataNodeContainer(final D declared, final StatementSourceReference ref,
146 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
148 this.substatements = maskList(substatements);
150 // Note: we do not leak this map, so iteration order does not matter
151 final Map<QName, DataSchemaNode> tmp = new HashMap<>();
153 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
154 if (stmt instanceof DataSchemaNode) {
155 final DataSchemaNode node = (DataSchemaNode) stmt;
156 final QName id = node.getQName();
157 final DataSchemaNode prev = tmp.put(id, node);
158 SourceException.throwIf(prev != null, ref,
159 "Cannot add child with name %s, a conflicting child already exists", id);
163 dataChildren = ImmutableMap.copyOf(tmp);
167 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
168 return unmaskList(substatements);
172 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
173 return Optional.ofNullable(dataChildren.get(requireNonNull(name)));
178 * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement.
179 * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in
180 * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via
181 * {@code uses} or similar.
183 * @param <A> Argument type ({@link Void} if statement does not have argument.)
184 * @param <D> Class representing declared version of this statement.
186 public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
187 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
188 extends DefaultArgument<A, D> {
189 private final @NonNull Object substatements;
191 protected WithSubstatements(final D declared,
192 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
194 this.substatements = maskList(substatements);
198 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
199 return unmaskList(substatements);
203 protected DefaultArgument(final D declared) {
208 public final @Nullable A argument() {
209 return getDeclared().argument();
214 * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
216 * @param <A> Argument type ({@link Void} if statement does not have argument.)
217 * @param <D> Class representing declared version of this statement.
218 * @param <E> Class representing effective version of this statement.
220 public abstract static class DefaultWithSchemaTree<A, D extends DeclaredStatement<A>,
221 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
222 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>,
223 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends DefaultWithSchemaTree<A, D, E> {
224 private final @NonNull Object substatements;
226 protected WithSubstatements(final D declared, final StmtContext<?, ?, ?> ctx,
227 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
228 super(declared, ctx, substatements);
229 this.substatements = maskList(substatements);
233 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
234 return unmaskList(substatements);
238 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
239 private final @NonNull D declared;
241 protected DefaultWithSchemaTree(final D declared, final StmtContext<?, ?, ?> ctx,
242 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
243 this.declared = requireNonNull(declared);
244 this.schemaTree = ImmutableMap.copyOf(createSchemaTreeNamespace(
245 ctx.getStatementSourceReference(), substatements));
249 public final D getDeclared() {
254 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
260 * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated
263 * @param <A> Argument type ({@link Void} if statement does not have argument.)
264 * @param <D> Class representing declared version of this statement.
265 * @param <E> Class representing effective version of this statement.
267 public abstract static class DefaultWithDataTree<A, D extends DeclaredStatement<A>,
268 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithDataTree<A, D, E> {
269 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>,
270 E extends DataTreeAwareEffectiveStatement<A, D>> extends DefaultWithDataTree<A, D, E> {
271 private final @NonNull Object substatements;
273 protected WithSubstatements(final D declared, final StmtContext<?, ?, ?> ctx,
274 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
275 super(declared, ctx, substatements);
276 this.substatements = maskList(substatements);
280 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
281 return unmaskList(substatements);
285 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
286 private final @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTree;
287 private final @NonNull D declared;
289 protected DefaultWithDataTree(final D declared, final StmtContext<?, ?, ?> ctx,
290 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
291 this.declared = requireNonNull(declared);
292 final StatementSourceReference ref = ctx.getStatementSourceReference();
293 final Map<QName, SchemaTreeEffectiveStatement<?>> schema = createSchemaTreeNamespace(ref, substatements);
294 this.schemaTree = ImmutableMap.copyOf(schema);
295 this.dataTree = createDataTreeNamespace(ref, schema.values(), schemaTree);
299 public final D getDeclared() {
304 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
309 protected final Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {