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