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