2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
10 import static com.google.common.base.Verify.verifyNotNull;
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;
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.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;
33 * An inference context associated with an instance of a statement.
35 * @param <A> Argument type
36 * @param <D> Declared Statement representation
37 * @param <E> Effective Statement representation
39 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
40 extends BoundStmtCtx<A> {
41 @Deprecated(forRemoval = true)
42 default @NonNull StatementDefinition getPublicDefinition() {
43 return publicDefinition();
46 @Deprecated(forRemoval = true)
47 default @NonNull StatementSource getStatementSource() {
51 @Deprecated(forRemoval = true)
52 default @NonNull StatementSourceReference getStatementSourceReference() {
53 return sourceReference();
57 * Return the statement argument in literal format.
59 * @return raw statement argument string, or null if this statement does not have an argument.
60 * @deprecated Use {@link #rawArgument()} instead.
62 @Deprecated(forRemoval = true)
63 default @Nullable String rawStatementArgument() {
68 * Return the statement argument in literal format.
70 * @return raw statement argument string
71 * @throws VerifyException if this statement does not have an argument
72 * @deprecated Use {@link #getRawArgument()} instead.
74 @Deprecated(forRemoval = true)
75 default @NonNull String coerceRawStatementArgument() {
76 return getRawArgument();
80 * Return the statement argument.
82 * @return statement argument, or null if this statement does not have an argument
83 * @deprecated Use {@link #argument()} instead.
85 @Deprecated(forRemoval = true)
86 default @Nullable A getStatementArgument() {
91 * Return the statement argument in literal format.
93 * @return raw statement argument string
94 * @throws VerifyException if this statement does not have an argument
95 * @deprecated Use {@link #getArgument()} instead.
97 @Deprecated(forRemoval = true)
98 default @NonNull A coerceStatementArgument() {
103 * Return the parent statement context, or null if this is the root statement.
105 * @return context of parent of statement, or null if this is the root statement.
107 @Nullable StmtContext<?, ?, ?> getParentContext();
110 * Return the parent statement context, forcing a VerifyException if this is the root statement.
112 * @return context of parent of statement
113 * @throws VerifyException if this statement is the root statement
115 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
116 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
119 boolean isConfiguration();
121 boolean isEnabledSemanticVersioning();
124 * Return a value associated with specified key within a namespace.
126 * @param type Namespace type
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.
135 <K, V, T extends K, N extends IdentifierNamespace<K, V>> @Nullable V getFromNamespace(Class<@NonNull N> type,
138 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
140 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
142 @NonNull RootStmtContext<?, ?, ?> getRoot();
145 * Return declared substatements. These are the statements which are explicitly written in the source model.
147 * @return Collection of declared substatements
149 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
152 * Return effective substatements. These are the statements which are added as this statement's substatements
153 * complete their effective model phase.
155 * @return Collection of declared substatements
157 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
159 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
160 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
163 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
164 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
168 * Builds {@link DeclaredStatement} for statement context.
173 * Builds {@link EffectiveStatement} for statement context.
177 boolean isSupportedToBuildEffective();
179 boolean isSupportedByFeatures();
181 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
184 * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
185 * - getCopyHistory() is a brief summary of what went on
186 * - getOriginalContext() points to the CopyHistory.ORIGINAL
187 * - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
188 * at getOriginalContext()
190 * When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
191 * intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
192 * account when creating the argument. At least parts of this are only needed during buildEffective()
193 * and hence should become arguments to that method.
197 * Return the executive summary of the copy process that has produced this context.
199 * @return A simplified summary of the copy process.
201 CopyHistory getCopyHistory();
204 * Return the statement context of the original definition, if this statement is an instantiated copy.
206 * @return Original definition, if this statement was copied.
208 Optional<StmtContext<A, D, E>> getOriginalCtx();
211 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
214 * @return Context of the previous copy of this statement, if this statement has been copied.
216 Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
218 ModelProcessingPhase getCompletedPhase();
221 * Return version of root statement context.
223 * @return version of root statement context
225 @NonNull YangVersion getRootVersion();
228 * An mutable view of an inference context associated with an instance of a statement.
230 * @param <A> Argument type
231 * @param <D> Declared Statement representation
232 * @param <E> Effective Statement representation
234 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
235 extends StmtContext<A, D, E> {
238 Mutable<?, ?, ?> getParentContext();
241 default Mutable<?, ?, ?> coerceParentContext() {
242 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
246 * Associate a value with a key within a namespace.
248 * @param type Namespace type
251 * @param <K> namespace key type
252 * @param <V> namespace value type
253 * @param <N> namespace type
254 * @param <T> key type
255 * @param <U> value type
256 * @throws NamespaceNotAvailableException when the namespace is not available.
258 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<@NonNull N> type,
262 RootStmtContext.Mutable<?, ?, ?> getRoot();
265 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
266 * child and recording copy type. Resulting object may only be added as a child of this statement.
268 * @param stmt Statement to be used as a template
269 * @param type Type of copy to record in history
270 * @param targetModule Optional new target module
271 * @return copy of statement considering {@link CopyType} (augment, uses)
273 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
274 * from an alien implementation.
275 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
277 Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
280 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
281 * child and recording copy type. Resulting object may only be added as a child of this statement.
283 * @param stmt Statement to be used as a template
284 * @param type Type of copy to record in history
285 * @return copy of statement considering {@link CopyType} (augment, uses)
287 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
288 * from an alien implementation.
289 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
291 default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
292 return childCopyOf(stmt, type, null);
296 @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
297 @Nullable QNameModule targetModule);
300 default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
301 return mutableDeclaredSubstatements();
304 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
307 default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
308 return mutableEffectiveSubstatements();
311 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
314 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
315 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
316 * this action is resolved. If the action cannot be resolved, model processing will fail.
318 * @param phase Target phase in which the action will resolved.
319 * @return A new action builder.
320 * @throws NullPointerException if the specified phase is null
322 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
325 * Adds s statement to namespace map with a key.
328 * {@link StatementNamespace} child that determines namespace to be added to
330 * of type according to namespace class specification
332 * to be added to namespace map
334 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
335 StmtContext<?, ?, ?> stmt);
338 * Set version of root statement context.
341 * of root statement context
343 void setRootVersion(YangVersion version);
346 * Add mutable statement to seal. Each mutable statement must be sealed
347 * as the last step of statement parser processing.
349 * @param mutableStatement
350 * mutable statement which should be sealed
352 void addMutableStmtToSeal(MutableStatement mutableStatement);
355 * Add required module. Based on these dependencies are collected required sources from library sources.
358 * SourceIdentifier of module required by current root
362 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
363 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
364 * users migrated to use proper global namespace.
366 void addRequiredSource(SourceIdentifier dependency);
368 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
370 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
373 * Set identifier of current root context.
376 * of current root context, must not be null
378 void setRootIdentifier(SourceIdentifier identifier);
380 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
384 * Search of any child statement context of specified type and return its argument. If such a statement exists, it
385 * is assumed to have the right argument. Users should be careful to use this method for statements which have
386 * cardinality {@code 0..1}, otherwise this method can return any one of the statement's argument.
389 * The default implementation defers to
390 * {@link StmtContextDefaults#findSubstatementArgument(StmtContext, Class)}, subclasses are expected to provide
391 * optimized implementation if possible.
393 * @param <X> Substatement argument type
394 * @param <Z> Substatement effective statement representation
395 * @param type Effective statement representation being look up
396 * @return {@link Optional#empty()} if no statement exists, otherwise the argument value
398 default <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
399 final @NonNull Class<Z> type) {
400 return StmtContextDefaults.findSubstatementArgument(this, type);
404 * Check if there is any child statement context of specified type.
407 * The default implementation defers to {@link StmtContextDefaults#hasSubstatement(StmtContext, Class)},
408 * subclasses are expected to provide optimized implementation if possible.
410 * @param type Effective statement representation being look up
411 * @return True if such a child statement exists, false otherwise
413 default boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
414 return StmtContextDefaults.hasSubstatement(this, type);