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.base.VerifyException;
13 import com.google.common.collect.Iterables;
14 import com.google.common.collect.Streams;
15 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.SchemaPath;
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>> {
41 * Returns the origin of the statement.
43 * @return origin of statement
45 @NonNull StatementSource getStatementSource();
48 * Returns a reference to statement source.
50 * @return reference of statement source
52 @NonNull StatementSourceReference getStatementSourceReference();
55 * See {@link StatementSupport#getPublicView()}.
57 @NonNull StatementDefinition getPublicDefinition();
60 * Return the parent statement context, or null if this is the root statement.
62 * @return context of parent of statement, or null if this is the root statement.
64 @Nullable StmtContext<?, ?, ?> getParentContext();
67 * Return the parent statement context, forcing a VerifyException if this is the root statement.
69 * @return context of parent of statement
70 * @throws VerifyException if this statement is the root statement
72 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
73 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
77 * Return the statement argument in literal format.
79 * @return raw statement argument string, or null if this statement does not have an argument.
81 @Nullable String rawStatementArgument();
84 * Return the statement argument in literal format.
86 * @return raw statement argument string
87 * @throws VerifyException if this statement does not have an argument
89 default @NonNull String coerceRawStatementArgument() {
90 return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
94 * Return the statement argument.
96 * @return statement argument, or null if this statement does not have an argument
98 @Nullable A getStatementArgument();
101 * Return the statement argument in literal format.
103 * @return raw statement argument string
104 * @throws VerifyException if this statement does not have an argument
106 default @NonNull A coerceStatementArgument() {
107 return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
111 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
112 * {@link Optional#empty()} is returned.
114 * @return Optional SchemaPath
116 @NonNull Optional<SchemaPath> getSchemaPath();
118 boolean isConfiguration();
120 boolean isEnabledSemanticVersioning();
123 * Return a value associated with specified key within a namespace.
125 * @param type Namespace type
127 * @param <K> namespace key type
128 * @param <V> namespace value type
129 * @param <N> namespace type
130 * @param <T> key type
131 * @return Value, or null if there is no element
132 * @throws NamespaceNotAvailableException when the namespace is not available.
134 @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key);
136 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
138 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
140 @NonNull StmtContext<?, ?, ?> getRoot();
143 * Return declared substatements. These are the statements which are explicitly written in the source model.
145 * @return Collection of declared substatements
147 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
150 * Return effective substatements. These are the statements which are added as this statement's substatements
151 * complete their effective model phase.
153 * @return Collection of declared substatements
155 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
157 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
158 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
161 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
162 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
166 * Builds {@link DeclaredStatement} for statement context.
171 * Builds {@link EffectiveStatement} for statement context.
175 boolean isSupportedToBuildEffective();
177 boolean isSupportedByFeatures();
179 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
182 * Return the executive summary of the copy process that has produced this context.
184 * @return A simplified summary of the copy process.
186 CopyHistory getCopyHistory();
189 * Return the statement context of the original definition, if this statement is an instantiated copy.
191 * @return Original definition, if this statement was copied.
193 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
196 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
199 * @return Context of the previous copy of this statement, if this statement has been copied.
201 Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
203 ModelProcessingPhase getCompletedPhase();
206 * Return version of root statement context.
208 * @return version of root statement context
210 @NonNull YangVersion getRootVersion();
213 * An mutable view of an inference context associated with an instance of a statement.
215 * @param <A> Argument type
216 * @param <D> Declared Statement representation
217 * @param <E> Effective Statement representation
219 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
220 extends StmtContext<A, D, E> {
223 Mutable<?, ?, ?> getParentContext();
226 default Mutable<?, ?, ?> coerceParentContext() {
227 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
231 * Associate a value with a key within a namespace.
233 * @param type Namespace type
236 * @param <K> namespace key type
237 * @param <V> namespace value type
238 * @param <N> namespace type
239 * @param <T> key type
240 * @param <U> value type
241 * @throws NamespaceNotAvailableException when the namespace is not available.
243 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
247 Mutable<?, ?, ?> getRoot();
250 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
251 * child and recording copy type. Resulting object may only be added as a child of this statement.
253 * @param stmt Statement to be used as a template
254 * @param type Type of copy to record in history
255 * @param targetModule Optional new target module
256 * @return copy of statement considering {@link CopyType} (augment, uses)
258 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
259 * from an alien implementation.
260 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
262 // FIXME: 4.0.0: remove generic arguments X, Y, Z. Callers should not care, as the returned copy can actually
263 // be an encapsulating implicit statement.
264 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
265 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
268 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
269 * child and recording copy type. Resulting object may only be added as a child of this statement.
271 * @param stmt Statement to be used as a template
272 * @param type Type of copy to record in history
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 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
280 final StmtContext<X, Y, Z> stmt, final CopyType type) {
281 return childCopyOf(stmt, type, null);
285 default Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements() {
286 return mutableDeclaredSubstatements();
289 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
292 default Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements() {
293 return mutableEffectiveSubstatements();
296 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
299 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
300 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
301 * this action is resolved. If the action cannot be resolved, model processing will fail.
303 * @param phase Target phase in which the action will resolved.
304 * @return A new action builder.
305 * @throws NullPointerException if the specified phase is null
307 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
310 * Adds s statement to namespace map with a key.
313 * {@link StatementNamespace} child that determines namespace to be added to
315 * of type according to namespace class specification
317 * to be added to namespace map
319 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
320 StmtContext<?, ?, ?> stmt);
323 * Set version of root statement context.
326 * of root statement context
328 void setRootVersion(YangVersion version);
331 * Add mutable statement to seal. Each mutable statement must be sealed
332 * as the last step of statement parser processing.
334 * @param mutableStatement
335 * mutable statement which should be sealed
337 void addMutableStmtToSeal(MutableStatement mutableStatement);
340 * Add required module. Based on these dependencies are collected required sources from library sources.
343 * SourceIdentifier of module required by current root
347 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
348 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
349 * users migrated to use proper global namespace.
351 void addRequiredSource(SourceIdentifier dependency);
353 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
355 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
358 * Set identifier of current root context.
361 * of current root context, must not be null
363 void setRootIdentifier(SourceIdentifier identifier);
365 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
367 // FIXME: this seems to be unused, but looks useful.
368 void setCompletedPhase(ModelProcessingPhase completedPhase);