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