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.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.rfc7950.stmt.EffectiveStatementMixins.DataNodeContainerMixin;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
33 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
37 * Base stateless superclass for statements which (logically) always have an associated {@link DeclaredStatement}. This
38 * is notably not true for all {@code case} statements, some of which may actually be implied.
41 * Note implementations are not strictly required to make the declared statement available, they are free to throw
42 * {@link UnsupportedOperationException} from {@link #getDeclared()}, rendering any services relying on declared
43 * statement to be not available.
45 * @param <A> Argument type ({@link Void} if statement does not have argument.)
46 * @param <D> Class representing declared version of this statement.
49 public abstract class AbstractDeclaredEffectiveStatement<A, D extends DeclaredStatement<A>>
50 extends AbstractEffectiveStatement<A, D> {
52 public final StatementSource getStatementSource() {
53 return StatementSource.DECLARATION;
57 public abstract @NonNull D getDeclared();
60 * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
61 * tree namespace based of effective substatements.
63 * @param <A> Argument type ({@link Void} if statement does not have argument.)
64 * @param <D> Class representing declared version of this statement.
65 * @param <E> Class representing effective version of this statement.
67 public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
68 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
70 @SuppressWarnings("unchecked")
71 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
72 final Class<N> namespace) {
73 if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
74 return Optional.of((Map<K, V>) schemaTreeNamespace());
76 return super.getNamespaceContents(namespace);
80 * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
82 protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
83 // Only DataNodeContainer subclasses should be calling this method
84 verify(this instanceof DataNodeContainer);
85 final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(name));
86 return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
89 protected abstract Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
93 * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
94 * namespace based of effective substatements.
96 * @param <A> Argument type ({@link Void} if statement does not have argument.)
97 * @param <D> Class representing declared version of this statement.
98 * @param <E> Class representing effective version of this statement.
100 public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
101 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
103 @SuppressWarnings("unchecked")
104 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
105 final Class<N> namespace) {
106 if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
107 return Optional.of((Map<K, V>) dataTreeNamespace());
109 return super.getNamespaceContents(namespace);
112 protected abstract Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
116 * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
119 * @param <A> Argument type ({@link Void} if statement does not have argument.)
120 * @param <D> Class representing declared version of this statement.
122 public abstract static class Default<A, D extends DeclaredStatement<A>>
123 extends AbstractDeclaredEffectiveStatement<A, D> {
124 private final @NonNull D declared;
126 protected Default(final D declared) {
127 this.declared = requireNonNull(declared);
131 public final D getDeclared() {
137 * Utility class for implementing DataNodeContainer-type statements.
139 public abstract static class DefaultDataNodeContainer<A, D extends DeclaredStatement<A>> extends Default<A, D>
140 implements DataNodeContainerMixin<A, D> {
141 private final @NonNull ImmutableMap<QName, DataSchemaNode> dataChildren;
142 private final @NonNull Object substatements;
144 protected DefaultDataNodeContainer(final D declared, final StatementSourceReference ref,
145 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
147 this.substatements = maskList(substatements);
149 // Note: we do not leak this map, so iteration order does not matter
150 final Map<QName, DataSchemaNode> tmp = new HashMap<>();
152 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
153 if (stmt instanceof DataSchemaNode) {
154 final DataSchemaNode node = (DataSchemaNode) stmt;
155 final QName id = node.getQName();
156 final DataSchemaNode prev = tmp.put(id, node);
157 SourceException.throwIf(prev != null, ref,
158 "Cannot add child with name %s, a conflicting child already exists", id);
162 dataChildren = ImmutableMap.copyOf(tmp);
166 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
167 return unmaskList(substatements);
171 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
172 return Optional.ofNullable(dataChildren.get(requireNonNull(name)));
177 * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement.
178 * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in
179 * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via
180 * {@code uses} or similar.
182 * @param <A> Argument type ({@link Void} if statement does not have argument.)
183 * @param <D> Class representing declared version of this statement.
185 public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
186 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
187 extends DefaultArgument<A, D> {
188 private final @NonNull Object substatements;
190 protected WithSubstatements(final D declared,
191 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
193 this.substatements = maskList(substatements);
197 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
198 return unmaskList(substatements);
202 protected DefaultArgument(final D declared) {
207 public final A argument() {
208 return getDeclared().argument();
213 * A building block on top of {@link Default}, which adds an explicit argument value, which is not related to the
214 * context. This is mostly useful when the effective argument value reflects additional statements and similar.
216 * @param <A> Argument type ({@link Void} if statement does not have argument.)
217 * @param <D> Class representing declared version of this statement.
219 public abstract static class DefaultWithArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
220 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
221 extends DefaultWithArgument<A, D> {
222 private final @NonNull Object substatements;
224 protected WithSubstatements(final D declared, final A argument,
225 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
226 super(declared, argument);
227 this.substatements = maskList(substatements);
231 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
232 return unmaskList(substatements);
236 private final A argument;
238 protected DefaultWithArgument(final D declared, final A argument) {
240 this.argument = argument;
244 public final A argument() {
250 * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
252 * @param <A> Argument type ({@link Void} if statement does not have argument.)
253 * @param <D> Class representing declared version of this statement.
254 * @param <E> Class representing effective version of this statement.
256 public abstract static class DefaultWithSchemaTree<A, D extends DeclaredStatement<A>,
257 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
258 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>,
259 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends DefaultWithSchemaTree<A, D, E> {
260 private final @NonNull Object substatements;
262 protected WithSubstatements(final D declared, final StmtContext<?, ?, ?> ctx,
263 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
264 super(declared, ctx, substatements);
265 this.substatements = maskList(substatements);
269 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
270 return unmaskList(substatements);
274 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
275 private final @NonNull D declared;
277 protected DefaultWithSchemaTree(final D declared, final StmtContext<?, ?, ?> ctx,
278 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
279 this.declared = requireNonNull(declared);
280 this.schemaTree = ImmutableMap.copyOf(createSchemaTreeNamespace(
281 ctx.getStatementSourceReference(), substatements));
285 public final D getDeclared() {
290 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
296 * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated
299 * @param <A> Argument type ({@link Void} if statement does not have argument.)
300 * @param <D> Class representing declared version of this statement.
301 * @param <E> Class representing effective version of this statement.
303 public abstract static class DefaultWithDataTree<A, D extends DeclaredStatement<A>,
304 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithDataTree<A, D, E> {
305 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>,
306 E extends DataTreeAwareEffectiveStatement<A, D>> extends DefaultWithDataTree<A, D, E> {
307 private final @NonNull Object substatements;
309 protected WithSubstatements(final D declared, final StmtContext<?, ?, ?> ctx,
310 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
311 super(declared, ctx, substatements);
312 this.substatements = maskList(substatements);
316 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
317 return unmaskList(substatements);
321 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
322 private final @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTree;
323 private final @NonNull D declared;
325 protected DefaultWithDataTree(final D declared, final StmtContext<?, ?, ?> ctx,
326 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
327 this.declared = requireNonNull(declared);
328 final StatementSourceReference ref = ctx.getStatementSourceReference();
329 final Map<QName, SchemaTreeEffectiveStatement<?>> schema = createSchemaTreeNamespace(ref, substatements);
330 this.schemaTree = ImmutableMap.copyOf(schema);
331 this.dataTree = createDataTreeNamespace(ref, schema.values(), schemaTree);
335 public final D getDeclared() {
340 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
345 protected final Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {