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