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.source.SourceException;
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 abstract @NonNull D getDeclared();
59 * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
60 * tree namespace based of effective substatements.
62 * @param <A> Argument type ({@link Void} if statement does not have argument.)
63 * @param <D> Class representing declared version of this statement.
64 * @param <E> Class representing effective version of this statement.
66 public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
67 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
69 @SuppressWarnings("unchecked")
70 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
71 final Class<N> namespace) {
72 if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
73 return Optional.of((Map<K, V>) schemaTreeNamespace());
75 return super.getNamespaceContents(namespace);
79 * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
81 protected final Optional<DataSchemaNode> findDataSchemaNode(final QName name) {
82 // Only DataNodeContainer subclasses should be calling this method
83 verify(this instanceof DataNodeContainer);
84 final SchemaTreeEffectiveStatement<?> child = schemaTreeNamespace().get(requireNonNull(name));
85 return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty();
88 protected abstract Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
92 * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
93 * namespace based of effective substatements.
95 * @param <A> Argument type ({@link Void} if statement does not have argument.)
96 * @param <D> Class representing declared version of this statement.
97 * @param <E> Class representing effective version of this statement.
99 public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
100 E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
102 @SuppressWarnings("unchecked")
103 protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
104 final Class<N> namespace) {
105 if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) {
106 return Optional.of((Map<K, V>) dataTreeNamespace());
108 return super.getNamespaceContents(namespace);
111 protected abstract Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
115 * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
118 * @param <A> Argument type ({@link Void} if statement does not have argument.)
119 * @param <D> Class representing declared version of this statement.
121 public abstract static class Default<A, D extends DeclaredStatement<A>>
122 extends AbstractDeclaredEffectiveStatement<A, D> {
123 private final @NonNull D declared;
125 protected Default(final D declared) {
126 this.declared = requireNonNull(declared);
130 public final D getDeclared() {
136 * Utility class for implementing DataNodeContainer-type statements.
138 public abstract static class DefaultDataNodeContainer<A, D extends DeclaredStatement<A>> extends Default<A, D>
139 implements DataNodeContainerMixin<A, D> {
140 private final @NonNull ImmutableMap<QName, DataSchemaNode> dataChildren;
141 private final @NonNull Object substatements;
143 protected DefaultDataNodeContainer(final D declared, final StatementSourceReference ref,
144 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
146 this.substatements = maskList(substatements);
148 // Note: we do not leak this map, so iteration order does not matter
149 final Map<QName, DataSchemaNode> tmp = new HashMap<>();
151 for (EffectiveStatement<?, ?> stmt : effectiveSubstatements()) {
152 if (stmt instanceof DataSchemaNode) {
153 final DataSchemaNode node = (DataSchemaNode) stmt;
154 final QName id = node.getQName();
155 final DataSchemaNode prev = tmp.put(id, node);
156 SourceException.throwIf(prev != null, ref,
157 "Cannot add child with name %s, a conflicting child already exists", id);
161 dataChildren = ImmutableMap.copyOf(tmp);
165 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
166 return unmaskList(substatements);
170 public final Optional<DataSchemaNode> findDataChildByName(final QName name) {
171 return Optional.ofNullable(dataChildren.get(requireNonNull(name)));
176 * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement.
177 * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in
178 * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via
179 * {@code uses} or similar.
181 * @param <A> Argument type ({@link Void} if statement does not have argument.)
182 * @param <D> Class representing declared version of this statement.
184 public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
185 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
186 extends DefaultArgument<A, D> {
187 private final @NonNull Object substatements;
189 protected WithSubstatements(final D declared,
190 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
192 this.substatements = maskList(substatements);
196 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
197 return unmaskList(substatements);
201 protected DefaultArgument(final D declared) {
206 public final A argument() {
207 return getDeclared().argument();
212 * A building block on top of {@link Default}, which adds an explicit argument value, which is not related to the
213 * context. This is mostly useful when the effective argument value reflects additional statements and similar.
215 * @param <A> Argument type ({@link Void} if statement does not have argument.)
216 * @param <D> Class representing declared version of this statement.
218 public abstract static class DefaultWithArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
219 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>>
220 extends DefaultWithArgument<A, D> {
221 private final @NonNull Object substatements;
223 protected WithSubstatements(final D declared, final A argument,
224 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
225 super(declared, argument);
226 this.substatements = maskList(substatements);
230 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
231 return unmaskList(substatements);
235 private final A argument;
237 protected DefaultWithArgument(final D declared, final A argument) {
239 this.argument = argument;
243 public final A argument() {
249 * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
251 * @param <A> Argument type ({@link Void} if statement does not have argument.)
252 * @param <D> Class representing declared version of this statement.
253 * @param <E> Class representing effective version of this statement.
255 public abstract static class DefaultWithSchemaTree<A, D extends DeclaredStatement<A>,
256 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
257 public abstract static class WithSubstatements<A, D extends DeclaredStatement<A>,
258 E extends SchemaTreeAwareEffectiveStatement<A, D>> extends DefaultWithSchemaTree<A, D, E> {
259 private final @NonNull Object substatements;
261 protected WithSubstatements(final D declared,
262 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
263 final StatementSourceReference ref) {
264 super(declared, substatements, ref);
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,
278 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
279 final StatementSourceReference ref) {
280 this.declared = requireNonNull(declared);
281 this.schemaTree = ImmutableMap.copyOf(createSchemaTreeNamespace(ref, 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,
310 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
311 final StatementSourceReference ref) {
312 super(declared, substatements, ref);
313 this.substatements = maskList(substatements);
317 public final ImmutableList<? extends EffectiveStatement<?, ?>> effectiveSubstatements() {
318 return unmaskList(substatements);
322 private final @NonNull ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTree;
323 private final @NonNull ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTree;
324 private final @NonNull D declared;
326 protected DefaultWithDataTree(final D declared,
327 final ImmutableList<? extends EffectiveStatement<?, ?>> substatements,
328 final StatementSourceReference ref) {
329 this.declared = requireNonNull(declared);
330 final Map<QName, SchemaTreeEffectiveStatement<?>> schema = createSchemaTreeNamespace(ref, substatements);
331 this.schemaTree = ImmutableMap.copyOf(schema);
332 this.dataTree = createDataTreeNamespace(ref, schema.values(), schemaTree);
336 public final D getDeclared() {
341 protected final Map<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
346 protected final Map<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {