Un-deprecate CopyableNode, AddedByUsesAware
[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.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
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 StatementSource getStatementSource() {
46         return source();
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     boolean isEnabledSemanticVersioning();
118
119     /**
120      * Returns the model root for this statement.
121      *
122      * @return root context of statement
123      */
124     @NonNull RootStmtContext<?, ?, ?> getRoot();
125
126     /**
127      * Return declared substatements. These are the statements which are explicitly written in the source model.
128      *
129      * @return Collection of declared substatements
130      */
131     @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
132
133     /**
134      * Return effective substatements. These are the statements which are added as this statement's substatements
135      * complete their effective model phase.
136      *
137      * @return Collection of declared substatements
138      */
139     @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
140
141     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
142         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
143     }
144
145     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
146         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
147     }
148
149     /**
150      * Return the {@link EffectiveStatement} for statement context, creating it if required. Implementations of this
151      * method are required to memoize the returned object, so that subsequent invocation return the same object.
152      *
153      * <p>
154      * If {@link #isSupportedToBuildEffective()} returns {@code false}, this method's behaviour is undefined.
155      *
156      * @return Effective statement instance.
157      */
158     @NonNull E buildEffective();
159
160     boolean isSupportedToBuildEffective();
161
162     boolean isSupportedByFeatures();
163
164     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
165
166     /*
167      * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
168      *        - copyHistory() is a brief summary of what went on
169      *        - getOriginalContext() points to the CopyHistory.ORIGINAL
170      *        - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
171      *          at getOriginalContext()
172      *
173      *        When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
174      *        intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
175      *        account when creating the argument. At least parts of this are only needed during buildEffective()
176      *        and hence should become arguments to that method.
177      */
178
179     /**
180      * Return the statement context of the original definition, if this statement is an instantiated copy.
181      *
182      * @return Original definition, if this statement was copied.
183      */
184     Optional<StmtContext<A, D, E>> getOriginalCtx();
185
186     /**
187      * Return the context of the previous copy of this statement -- effectively walking towards the source origin
188      * of this statement.
189      *
190      * @return Context of the previous copy of this statement, if this statement has been copied.
191      */
192     Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
193
194     ModelProcessingPhase getCompletedPhase();
195
196     /**
197      * An mutable view of an inference context associated with an instance of a statement.
198      *
199      * @param <A> Argument type
200      * @param <D> Declared Statement representation
201      * @param <E> Effective Statement representation
202      */
203     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
204             extends StmtContext<A, D, E> {
205
206         @Override
207         Mutable<?, ?, ?> getParentContext();
208
209         @Override
210         default Mutable<?, ?, ?> coerceParentContext() {
211             return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
212         }
213
214         /**
215          * Associate a value with a key within a namespace.
216          *
217          * @param type Namespace type
218          * @param key Key
219          * @param value value
220          * @param <K> namespace key type
221          * @param <V> namespace value type
222          * @param <N> namespace type
223          * @param <T> key type
224          * @param <U> value type
225          * @throws NamespaceNotAvailableException when the namespace is not available.
226          */
227         <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(Class<@NonNull N> type,
228                 T key, U value);
229
230         @Override
231         RootStmtContext.Mutable<?, ?, ?> getRoot();
232
233         /**
234          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
235          * child and recording copy type. Resulting object may only be added as a child of this statement.
236          *
237          * @param stmt Statement to be used as a template
238          * @param type Type of copy to record in history
239          * @param targetModule Optional new target module
240          * @return copy of statement considering {@link CopyType} (augment, uses)
241          *
242          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
243          *                                  from an alien implementation.
244          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
245          */
246         Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
247
248         /**
249          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
250          * child and recording copy type. Resulting object may only be added as a child of this statement.
251          *
252          * @param stmt Statement to be used as a template
253          * @param type Type of copy to record in history
254          * @return copy of statement considering {@link CopyType} (augment, uses)
255          *
256          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
257          *                                  from an alien implementation.
258          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
259          */
260         default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
261             return childCopyOf(stmt, type, null);
262         }
263
264         /**
265          * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
266          * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
267          *
268          * @param parent Parent of the replica statement
269          * @return replica of this statement
270          * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
271          *                                  comes from an alien implementation.
272          */
273         @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
274
275         @Beta
276         @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
277                 @Nullable QNameModule targetModule);
278
279         @Override
280         default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
281             return mutableDeclaredSubstatements();
282         }
283
284         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
285
286         @Override
287         default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
288             return mutableEffectiveSubstatements();
289         }
290
291         @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
292
293         /**
294          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
295          * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
296          * this action is resolved. If the action cannot be resolved, model processing will fail.
297          *
298          * @param phase Target phase in which the action will resolved.
299          * @return A new action builder.
300          * @throws NullPointerException if the specified phase is null
301          */
302         @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
303
304         /**
305          * Adds s statement to namespace map with a key.
306          *
307          * @param namespace
308          *            {@link StatementNamespace} child that determines namespace to be added to
309          * @param key
310          *            of type according to namespace class specification
311          * @param stmt
312          *            to be added to namespace map
313          */
314         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
315                 StmtContext<?, ?, ?> stmt);
316
317         /**
318          * Set version of root statement context.
319          *
320          * @param version
321          *            of root statement context
322          */
323         void setRootVersion(YangVersion version);
324
325         /**
326          * Add required module. Based on these dependencies are collected required sources from library sources.
327          *
328          * @param dependency
329          *            SourceIdentifier of module required by current root
330          *            context
331          */
332         /*
333          * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
334          *        getter -- which makes it rather strange. At some point this method needs to be deprecated and its
335          *        users migrated to use proper global namespace.
336          */
337         void addRequiredSource(SourceIdentifier dependency);
338
339         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
340
341         /**
342          * Set identifier of current root context.
343          *
344          * @param identifier
345          *            of current root context, must not be null
346          */
347         void setRootIdentifier(SourceIdentifier identifier);
348
349         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
350     }
351 }