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