Populate xpath/ hierarchy
[yangtools.git] / parser / 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.annotations.Beta;
13 import com.google.common.base.VerifyException;
14 import com.google.common.collect.Iterables;
15 import com.google.common.collect.Streams;
16 import java.util.Collection;
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.meta.DeclaredStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementOrigin;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
29
30 /**
31  * An inference context associated with an instance of a statement.
32  *
33  * @param <A> Argument type
34  * @param <D> Declared Statement representation
35  * @param <E> Effective Statement representation
36  */
37 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
38         extends NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
39     @Deprecated(forRemoval = true)
40     default @NonNull StatementDefinition getPublicDefinition() {
41         return publicDefinition();
42     }
43
44     @Deprecated(forRemoval = true)
45     default @NonNull StatementOrigin getStatementSource() {
46         return origin();
47     }
48
49     @Deprecated(forRemoval = true)
50     default @NonNull StatementSourceReference getStatementSourceReference() {
51         return sourceReference();
52     }
53
54     /**
55      * Return the statement argument in literal format.
56      *
57      * @return raw statement argument string, or null if this statement does not have an argument.
58      * @deprecated Use {@link #rawArgument()} instead.
59      */
60     @Deprecated(forRemoval = true)
61     default @Nullable String rawStatementArgument() {
62         return rawArgument();
63     }
64
65     /**
66      * Return the statement argument in literal format.
67      *
68      * @return raw statement argument string
69      * @throws VerifyException if this statement does not have an argument
70      * @deprecated Use {@link #getRawArgument()} instead.
71      */
72     @Deprecated(forRemoval = true)
73     default @NonNull String coerceRawStatementArgument() {
74         return getRawArgument();
75     }
76
77     /**
78      * Return the statement argument.
79      *
80      * @return statement argument, or null if this statement does not have an argument
81      * @deprecated Use {@link #argument()} instead.
82      */
83     @Deprecated(forRemoval = true)
84     default @Nullable A getStatementArgument() {
85         return argument();
86     }
87
88     /**
89      * Return the statement argument in literal format.
90      *
91      * @return raw statement argument string
92      * @throws VerifyException if this statement does not have an argument
93      * @deprecated Use {@link #getArgument()} instead.
94      */
95     @Deprecated(forRemoval = true)
96     default @NonNull A coerceStatementArgument() {
97         return getArgument();
98     }
99
100     /**
101      * Return the parent statement context, or null if this is the root statement.
102      *
103      * @return context of parent of statement, or null if this is the root statement.
104      */
105     @Nullable StmtContext<?, ?, ?> getParentContext();
106
107     /**
108      * Return the parent statement context, forcing a VerifyException if this is the root statement.
109      *
110      * @return context of parent of statement
111      * @throws VerifyException if this statement is the root statement
112      */
113     default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
114         return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
115     }
116
117     /**
118      * Returns the model root for this statement.
119      *
120      * @return root context of statement
121      */
122     @NonNull RootStmtContext<?, ?, ?> getRoot();
123
124     /**
125      * Return declared substatements. These are the statements which are explicitly written in the source model.
126      *
127      * @return Collection of declared substatements
128      */
129     @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
130
131     /**
132      * Return effective substatements. These are the statements which are added as this statement's substatements
133      * complete their effective model phase.
134      *
135      * @return Collection of declared substatements
136      */
137     @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
138
139     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
140         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
141     }
142
143     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
144         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
145     }
146
147     /**
148      * Return the {@link EffectiveStatement} for statement context, creating it if required. Implementations of this
149      * method are required to memoize the returned object, so that subsequent invocation return the same object.
150      *
151      * <p>
152      * If {@link #isSupportedToBuildEffective()} returns {@code false}, this method's behaviour is undefined.
153      *
154      * @return Effective statement instance.
155      */
156     @NonNull E buildEffective();
157
158     boolean isSupportedToBuildEffective();
159
160     boolean isSupportedByFeatures();
161
162     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
163
164     /*
165      * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
166      *        - copyHistory() is a brief summary of what went on
167      *        - getOriginalContext() points to the CopyHistory.ORIGINAL
168      *        - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
169      *          at getOriginalContext()
170      *
171      *        When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
172      *        intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
173      *        account when creating the argument. At least parts of this are only needed during buildEffective()
174      *        and hence should become arguments to that method.
175      */
176
177     /**
178      * Return the statement context of the original definition, if this statement is an instantiated copy.
179      *
180      * @return Original definition, if this statement was copied.
181      */
182     Optional<StmtContext<A, D, E>> getOriginalCtx();
183
184     /**
185      * Return the context of the previous copy of this statement -- effectively walking towards the source origin
186      * of this statement.
187      *
188      * @return Context of the previous copy of this statement, if this statement has been copied.
189      */
190     Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
191
192     ModelProcessingPhase getCompletedPhase();
193
194     /**
195      * An mutable view of an inference context associated with an instance of a statement.
196      *
197      * @param <A> Argument type
198      * @param <D> Declared Statement representation
199      * @param <E> Effective Statement representation
200      */
201     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
202             extends StmtContext<A, D, E> {
203
204         @Override
205         Mutable<?, ?, ?> getParentContext();
206
207         @Override
208         default Mutable<?, ?, ?> coerceParentContext() {
209             return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
210         }
211
212         /**
213          * Associate a value with a key within a namespace.
214          *
215          * @param type Namespace type
216          * @param key Key
217          * @param value value
218          * @param <K> namespace key type
219          * @param <V> namespace value type
220          * @param <N> namespace type
221          * @param <T> key type
222          * @param <U> value type
223          * @throws NamespaceNotAvailableException when the namespace is not available.
224          */
225         <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(Class<@NonNull N> type,
226                 T key, U value);
227
228         @Override
229         RootStmtContext.Mutable<?, ?, ?> getRoot();
230
231         /**
232          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
233          * child and recording copy type. Resulting object may only be added as a child of this statement.
234          *
235          * @param stmt Statement to be used as a template
236          * @param type Type of copy to record in history
237          * @param targetModule Optional new target module
238          * @return copy of statement considering {@link CopyType} (augment, uses)
239          *
240          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
241          *                                  from an alien implementation.
242          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
243          */
244         Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
245
246         /**
247          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
248          * child and recording copy type. Resulting object may only be added as a child of this statement.
249          *
250          * @param stmt Statement to be used as a template
251          * @param type Type of copy to record in history
252          * @return copy of statement considering {@link CopyType} (augment, uses)
253          *
254          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
255          *                                  from an alien implementation.
256          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
257          */
258         default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
259             return childCopyOf(stmt, type, null);
260         }
261
262         /**
263          * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
264          * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
265          *
266          * @param parent Parent of the replica statement
267          * @return replica of this statement
268          * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
269          *                                  comes from an alien implementation.
270          */
271         @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
272
273         @Beta
274         @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
275                 @Nullable QNameModule targetModule);
276
277         @Override
278         default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
279             return mutableDeclaredSubstatements();
280         }
281
282         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
283
284         @Override
285         default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
286             return mutableEffectiveSubstatements();
287         }
288
289         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
290
291         /**
292          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
293          * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
294          * this action is resolved. If the action cannot be resolved, model processing will fail.
295          *
296          * @param phase Target phase in which the action will resolved.
297          * @return A new action builder.
298          * @throws NullPointerException if the specified phase is null
299          */
300         @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
301
302         /**
303          * Adds s statement to namespace map with a key.
304          *
305          * @param namespace
306          *            {@link StatementNamespace} child that determines namespace to be added to
307          * @param key
308          *            of type according to namespace class specification
309          * @param stmt
310          *            to be added to namespace map
311          */
312         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
313                 StmtContext<?, ?, ?> stmt);
314
315         /**
316          * Set version of root statement context.
317          *
318          * @param version
319          *            of root statement context
320          */
321         void setRootVersion(YangVersion version);
322
323         /**
324          * Add required module. Based on these dependencies are collected required sources from library sources.
325          *
326          * @param dependency
327          *            SourceIdentifier of module required by current root
328          *            context
329          */
330         /*
331          * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
332          *        getter -- which makes it rather strange. At some point this method needs to be deprecated and its
333          *        users migrated to use proper global namespace.
334          */
335         void addRequiredSource(SourceIdentifier dependency);
336
337         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
338
339         /**
340          * Set identifier of current root context.
341          *
342          * @param identifier
343          *            of current root context, must not be null
344          */
345         void setRootIdentifier(SourceIdentifier identifier);
346
347         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
348     }
349 }