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.meta.StatementOrigin;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
31 * An inference context associated with an instance of a statement.
33 * @param <A> Argument type
34 * @param <D> Declared Statement representation
35 * @param <E> Effective Statement representation
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();
44 @Deprecated(forRemoval = true)
45 default @NonNull StatementOrigin getStatementSource() {
49 @Deprecated(forRemoval = true)
50 default @NonNull StatementSourceReference getStatementSourceReference() {
51 return sourceReference();
55 * Return the statement argument in literal format.
57 * @return raw statement argument string, or null if this statement does not have an argument.
58 * @deprecated Use {@link #rawArgument()} instead.
60 @Deprecated(forRemoval = true)
61 default @Nullable String rawStatementArgument() {
66 * Return the statement argument in literal format.
68 * @return raw statement argument string
69 * @throws VerifyException if this statement does not have an argument
70 * @deprecated Use {@link #getRawArgument()} instead.
72 @Deprecated(forRemoval = true)
73 default @NonNull String coerceRawStatementArgument() {
74 return getRawArgument();
78 * Return the statement argument.
80 * @return statement argument, or null if this statement does not have an argument
81 * @deprecated Use {@link #argument()} instead.
83 @Deprecated(forRemoval = true)
84 default @Nullable A getStatementArgument() {
89 * Return the statement argument in literal format.
91 * @return raw statement argument string
92 * @throws VerifyException if this statement does not have an argument
93 * @deprecated Use {@link #getArgument()} instead.
95 @Deprecated(forRemoval = true)
96 default @NonNull A coerceStatementArgument() {
101 * Return the parent statement context, or null if this is the root statement.
103 * @return context of parent of statement, or null if this is the root statement.
105 @Nullable StmtContext<?, ?, ?> getParentContext();
108 * Return the parent statement context, forcing a VerifyException if this is the root statement.
110 * @return context of parent of statement
111 * @throws VerifyException if this statement is the root statement
113 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
114 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
118 * Returns the model root for this statement.
120 * @return root context of statement
122 @NonNull RootStmtContext<?, ?, ?> getRoot();
125 * Return declared substatements. These are the statements which are explicitly written in the source model, but
126 * reflect implicit containment statements as well. To but that statement into practical terms, this snippet:
134 * reports the same structure as the canonical verbose equivalent:
144 * Returned collection is therefore well suited for reasoning about the schema tree. It is not appropriate for
145 * populating populating {@link DeclaredStatement#declaredSubstatements()}.
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 * Return the {@link EffectiveStatement} for statement context, creating it if required. Implementations of this
169 * method are required to memoize the returned object, so that subsequent invocation return the same object.
172 * If {@link #isSupportedToBuildEffective()} returns {@code false}, this method's behaviour is undefined.
174 * @return Effective statement instance.
176 @NonNull E buildEffective();
178 boolean isSupportedToBuildEffective();
180 boolean isSupportedByFeatures();
182 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
185 * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
186 * - copyHistory() is a brief summary of what went on
187 * - getOriginalContext() points to the CopyHistory.ORIGINAL
188 * - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
189 * at getOriginalContext()
191 * When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
192 * intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
193 * account when creating the argument. At least parts of this are only needed during buildEffective()
194 * and hence should become arguments to that method.
198 * Return the statement context of the original definition, if this statement is an instantiated copy.
200 * @return Original definition, if this statement was copied.
202 Optional<StmtContext<A, D, E>> getOriginalCtx();
205 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
208 * @return Context of the previous copy of this statement, if this statement has been copied.
210 Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
213 * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
214 * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
216 * @param parent Parent of the replica statement
217 * @return replica of this statement
218 * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
219 * comes from an alien implementation.
221 @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
224 @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
225 @Nullable QNameModule targetModule);
227 ModelProcessingPhase getCompletedPhase();
230 * An mutable view of an inference context associated with an instance of a statement.
232 * @param <A> Argument type
233 * @param <D> Declared Statement representation
234 * @param <E> Effective Statement representation
236 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
237 extends StmtContext<A, D, E> {
240 Mutable<?, ?, ?> getParentContext();
243 default Mutable<?, ?, ?> coerceParentContext() {
244 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
248 * Associate a value with a key within a namespace.
250 * @param type Namespace type
253 * @param <K> namespace key type
254 * @param <V> namespace value type
255 * @param <N> namespace type
256 * @param <T> key type
257 * @param <U> value type
258 * @throws NamespaceNotAvailableException when the namespace is not available.
260 <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(Class<@NonNull N> type,
264 RootStmtContext.Mutable<?, ?, ?> getRoot();
267 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
268 * child and recording copy type. Resulting object may only be added as a child of this statement.
270 * @param stmt Statement to be used as a template
271 * @param type Type of copy to record in history
272 * @param targetModule Optional new target module
273 * @return copy of statement considering {@link CopyType} (augment, uses)
275 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
276 * from an alien implementation.
277 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
279 Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
282 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
283 * child and recording copy type. Resulting object may only be added as a child of this statement.
285 * @param stmt Statement to be used as a template
286 * @param type Type of copy to record in history
287 * @return copy of statement considering {@link CopyType} (augment, uses)
289 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
290 * from an alien implementation.
291 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
293 default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
294 return childCopyOf(stmt, type, null);
298 default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
299 return mutableDeclaredSubstatements();
302 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
305 default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
306 return mutableEffectiveSubstatements();
309 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
312 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
313 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
314 * this action is resolved. If the action cannot be resolved, model processing will fail.
316 * @param phase Target phase in which the action will resolved.
317 * @return A new action builder.
318 * @throws NullPointerException if the specified phase is null
320 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
323 * Adds s statement to namespace map with a key.
326 * {@link StatementNamespace} child that determines namespace to be added to
328 * of type according to namespace class specification
330 * to be added to namespace map
332 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
333 StmtContext<?, ?, ?> stmt);
336 * Set version of root statement context.
339 * of root statement context
341 void setRootVersion(YangVersion version);
344 * Add required module. Based on these dependencies are collected required sources from library sources.
347 * SourceIdentifier of module required by current root
351 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
352 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
353 * users migrated to use proper global namespace.
355 void addRequiredSource(SourceIdentifier dependency);
357 // YANG example: RPC/action statements always have 'input' and 'output' defined
359 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
360 @NonNull Mutable<X, Y, Z> addEffectiveSubstatement(StatementSupport<X, Y, Z> support, @Nullable X arg);
363 * Adds an effective statement to collection of substatements.
365 * @param substatement substatement
366 * @throws IllegalStateException if added in declared phase
367 * @throws NullPointerException if statement parameter is null
369 void addEffectiveSubstatement(Mutable<?, ?, ?> substatement);
372 * Adds an effective statement to collection of substatements.
374 * @param statements substatements
375 * @throws IllegalStateException if added in declared phase
376 * @throws NullPointerException if statement parameter is null
378 void addEffectiveSubstatements(Collection<? extends Mutable<?, ?, ?>> statements);
381 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef);
384 * Removes a statement context from the effective substatements based on its statement definition (i.e statement
385 * keyword) and raw (in String form) statement argument. The statement context is removed only if both statement
386 * definition and statement argument match with one of the effective substatements' statement definition
390 * If the statementArg parameter is null, the statement context is removed based only on its statement
393 * @param statementDef statement definition of the statement context to remove
394 * @param statementArg statement argument of the statement context to remove
397 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef, String statementArg);
400 // FIXME: this information should be exposed as a well-known Namespace
401 boolean hasImplicitParentSupport();
404 StmtContext<?, ?, ?> wrapWithImplicit(StmtContext<?, ?, ?> original);
406 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
409 * Set identifier of current root context.
412 * of current root context, must not be null
414 void setRootIdentifier(SourceIdentifier identifier);
416 void setUnsupported();