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