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 com.google.common.collect.Iterables;
11 import com.google.common.collect.Streams;
12 import java.util.Collection;
14 import java.util.Optional;
15 import java.util.stream.Stream;
16 import javax.annotation.Nonnull;
17 import javax.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.common.YangVersion;
20 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
29 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
32 StatementSource getStatementSource();
35 StatementSourceReference getStatementSourceReference();
38 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.
46 StmtContext<?, ?, ?> getParentContext();
49 * Return the statement argument in literal format.
51 * @return raw statement argument string
54 String rawStatementArgument();
57 A getStatementArgument();
60 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
61 * {@link Optional#empty()} is returned.
63 * @return Optional SchemaPath
65 @Nonnull Optional<SchemaPath> getSchemaPath();
67 boolean isConfiguration();
69 boolean isEnabledSemanticVersioning();
72 * Return a value associated with specified key within a namespace.
74 * @param type Namespace type
76 * @param <K> namespace key type
77 * @param <V> namespace value type
78 * @param <N> namespace type
80 * @return Value, or null if there is no element
81 * @throws NamespaceNotAvailableException when the namespace is not available.
84 <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key) ;
86 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
89 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
92 StmtContext<?, ?, ?> getRoot();
95 * Return declared substatements. These are the statements which are explicitly written in the source model.
97 * @return Collection of declared substatements
100 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
103 * Return effective substatements. These are the statements which are added as this statement's substatements
104 * complete their effective model phase.
106 * @return Collection of declared substatements
109 Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
111 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
112 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
115 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
116 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
120 * Builds {@link DeclaredStatement} for statement context.
125 * Builds {@link EffectiveStatement} for statement context.
129 boolean isSupportedToBuildEffective();
131 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
133 CopyHistory getCopyHistory();
135 boolean isSupportedByFeatures();
137 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
139 ModelProcessingPhase getCompletedPhase();
142 * Return version of root statement context.
144 * @return version of root statement context
146 @Nonnull YangVersion getRootVersion();
148 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
149 extends StmtContext<A, D, E> {
152 Mutable<?, ?, ?> getParentContext();
155 * Associate a value with a key within a namespace.
157 * @param type Namespace type
160 * @param <K> namespace key type
161 * @param <V> namespace value type
162 * @param <N> namespace type
163 * @param <T> key type
164 * @param <U> value type
165 * @throws NamespaceNotAvailableException when the namespace is not available.
167 <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
172 Mutable<?, ?, ?> getRoot();
175 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
176 * child and recording copy type. Resulting object may only be added as a child of this statement.
178 * @param stmt Statement to be used as a template
179 * @param type Type of copy to record in history
180 * @param targetModule Optional new target module
181 * @return copy of statement considering {@link CopyType} (augment, uses)
183 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
184 * from an alien implementation.
185 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
187 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
188 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
191 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
192 * child and recording copy type. Resulting object may only be added as a child of this statement.
194 * @param stmt Statement to be used as a template
195 * @param type Type of copy to record in history
196 * @return copy of statement considering {@link CopyType} (augment, uses)
198 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
199 * from an alien implementation.
200 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
202 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
203 final StmtContext<X, Y, Z> stmt, final CopyType type) {
204 return childCopyOf(stmt, type, null);
208 Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
211 Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
214 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
215 * and will be executed even if its definition remains incomplete. The specified phase cannot complete until
216 * this action is resolved. If the action cannot be resolved, model processing will fail.
218 * @param phase Target phase in which the action will resolved.
219 * @return A new action builder.
220 * @throws NullPointerException if the specified phase is null
222 @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
225 * Adds s statement to namespace map with a key.
228 * {@link StatementNamespace} child that determines namespace to be added to
230 * of type according to namespace class specification
232 * to be added to namespace map
234 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
235 StmtContext<?, ?, ?> stmt);
238 * Set version of root statement context.
241 * of root statement context
243 void setRootVersion(YangVersion version);
246 * Add mutable statement to seal. Each mutable statement must be sealed
247 * as the last step of statement parser processing.
249 * @param mutableStatement
250 * mutable statement which should be sealed
252 void addMutableStmtToSeal(MutableStatement mutableStatement);
255 * Add required module. Based on these dependencies are collected required sources from library sources.
258 * SourceIdentifier of module required by current root
262 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
263 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
264 * users migrated to use proper global namespace.
266 void addRequiredSource(SourceIdentifier dependency);
268 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
270 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
273 * Set identifier of current root context.
276 * of current root context, must not be null
278 void setRootIdentifier(SourceIdentifier identifier);
280 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
282 // FIXME: this seems to be unused, but looks useful.
283 void setCompletedPhase(ModelProcessingPhase completedPhase);