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 @NonNull StatementSource getStatementSource();
36 @NonNull StatementSourceReference getStatementSourceReference();
38 @NonNull StatementDefinition getPublicDefinition();
41 * Return the parent statement context, or null if this is the root statement.
43 * @return context of parent of statement, or null if this is the root statement.
45 @Nullable StmtContext<?, ?, ?> getParentContext();
48 * Return the parent statement context, forcing a VerifyException if this is the root statement.
50 * @return context of parent of statement
51 * @throws VerifyException if this statement is the root statement
53 default @NonNull StmtContext<?, ?, ?> coerceParentContext() {
54 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
58 * Return the statement argument in literal format.
60 * @return raw statement argument string, or null if this statement does not have an argument.
62 @Nullable String rawStatementArgument();
65 * Return the statement argument in literal format.
67 * @return raw statement argument string
68 * @throws VerifyException if this statement does not have an argument
70 default @NonNull String coerceRawStatementArgument() {
71 return verifyNotNull(rawStatementArgument(), "Statement context %s does not have an argument", this);
75 * Return the statement argument.
77 * @return statement argument, or null if this statement does not have an argument
79 @Nullable A getStatementArgument();
82 * Return the statement argument in literal format.
84 * @return raw statement argument string
85 * @throws VerifyException if this statement does not have an argument
87 default @NonNull A coerceStatementArgument() {
88 return verifyNotNull(getStatementArgument(), "Statement context %s does not have an argument", this);
92 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
93 * {@link Optional#empty()} is returned.
95 * @return Optional SchemaPath
97 @NonNull Optional<SchemaPath> getSchemaPath();
99 boolean isConfiguration();
101 boolean isEnabledSemanticVersioning();
104 * Return a value associated with specified key within a namespace.
106 * @param type Namespace type
108 * @param <K> namespace key type
109 * @param <V> namespace value type
110 * @param <N> namespace type
111 * @param <T> key type
112 * @return Value, or null if there is no element
113 * @throws NamespaceNotAvailableException when the namespace is not available.
115 @NonNull <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key);
117 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(Class<N> type);
119 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
121 @NonNull StmtContext<?, ?, ?> getRoot();
124 * Return declared substatements. These are the statements which are explicitly written in the source model.
126 * @return Collection of declared substatements
128 @NonNull Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
131 * Return effective substatements. These are the statements which are added as this statement's substatements
132 * complete their effective model phase.
134 * @return Collection of declared substatements
136 @NonNull Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
138 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
139 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
142 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
143 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
147 * Builds {@link DeclaredStatement} for statement context.
152 * Builds {@link EffectiveStatement} for statement context.
156 boolean isSupportedToBuildEffective();
158 boolean isSupportedByFeatures();
160 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
163 * Return the executive summary of the copy process that has produced this context.
165 * @return A simplified summary of the copy process.
167 CopyHistory getCopyHistory();
170 * Return the statement context of the original definition, if this statement is an instantiated copy.
172 * @return Original definition, if this statement was copied.
174 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
177 * Return the context of the previous copy of this statement -- effectively walking towards the source origin
180 * @return Context of the previous copy of this statement, if this statement has been copied.
182 Optional<? extends StmtContext<?, ?, ?>> getPreviousCopyCtx();
184 ModelProcessingPhase getCompletedPhase();
187 * Return version of root statement context.
189 * @return version of root statement context
191 @NonNull YangVersion getRootVersion();
193 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
194 extends StmtContext<A, D, E> {
197 Mutable<?, ?, ?> getParentContext();
200 default Mutable<?, ?, ?> coerceParentContext() {
201 return verifyNotNull(getParentContext(), "Root context %s does not have a parent", this);
205 * Associate a value with a key within a namespace.
207 * @param type Namespace type
210 * @param <K> namespace key type
211 * @param <V> namespace value type
212 * @param <N> namespace type
213 * @param <T> key type
214 * @param <U> value type
215 * @throws NamespaceNotAvailableException when the namespace is not available.
217 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
221 Mutable<?, ?, ?> getRoot();
224 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
225 * child and recording copy type. Resulting object may only be added as a child of this statement.
227 * @param stmt Statement to be used as a template
228 * @param type Type of copy to record in history
229 * @param targetModule Optional new target module
230 * @return copy of statement considering {@link CopyType} (augment, uses)
232 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
233 * from an alien implementation.
234 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
236 // FIXME: 5.0.0: remove generic arguments X, Y, Z. Callers should not care, as the returned copy can actually
237 // be an encapsulating implicit statement.
238 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
239 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
242 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
243 * child and recording copy type. Resulting object may only be added as a child of this statement.
245 * @param stmt Statement to be used as a template
246 * @param type Type of copy to record in history
247 * @return copy of statement considering {@link CopyType} (augment, uses)
249 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
250 * from an alien implementation.
251 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
253 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
254 final StmtContext<X, Y, Z> stmt, final CopyType type) {
255 return childCopyOf(stmt, type, null);
258 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
260 @NonNull Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
263 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
264 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
265 * this action is resolved. If the action cannot be resolved, model processing will fail.
267 * @param phase Target phase in which the action will resolved.
268 * @return A new action builder.
269 * @throws NullPointerException if the specified phase is null
271 @NonNull ModelActionBuilder newInferenceAction(@NonNull ModelProcessingPhase phase);
274 * Adds s statement to namespace map with a key.
277 * {@link StatementNamespace} child that determines namespace to be added to
279 * of type according to namespace class specification
281 * to be added to namespace map
283 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
284 StmtContext<?, ?, ?> stmt);
287 * Set version of root statement context.
290 * of root statement context
292 void setRootVersion(YangVersion version);
295 * Add mutable statement to seal. Each mutable statement must be sealed
296 * as the last step of statement parser processing.
298 * @param mutableStatement
299 * mutable statement which should be sealed
301 void addMutableStmtToSeal(MutableStatement mutableStatement);
304 * Add required module. Based on these dependencies are collected required sources from library sources.
307 * SourceIdentifier of module required by current root
311 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
312 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
313 * users migrated to use proper global namespace.
315 void addRequiredSource(SourceIdentifier dependency);
317 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
319 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
322 * Set identifier of current root context.
325 * of current root context, must not be null
327 void setRootIdentifier(SourceIdentifier identifier);
329 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
331 // FIXME: this seems to be unused, but looks useful.
332 void setCompletedPhase(ModelProcessingPhase completedPhase);