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;
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.source.SourceIdentifier;
29 * An inference context associated with an instance of a statement.
31 * @param <A> Argument type
32 * @param <D> Declared Statement representation
33 * @param <E> Effective Statement representation
35 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
36 extends NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
38 * Return the parent statement context, or null if this is the root statement.
40 * @return context of parent of statement, or null if this is the root statement.
42 @Nullable StmtContext<?, ?, ?> getParentContext();
45 * Return the parent statement context, forcing a VerifyException if this is the root statement.
47 * @return context of parent of statement
48 * @throws VerifyException if this statement is the root statement
50 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
51 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
55 * Returns the model root for this statement.
57 * @return root context of statement
59 @NonNull RootStmtContext<?, ?, ?> getRoot();
62 * Return declared substatements. These are the statements which are explicitly written in the source model, but
63 * reflect implicit containment statements as well. To but that statement into practical terms, this snippet:
71 * reports the same structure as the canonical verbose equivalent:
81 * Returned collection is therefore well suited for reasoning about the schema tree. It is not appropriate for
82 * populating populating {@link DeclaredStatement#declaredSubstatements()}.
84 * @return Collection of declared substatements
86 @NonNull Collection<? extends @NonNull StmtContext<?, ?, ?>> declaredSubstatements();
89 * Return effective substatements. These are the statements which are added as this statement's substatements
90 * complete their effective model phase.
92 * @return Collection of declared substatements
94 @NonNull Collection<? extends @NonNull StmtContext<?, ?, ?>> effectiveSubstatements();
96 default Iterable<? extends @NonNull StmtContext<?, ?, ?>> allSubstatements() {
97 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
100 default Stream<? extends @NonNull StmtContext<?, ?, ?>> allSubstatementsStream() {
101 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
105 * Return the {@link EffectiveStatement} for statement context, creating it if required. Implementations of this
106 * method are required to memoize the returned object, so that subsequent invocation return the same object.
109 * If {@link #isSupportedToBuildEffective()} returns {@code false}, this method's behaviour is undefined.
111 * @return Effective statement instance.
113 @NonNull E buildEffective();
115 boolean isSupportedToBuildEffective();
117 boolean isSupportedByFeatures();
119 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
122 * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
123 * - copyHistory() is a brief summary of what went on
124 * - getOriginalContext() points to the CopyHistory.ORIGINAL
125 * - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
126 * at getOriginalContext()
128 * When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
129 * intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
130 * account when creating the argument. At least parts of this are only needed during buildEffective()
131 * and hence should become arguments to that method.
135 * Return the statement context of the original definition, if this statement is an instantiated copy.
137 * @return Original definition, if this statement was copied.
139 Optional<StmtContext<A, D, E>> getOriginalCtx();
142 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
145 * @return Context of the previous copy of this statement, if this statement has been copied.
147 Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
150 * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
151 * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
153 * @param parent Parent of the replica statement
154 * @return replica of this statement
155 * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
156 * comes from an alien implementation.
158 @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
161 @NonNull Optional<Mutable<A, D, E>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
162 @Nullable QNameModule targetModule);
164 ModelProcessingPhase getCompletedPhase();
167 * An mutable view of an inference context associated with an instance of a statement.
169 * @param <A> Argument type
170 * @param <D> Declared Statement representation
171 * @param <E> Effective Statement representation
173 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
174 extends StmtContext<A, D, E> {
177 Mutable<?, ?, ?> getParentContext();
180 default Mutable<?, ?, ?> coerceParentContext() {
181 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
185 * Associate a value with a key within a namespace.
187 * @param <K> key type
188 * @param <V> value type
189 * @param type Namespace type
192 * @throws NamespaceNotAvailableException when the namespace is not available.
194 <K, V> void addToNs(@NonNull ParserNamespace<K, V> type, K key, V value);
197 RootStmtContext.Mutable<?, ?, ?> getRoot();
200 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
201 * child and recording copy type. Resulting object may only be added as a child of this statement.
203 * @param stmt Statement to be used as a template
204 * @param type Type of copy to record in history
205 * @param targetModule Optional new target module
206 * @return copy of statement considering {@link CopyType} (augment, uses)
208 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
209 * from an alien implementation.
210 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
212 Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
215 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
216 * child and recording copy type. Resulting object may only be added as a child of this statement.
218 * @param stmt Statement to be used as a template
219 * @param type Type of copy to record in history
220 * @return copy of statement considering {@link CopyType} (augment, uses)
222 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
223 * from an alien implementation.
224 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
226 default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
227 return childCopyOf(stmt, type, null);
231 default Collection<? extends @NonNull StmtContext<?, ?, ?>> declaredSubstatements() {
232 return mutableDeclaredSubstatements();
235 @NonNull Collection<? extends @NonNull Mutable<?, ?, ?>> mutableDeclaredSubstatements();
238 default Collection<? extends @NonNull StmtContext<?, ?, ?>> effectiveSubstatements() {
239 return mutableEffectiveSubstatements();
242 @NonNull Collection<? extends @NonNull Mutable<?, ?, ?>> mutableEffectiveSubstatements();
245 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
246 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
247 * this action is resolved. If the action cannot be resolved, model processing will fail.
249 * @param phase Target phase in which the action will resolved.
250 * @return A new action builder.
251 * @throws NullPointerException if the specified phase is null
253 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
256 * Set version of root statement context.
259 * of root statement context
261 void setRootVersion(YangVersion version);
264 * Add required module. Based on these dependencies are collected required sources from library sources.
267 * SourceIdentifier of module required by current root
271 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
272 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
273 * users migrated to use proper global namespace.
275 void addRequiredSource(SourceIdentifier dependency);
278 * Adds an effective statement to collection of substatements.
280 * @param substatement substatement
281 * @throws IllegalStateException if added in declared phase
282 * @throws NullPointerException if {@code substatement} is null
284 void addEffectiveSubstatement(Mutable<?, ?, ?> substatement);
287 * Adds an effective statement to collection of substatements.
289 * @param statements substatements
290 * @throws IllegalStateException if added in declared phase
291 * @throws NullPointerException if statement parameter is null
293 void addEffectiveSubstatements(Collection<? extends Mutable<?, ?, ?>> statements);
296 * Create a purely-effective substatement. The statement will report a {@code null}
297 * {@link EffectiveStatement#getDeclared()} object. A typical example of statements which require this mechanics
298 * are {@code rpc} and {@code action} statements, which always have {@code input} and {@code output}
299 * substatements, even if those are not declared in YANG text. The returned context is not added to this
300 * context's substatements. That needs to done once the statement is completely defined through
301 * {@link #addEffectiveSubstatement(Mutable)} -- which will trigger
302 * {@link StatementSupport#onFullDefinitionDeclared(Mutable)}.
304 * @param support Statement support of the statement being created
305 * @param arg Effective argument. If specified as {@code null}, statement support will be consulted for the
307 * @return A new statement
308 * @throws IllegalArgumentException if {@code support} does not implement {@link UndeclaredStatementFactory}
309 * @throws IllegalStateException if added in declared phase
310 * @throws NullPointerException if {@code support} is null
313 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
314 @NonNull Mutable<X, Y, Z> createUndeclaredSubstatement(StatementSupport<X, Y, Z> support, @Nullable X arg);
317 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef);
320 * Removes a statement context from the effective substatements based on its statement definition (i.e statement
321 * keyword) and raw (in String form) statement argument. The statement context is removed only if both statement
322 * definition and statement argument match with one of the effective substatements' statement definition
326 * If the statementArg parameter is null, the statement context is removed based only on its statement
329 * @param statementDef statement definition of the statement context to remove
330 * @param statementArg statement argument of the statement context to remove
333 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef, String statementArg);
336 // FIXME: this information should be exposed as a well-known Namespace
337 boolean hasImplicitParentSupport();
340 StmtContext<?, ?, ?> wrapWithImplicit(StmtContext<?, ?, ?> original);
342 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
345 * Set identifier of current root context.
348 * of current root context, must not be null
350 void setRootIdentifier(SourceIdentifier identifier);
352 void setUnsupported();