95341eeb27138e454d71bc1aac39609b6f66358a
[yangtools.git] / yang / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / AbstractDeclaredEffectiveStatement.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt;
9
10 import static com.google.common.base.Verify.verify;
11 import static java.util.Objects.requireNonNull;
12
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.Map;
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;
34
35 /**
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.
38  *
39  * <p>
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.
43  *
44  * @param <A> Argument type ({@link Void} if statement does not have argument.)
45  * @param <D> Class representing declared version of this statement.
46  */
47 @Beta
48 public abstract class AbstractDeclaredEffectiveStatement<A, D extends DeclaredStatement<A>>
49         extends AbstractEffectiveStatement<A, D> {
50     @Override
51     public final StatementSource getStatementSource() {
52         return StatementSource.DECLARATION;
53     }
54
55     @Override
56     public final StatementDefinition statementDefinition() {
57         return getDeclared().statementDefinition();
58     }
59
60     @Override
61     public abstract @NonNull D getDeclared();
62
63     /**
64      * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema
65      * tree namespace based of effective substatements.
66      *
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.
70      */
71     public abstract static class WithSchemaTree<A, D extends DeclaredStatement<A>,
72             E extends SchemaTreeAwareEffectiveStatement<A, D>> extends AbstractDeclaredEffectiveStatement<A, D> {
73         @Override
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());
79             }
80             return super.getNamespaceContents(namespace);
81         }
82
83         /**
84          * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}.
85          */
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();
91         }
92
93         protected abstract ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace();
94     }
95
96     /**
97      * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree
98      * namespace based of effective substatements.
99      *
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.
103      */
104     public abstract static class WithDataTree<A, D extends DeclaredStatement<A>,
105             E extends DataTreeAwareEffectiveStatement<A, D>> extends WithSchemaTree<A, D, E> {
106         @Override
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());
112             }
113             return super.getNamespaceContents(namespace);
114         }
115
116         protected abstract ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace();
117     }
118
119     /**
120      * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared
121      * statement.
122      *
123      * @param <A> Argument type ({@link Void} if statement does not have argument.)
124      * @param <D> Class representing declared version of this statement.
125      */
126     public abstract static class Default<A, D extends DeclaredStatement<A>>
127             extends AbstractDeclaredEffectiveStatement<A, D> {
128         private final @NonNull D declared;
129
130         protected Default(final D declared) {
131             this.declared = requireNonNull(declared);
132         }
133
134         @Override
135         public final D getDeclared() {
136             return declared;
137         }
138     }
139
140     /**
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.
145      *
146      * @param <A> Argument type ({@link Void} if statement does not have argument.)
147      * @param <D> Class representing declared version of this statement.
148      */
149     public abstract static class DefaultArgument<A, D extends DeclaredStatement<A>> extends Default<A, D> {
150         protected DefaultArgument(final D declared) {
151             super(declared);
152         }
153
154         @Override
155         public final @Nullable A argument() {
156             return getDeclared().argument();
157         }
158     }
159
160     /**
161      * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked).
162      *
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.
166      */
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;
171
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);
177         }
178
179         @Override
180         public final D getDeclared() {
181             return declared;
182         }
183
184         @Override
185         protected final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
186             return schemaTree;
187         }
188     }
189
190     /**
191      * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated
192      * (and checked).
193      *
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.
197      */
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;
203
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);
210         }
211
212         @Override
213         public final D getDeclared() {
214             return declared;
215         }
216
217         @Override
218         protected final ImmutableMap<QName, SchemaTreeEffectiveStatement<?>> schemaTreeNamespace() {
219             return schemaTree;
220         }
221
222         @Override
223         protected final ImmutableMap<QName, DataTreeEffectiveStatement<?>> dataTreeNamespace() {
224             return dataTree;
225         }
226     }
227 }