Move declaredInstance out of StatementContextBase
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.spi.meta;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.base.VerifyException;
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Streams;
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.Optional;
18 import java.util.stream.Stream;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.opendaylight.yangtools.yang.common.QNameModule;
22 import org.opendaylight.yangtools.yang.common.YangVersion;
23 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
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.StatementDefinition;
28 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
29 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
30 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
31
32 /**
33  * An inference context associated with an instance of a statement.
34  *
35  * @param <A> Argument type
36  * @param <D> Declared Statement representation
37  * @param <E> Effective Statement representation
38  */
39 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
40     /**
41      * Returns the origin of the statement.
42      *
43      * @return origin of statement
44      */
45     @NonNull StatementSource getStatementSource();
46
47     /**
48      * Returns a reference to statement source.
49      *
50      * @return reference of statement source
51      */
52     @NonNull StatementSourceReference getStatementSourceReference();
53
54     /**
55      * See {@link StatementSupport#getPublicView()}.
56      */
57     @NonNull StatementDefinition getPublicDefinition();
58
59     /**
60      * Return the parent statement context, or null if this is the root statement.
61      *
62      * @return context of parent of statement, or null if this is the root statement.
63      */
64     @Nullable StmtContext<?, ?, ?> getParentContext();
65
66     /**
67      * Return the parent statement context, forcing a VerifyException if this is the root statement.
68      *
69      * @return context of parent of statement
70      * @throws VerifyException if this statement is the root statement
71      */
72     default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
73         return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
74     }
75
76     /**
77      * Return the statement argument in literal format.
78      *
79      * @return raw statement argument string, or null if this statement does not have an argument.
80      */
81     @Nullable String rawStatementArgument();
82
83     /**
84      * Return the statement argument in literal format.
85      *
86      * @return raw statement argument string
87      * @throws VerifyException if this statement does not have an argument
88      */
89     default @NonNull String coerceRawStatementArgument() {
90         return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
91     }
92
93     /**
94      * Return the statement argument.
95      *
96      * @return statement argument, or null if this statement does not have an argument
97      */
98     @Nullable A getStatementArgument();
99
100     /**
101      * Return the statement argument in literal format.
102      *
103      * @return raw statement argument string
104      * @throws VerifyException if this statement does not have an argument
105      */
106     default @NonNull A coerceStatementArgument() {
107         return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
108     }
109
110     /**
111      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
112      * {@link Optional#empty()} is returned.
113      *
114      * @return Optional SchemaPath
115      */
116     @NonNull Optional<SchemaPath> getSchemaPath();
117
118     boolean isConfiguration();
119
120     boolean isEnabledSemanticVersioning();
121
122     /**
123      * Return a value associated with specified key within a namespace.
124      *
125      * @param type Namespace type
126      * @param key Key
127      * @param <K> namespace key type
128      * @param <V> namespace value type
129      * @param <N> namespace type
130      * @param <T> key type
131      * @return Value, or null if there is no element
132      * @throws NamespaceNotAvailableException when the namespace is not available.
133      */
134     @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key);
135
136     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
137
138     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
139
140     @NonNull StmtContext<?, ?, ?> getRoot();
141
142     /**
143      * Return declared substatements. These are the statements which are explicitly written in the source model.
144      *
145      * @return Collection of declared substatements
146      */
147     @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
148
149     /**
150      * Return effective substatements. These are the statements which are added as this statement's substatements
151      * complete their effective model phase.
152      *
153      * @return Collection of declared substatements
154      */
155     @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
156
157     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
158         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
159     }
160
161     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
162         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
163     }
164
165     /**
166      * Builds {@link DeclaredStatement} for statement context.
167      */
168     D buildDeclared();
169
170     /**
171      * Builds {@link EffectiveStatement} for statement context.
172      */
173     E buildEffective();
174
175     boolean isSupportedToBuildEffective();
176
177     boolean isSupportedByFeatures();
178
179     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
180
181     /**
182      * Return the executive summary of the copy process that has produced this context.
183      *
184      * @return A simplified summary of the copy process.
185      */
186     CopyHistory getCopyHistory();
187
188     /**
189      * Return the statement context of the original definition, if this statement is an instantiated copy.
190      *
191      * @return Original definition, if this statement was copied.
192      */
193     // FIXME: 5.0.0: this should return Optional<? extends StmtContext<A, D, E>>, is that feasible?
194     Optional<StmtContext<?, ?, ?>> getOriginalCtx();
195
196     /**
197      * Return the context of the previous copy of this statement -- effectively walking towards the source origin
198      * of this statement.
199      *
200      * @return Context of the previous copy of this statement, if this statement has been copied.
201      */
202     // FIXME: 5.0.0: this should return Optional<? extends StmtContext<A, D, E>>
203     Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
204
205     ModelProcessingPhase getCompletedPhase();
206
207     /**
208      * Return version of root statement context.
209      *
210      * @return version of root statement context
211      */
212     @NonNull YangVersion getRootVersion();
213
214     /**
215      * An mutable view of an inference context associated with an instance of a statement.
216      *
217      * @param <A> Argument type
218      * @param <D> Declared Statement representation
219      * @param <E> Effective Statement representation
220      */
221     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
222             extends StmtContext<A, D, E> {
223
224         @Override
225         Mutable<?, ?, ?> getParentContext();
226
227         @Override
228         default Mutable<?, ?, ?> coerceParentContext() {
229             return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
230         }
231
232         /**
233          * Associate a value with a key within a namespace.
234          *
235          * @param type Namespace type
236          * @param key Key
237          * @param value value
238          * @param <K> namespace key type
239          * @param <V> namespace value type
240          * @param <N> namespace type
241          * @param <T> key type
242          * @param <U> value type
243          * @throws NamespaceNotAvailableException when the namespace is not available.
244          */
245         <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
246                 U value);
247
248         @Override
249         Mutable<?, ?, ?> getRoot();
250
251         /**
252          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
253          * child and recording copy type. Resulting object may only be added as a child of this statement.
254          *
255          * @param stmt Statement to be used as a template
256          * @param type Type of copy to record in history
257          * @param targetModule Optional new target module
258          * @return copy of statement considering {@link CopyType} (augment, uses)
259          *
260          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
261          *                                  from an alien implementation.
262          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
263          */
264         // FIXME: 4.0.0: remove generic arguments X, Y, Z. Callers should not care, as the returned copy can actually
265         //               be an encapsulating implicit statement.
266         <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
267                 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
268
269         /**
270          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
271          * child and recording copy type. Resulting object may only be added as a child of this statement.
272          *
273          * @param stmt Statement to be used as a template
274          * @param type Type of copy to record in history
275          * @return copy of statement considering {@link CopyType} (augment, uses)
276          *
277          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
278          *                                  from an alien implementation.
279          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
280          */
281         default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
282                 final StmtContext<X, Y, Z> stmt, final CopyType type) {
283             return childCopyOf(stmt, type, null);
284         }
285
286         @Override
287         default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
288             return mutableDeclaredSubstatements();
289         }
290
291         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
292
293         @Override
294         default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
295             return mutableEffectiveSubstatements();
296         }
297
298         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
299
300         /**
301          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
302          * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
303          * this action is resolved. If the action cannot be resolved, model processing will fail.
304          *
305          * @param phase Target phase in which the action will resolved.
306          * @return A new action builder.
307          * @throws NullPointerException if the specified phase is null
308          */
309         @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
310
311         /**
312          * Adds s statement to namespace map with a key.
313          *
314          * @param namespace
315          *            {@link StatementNamespace} child that determines namespace to be added to
316          * @param key
317          *            of type according to namespace class specification
318          * @param stmt
319          *            to be added to namespace map
320          */
321         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
322                 StmtContext<?, ?, ?> stmt);
323
324         /**
325          * Set version of root statement context.
326          *
327          * @param version
328          *            of root statement context
329          */
330         void setRootVersion(YangVersion version);
331
332         /**
333          * Add mutable statement to seal. Each mutable statement must be sealed
334          * as the last step of statement parser processing.
335          *
336          * @param mutableStatement
337          *            mutable statement which should be sealed
338          */
339         void addMutableStmtToSeal(MutableStatement mutableStatement);
340
341         /**
342          * Add required module. Based on these dependencies are collected required sources from library sources.
343          *
344          * @param dependency
345          *            SourceIdentifier of module required by current root
346          *            context
347          */
348         /*
349          * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
350          *        getter -- which makes it rather strange. At some point this method needs to be deprecated and its
351          *        users migrated to use proper global namespace.
352          */
353         void addRequiredSource(SourceIdentifier dependency);
354
355         void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
356
357         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
358
359         /**
360          * Set identifier of current root context.
361          *
362          * @param identifier
363          *            of current root context, must not be null
364          */
365         void setRootIdentifier(SourceIdentifier identifier);
366
367         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
368
369         // FIXME: this seems to be unused, but looks useful.
370         void setCompletedPhase(ModelProcessingPhase completedPhase);
371     }
372 }