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.IdentifierNamespace;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
27 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
28 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
32 * An inference context associated with an instance of a statement.
34 * @param <A> Argument type
35 * @param <D> Declared Statement representation
36 * @param <E> Effective Statement representation
38 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
39 extends NamespaceStmtCtx, BoundStmtCtxCompat<A, D> {
40 @Deprecated(forRemoval = true)
41 default @NonNull StatementDefinition getPublicDefinition() {
42 return publicDefinition();
45 @Deprecated(forRemoval = true)
46 default @NonNull StatementSource getStatementSource() {
50 @Deprecated(forRemoval = true)
51 default @NonNull StatementSourceReference getStatementSourceReference() {
52 return sourceReference();
56 * Return the statement argument in literal format.
58 * @return raw statement argument string, or null if this statement does not have an argument.
59 * @deprecated Use {@link #rawArgument()} instead.
61 @Deprecated(forRemoval = true)
62 default @Nullable String rawStatementArgument() {
67 * Return the statement argument in literal format.
69 * @return raw statement argument string
70 * @throws VerifyException if this statement does not have an argument
71 * @deprecated Use {@link #getRawArgument()} instead.
73 @Deprecated(forRemoval = true)
74 default @NonNull String coerceRawStatementArgument() {
75 return getRawArgument();
79 * Return the statement argument.
81 * @return statement argument, or null if this statement does not have an argument
82 * @deprecated Use {@link #argument()} instead.
84 @Deprecated(forRemoval = true)
85 default @Nullable A getStatementArgument() {
90 * Return the statement argument in literal format.
92 * @return raw statement argument string
93 * @throws VerifyException if this statement does not have an argument
94 * @deprecated Use {@link #getArgument()} instead.
96 @Deprecated(forRemoval = true)
97 default @NonNull A coerceStatementArgument() {
102 * Return the parent statement context, or null if this is the root statement.
104 * @return context of parent of statement, or null if this is the root statement.
106 @Nullable StmtContext<?, ?, ?> getParentContext();
109 * Return the parent statement context, forcing a VerifyException if this is the root statement.
111 * @return context of parent of statement
112 * @throws VerifyException if this statement is the root statement
114 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
115 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
118 boolean isEnabledSemanticVersioning();
121 * Returns the model root for this statement.
123 * @return root context of statement
125 @NonNull RootStmtContext<?, ?, ?> getRoot();
128 * Return declared substatements. These are the statements which are explicitly written in the source model.
130 * @return Collection of declared substatements
132 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
135 * Return effective substatements. These are the statements which are added as this statement's substatements
136 * complete their effective model phase.
138 * @return Collection of declared substatements
140 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
142 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
143 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
146 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
147 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
151 * Builds {@link EffectiveStatement} for statement context.
155 boolean isSupportedToBuildEffective();
157 boolean isSupportedByFeatures();
159 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
162 * FIXME: YANGTOOLS-784: the next three methods are closely related to the copy process:
163 * - copyHistory() is a brief summary of what went on
164 * - getOriginalContext() points to the CopyHistory.ORIGINAL
165 * - getPreviousCopyCtx() points to the immediate predecessor forming a singly-linked list terminated
166 * at getOriginalContext()
168 * When implementing YANGTOOLS-784, this needs to be taken into account and properly forwarded through
169 * intermediate MutableTrees. Also note this closely relates to current namespace context, as taken into
170 * account when creating the argument. At least parts of this are only needed during buildEffective()
171 * and hence should become arguments to that method.
175 * Return the statement context of the original definition, if this statement is an instantiated copy.
177 * @return Original definition, if this statement was copied.
179 Optional<StmtContext<A, D, E>> getOriginalCtx();
182 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
185 * @return Context of the previous copy of this statement, if this statement has been copied.
187 Optional<StmtContext<A, D, E>> getPreviousCopyCtx();
189 ModelProcessingPhase getCompletedPhase();
192 * An mutable view of an inference context associated with an instance of a statement.
194 * @param <A> Argument type
195 * @param <D> Declared Statement representation
196 * @param <E> Effective Statement representation
198 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
199 extends StmtContext<A, D, E> {
202 Mutable<?, ?, ?> getParentContext();
205 default Mutable<?, ?, ?> coerceParentContext() {
206 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
210 * Associate a value with a key within a namespace.
212 * @param type Namespace type
215 * @param <K> namespace key type
216 * @param <V> namespace value type
217 * @param <N> namespace type
218 * @param <T> key type
219 * @param <U> value type
220 * @throws NamespaceNotAvailableException when the namespace is not available.
222 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<@NonNull N> type,
226 RootStmtContext.Mutable<?, ?, ?> getRoot();
229 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
230 * child and recording copy type. Resulting object may only be added as a child of this statement.
232 * @param stmt Statement to be used as a template
233 * @param type Type of copy to record in history
234 * @param targetModule Optional new target module
235 * @return copy of statement considering {@link CopyType} (augment, uses)
237 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
238 * from an alien implementation.
239 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
241 Mutable<?, ?, ?> childCopyOf(StmtContext<?, ?, ?> stmt, CopyType type, @Nullable QNameModule targetModule);
244 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
245 * child and recording copy type. Resulting object may only be added as a child of this statement.
247 * @param stmt Statement to be used as a template
248 * @param type Type of copy to record in history
249 * @return copy of statement considering {@link CopyType} (augment, uses)
251 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
252 * from an alien implementation.
253 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
255 default Mutable<?, ?, ?> childCopyOf(final StmtContext<?, ?, ?> stmt, final CopyType type) {
256 return childCopyOf(stmt, type, null);
260 * Create a replica of this statement as a substatement of specified {@code parent}. The replica must not be
261 * modified and acts as a source of {@link EffectiveStatement} from outside of {@code parent}'s subtree.
263 * @param parent Parent of the replica statement
264 * @return replica of this statement
265 * @throws IllegalArgumentException if this statement cannot be replicated into parent, for example because it
266 * comes from an alien implementation.
268 @NonNull Mutable<A, D, E> replicaAsChildOf(Mutable<?, ?, ?> parent);
271 @NonNull Optional<? extends Mutable<?, ?, ?>> copyAsChildOf(Mutable<?, ?, ?> parent, CopyType type,
272 @Nullable QNameModule targetModule);
275 default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
276 return mutableDeclaredSubstatements();
279 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
282 default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
283 return mutableEffectiveSubstatements();
286 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
289 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
290 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
291 * this action is resolved. If the action cannot be resolved, model processing will fail.
293 * @param phase Target phase in which the action will resolved.
294 * @return A new action builder.
295 * @throws NullPointerException if the specified phase is null
297 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
300 * Adds s statement to namespace map with a key.
303 * {@link StatementNamespace} child that determines namespace to be added to
305 * of type according to namespace class specification
307 * to be added to namespace map
309 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<@NonNull N> namespace, KT key,
310 StmtContext<?, ?, ?> stmt);
313 * Set version of root statement context.
316 * of root statement context
318 void setRootVersion(YangVersion version);
321 * Add mutable statement to seal. Each mutable statement must be sealed
322 * as the last step of statement parser processing.
324 * @param mutableStatement
325 * mutable statement which should be sealed
327 void addMutableStmtToSeal(MutableStatement mutableStatement);
330 * Add required module. Based on these dependencies are collected required sources from library sources.
333 * SourceIdentifier of module required by current root
337 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
338 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
339 * users migrated to use proper global namespace.
341 void addRequiredSource(SourceIdentifier dependency);
343 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
345 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
348 * Set identifier of current root context.
351 * of current root context, must not be null
353 void setRootIdentifier(SourceIdentifier identifier);
355 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
359 * Search of any child statement context of specified type and return its argument. If such a statement exists, it
360 * is assumed to have the right argument. Users should be careful to use this method for statements which have
361 * cardinality {@code 0..1}, otherwise this method can return any one of the statement's argument.
364 * The default implementation defers to
365 * {@link StmtContextDefaults#findSubstatementArgument(StmtContext, Class)}, subclasses are expected to provide
366 * optimized implementation if possible.
368 * @param <X> Substatement argument type
369 * @param <Z> Substatement effective statement representation
370 * @param type Effective statement representation being look up
371 * @return {@link Optional#empty()} if no statement exists, otherwise the argument value
373 default <X, Z extends EffectiveStatement<X, ?>> @NonNull Optional<X> findSubstatementArgument(
374 final @NonNull Class<Z> type) {
375 return StmtContextDefaults.findSubstatementArgument(this, type);
379 * Check if there is any child statement context of specified type.
382 * The default implementation defers to {@link StmtContextDefaults#hasSubstatement(StmtContext, Class)},
383 * subclasses are expected to provide optimized implementation if possible.
385 * @param type Effective statement representation being look up
386 * @return True if such a child statement exists, false otherwise
388 default boolean hasSubstatement(final @NonNull Class<? extends EffectiveStatement<?, ?>> type) {
389 return StmtContextDefaults.hasSubstatement(this, type);