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.
127 * @return Collection of declared substatements
129 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
132 * Return effective substatements. These are the statements which are added as this statement's substatements
133 * complete their effective model phase.
135 * @return Collection of declared substatements
137 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
139 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
140 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
143 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
144 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
148 * Return the {@link EffectiveStatement} for statement context, creating it if required. Implementations of this
149 * method are required to memoize the returned object, so that subsequent invocation return the same object.
152 * If {@link #isSupportedToBuildEffective()} returns {@code false}, this method's behaviour is undefined.
154 * @return Effective statement instance.
156 @NonNull E buildEffective();
158 boolean isSupportedToBuildEffective();
160 boolean isSupportedByFeatures();
162 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
165 * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
166 * - copyHistory() is a brief summary of what went on
167 * - getOriginalContext() points to the CopyHistory.ORIGINAL
168 * - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
169 * at getOriginalContext()
171 * When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
172 * intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
173 * account when creating the argument. At least parts of this are only needed during buildEffective()
174 * and hence should become arguments to that method.
178 * Return the statement context of the original definition, if this statement is an instantiated copy.
180 * @return Original definition, if this statement was copied.
182 Optional<StmtContext<A, D, E>> getOriginalCtx();
185 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
188 * @return Context of the previous copy of this statement, if this statement has been copied.
190 Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
193 * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
194 * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
196 * @param parent Parent of the replica statement
197 * @return replica of this statement
198 * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
199 * comes from an alien implementation.
201 @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
204 @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
205 @Nullable QNameModule targetModule);
207 ModelProcessingPhase getCompletedPhase();
210 * An mutable view of an inference context associated with an instance of a statement.
212 * @param <A> Argument type
213 * @param <D> Declared Statement representation
214 * @param <E> Effective Statement representation
216 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
217 extends StmtContext<A, D, E> {
220 Mutable<?, ?, ?> getParentContext();
223 default Mutable<?, ?, ?> coerceParentContext() {
224 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
228 * Associate a value with a key within a namespace.
230 * @param type Namespace type
233 * @param <K> namespace key type
234 * @param <V> namespace value type
235 * @param <N> namespace type
236 * @param <T> key type
237 * @param <U> value type
238 * @throws NamespaceNotAvailableException when the namespace is not available.
240 <K, V, T extends K, U extends V, N extends ParserNamespace<K, V>> void addToNs(Class<@NonNull N> type,
244 RootStmtContext.Mutable<?, ?, ?> getRoot();
247 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
248 * child and recording copy type. Resulting object may only be added as a child of this statement.
250 * @param stmt Statement to be used as a template
251 * @param type Type of copy to record in history
252 * @param targetModule Optional new target module
253 * @return copy of statement considering {@link CopyType} (augment, uses)
255 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
256 * from an alien implementation.
257 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
259 Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
262 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
263 * child and recording copy type. Resulting object may only be added as a child of this statement.
265 * @param stmt Statement to be used as a template
266 * @param type Type of copy to record in history
267 * @return copy of statement considering {@link CopyType} (augment, uses)
269 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
270 * from an alien implementation.
271 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
273 default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
274 return childCopyOf(stmt, type, null);
278 default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
279 return mutableDeclaredSubstatements();
282 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
285 default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
286 return mutableEffectiveSubstatements();
289 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
292 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
293 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
294 * this action is resolved. If the action cannot be resolved, model processing will fail.
296 * @param phase Target phase in which the action will resolved.
297 * @return A new action builder.
298 * @throws NullPointerException if the specified phase is null
300 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
303 * Adds s statement to namespace map with a key.
306 * {@link StatementNamespace} child that determines namespace to be added to
308 * of type according to namespace class specification
310 * to be added to namespace map
312 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
313 StmtContext<?, ?, ?> stmt);
316 * Set version of root statement context.
319 * of root statement context
321 void setRootVersion(YangVersion version);
324 * Add required module. Based on these dependencies are collected required sources from library sources.
327 * SourceIdentifier of module required by current root
331 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
332 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
333 * users migrated to use proper global namespace.
335 void addRequiredSource(SourceIdentifier dependency);
337 // YANG example: RPC/action statements always have 'input' and 'output' defined
339 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>>
340 @NonNull Mutable<X, Y, Z> addEffectiveSubstatement(StatementSupport<X, Y, Z> support, @Nullable X arg);
343 * Adds an effective statement to collection of substatements.
345 * @param substatement substatement
346 * @throws IllegalStateException if added in declared phase
347 * @throws NullPointerException if statement parameter is null
349 void addEffectiveSubstatement(Mutable<?, ?, ?> substatement);
352 * Adds an effective statement to collection of substatements.
354 * @param statements substatements
355 * @throws IllegalStateException if added in declared phase
356 * @throws NullPointerException if statement parameter is null
358 void addEffectiveSubstatements(Collection<? extends Mutable<?, ?, ?>> statements);
361 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef);
364 * Removes a statement context from the effective substatements based on its statement definition (i.e statement
365 * keyword) and raw (in String form) statement argument. The statement context is removed only if both statement
366 * definition and statement argument match with one of the effective substatements' statement definition
370 * If the statementArg parameter is null, the statement context is removed based only on its statement
373 * @param statementDef statement definition of the statement context to remove
374 * @param statementArg statement argument of the statement context to remove
377 void removeStatementFromEffectiveSubstatements(StatementDefinition statementDef, String statementArg);
380 // FIXME: this information should be exposed as a well-known Namespace
381 boolean hasImplicitParentSupport();
384 StmtContext<?, ?, ?> wrapWithImplicit(StmtContext<?, ?, ?> original);
386 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
389 * Set identifier of current root context.
392 * of current root context, must not be null
394 void setRootIdentifier(SourceIdentifier identifier);
396 void setUnsupported();