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;
32 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
34 * Returns the origin of the statement.
36 * @return origin of statement
38 @NonNull StatementSource getStatementSource();
41 * Returns a reference to statement source.
43 * @return reference of statement source
45 @NonNull StatementSourceReference getStatementSourceReference();
48 * See {@link StatementSupport#getPublicView()}.
50 @NonNull StatementDefinition getPublicDefinition();
53 * Return the parent statement context, or null if this is the root statement.
55 * @return context of parent of statement, or null if this is the root statement.
57 @Nullable StmtContext<?, ?, ?> getParentContext();
60 * Return the parent statement context, forcing a VerifyException if this is the root statement.
62 * @return context of parent of statement
63 * @throws VerifyException if this statement is the root statement
65 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
66 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
70 * Return the statement argument in literal format.
72 * @return raw statement argument string, or null if this statement does not have an argument.
74 @Nullable String rawStatementArgument();
77 * Return the statement argument in literal format.
79 * @return raw statement argument string
80 * @throws VerifyException if this statement does not have an argument
82 default @NonNull String coerceRawStatementArgument() {
83 return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
87 * Return the statement argument.
89 * @return statement argument, or null if this statement does not have an argument
91 @Nullable A getStatementArgument();
94 * Return the statement argument in literal format.
96 * @return raw statement argument string
97 * @throws VerifyException if this statement does not have an argument
99 default @NonNull A coerceStatementArgument() {
100 return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
104 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
105 * {@link Optional#empty()} is returned.
107 * @return Optional SchemaPath
109 @NonNull Optional<SchemaPath> getSchemaPath();
111 boolean isConfiguration();
113 boolean isEnabledSemanticVersioning();
116 * Return a value associated with specified key within a namespace.
118 * @param type Namespace type
120 * @param <K> namespace key type
121 * @param <V> namespace value type
122 * @param <N> namespace type
123 * @param <T> key type
124 * @return Value, or null if there is no element
125 * @throws NamespaceNotAvailableException when the namespace is not available.
127 @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key);
129 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
131 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
133 @NonNull StmtContext<?, ?, ?> getRoot();
136 * Return declared substatements. These are the statements which are explicitly written in the source model.
138 * @return Collection of declared substatements
140 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
143 * Return effective substatements. These are the statements which are added as this statement's substatements
144 * complete their effective model phase.
146 * @return Collection of declared substatements
148 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
150 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
151 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
154 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
155 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
159 * Builds {@link DeclaredStatement} for statement context.
164 * Builds {@link EffectiveStatement} for statement context.
168 boolean isSupportedToBuildEffective();
170 boolean isSupportedByFeatures();
172 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
175 * Return the executive summary of the copy process that has produced this context.
177 * @return A simplified summary of the copy process.
179 CopyHistory getCopyHistory();
182 * Return the statement context of the original definition, if this statement is an instantiated copy.
184 * @return Original definition, if this statement was copied.
186 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
189 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
192 * @return Context of the previous copy of this statement, if this statement has been copied.
194 Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
196 ModelProcessingPhase getCompletedPhase();
199 * Return version of root statement context.
201 * @return version of root statement context
203 @NonNull YangVersion getRootVersion();
205 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
206 extends StmtContext<A, D, E> {
209 Mutable<?, ?, ?> getParentContext();
212 default Mutable<?, ?, ?> coerceParentContext() {
213 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
217 * Associate a value with a key within a namespace.
219 * @param type Namespace type
222 * @param <K> namespace key type
223 * @param <V> namespace value type
224 * @param <N> namespace type
225 * @param <T> key type
226 * @param <U> value type
227 * @throws NamespaceNotAvailableException when the namespace is not available.
229 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
233 Mutable<?, ?, ?> getRoot();
236 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
237 * child and recording copy type. Resulting object may only be added as a child of this statement.
239 * @param stmt Statement to be used as a template
240 * @param type Type of copy to record in history
241 * @param targetModule Optional new target module
242 * @return copy of statement considering {@link CopyType} (augment, uses)
244 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
245 * from an alien implementation.
246 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
248 // FIXME: 4.0.0: remove generic arguments X, Y, Z. Callers should not care, as the returned copy can actually
249 // be an encapsulating implicit statement.
250 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
251 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
254 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
255 * child and recording copy type. Resulting object may only be added as a child of this statement.
257 * @param stmt Statement to be used as a template
258 * @param type Type of copy to record in history
259 * @return copy of statement considering {@link CopyType} (augment, uses)
261 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
262 * from an alien implementation.
263 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
265 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
266 final StmtContext<X, Y, Z> stmt, final CopyType type) {
267 return childCopyOf(stmt, type, null);
270 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
272 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
275 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
276 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
277 * this action is resolved. If the action cannot be resolved, model processing will fail.
279 * @param phase Target phase in which the action will resolved.
280 * @return A new action builder.
281 * @throws NullPointerException if the specified phase is null
283 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
286 * Adds s statement to namespace map with a key.
289 * {@link StatementNamespace} child that determines namespace to be added to
291 * of type according to namespace class specification
293 * to be added to namespace map
295 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
296 StmtContext<?, ?, ?> stmt);
299 * Set version of root statement context.
302 * of root statement context
304 void setRootVersion(YangVersion version);
307 * Add mutable statement to seal. Each mutable statement must be sealed
308 * as the last step of statement parser processing.
310 * @param mutableStatement
311 * mutable statement which should be sealed
313 void addMutableStmtToSeal(MutableStatement mutableStatement);
316 * Add required module. Based on these dependencies are collected required sources from library sources.
319 * SourceIdentifier of module required by current root
323 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
324 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
325 * users migrated to use proper global namespace.
327 void addRequiredSource(SourceIdentifier dependency);
329 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
331 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
334 * Set identifier of current root context.
337 * of current root context, must not be null
339 void setRootIdentifier(SourceIdentifier identifier);
341 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
343 // FIXME: this seems to be unused, but looks useful.
344 void setCompletedPhase(ModelProcessingPhase completedPhase);