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.ModuleIdentifier;
21 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
22 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
24 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
30 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
33 StatementSource getStatementSource();
36 StatementSourceReference getStatementSourceReference();
39 StatementDefinition getPublicDefinition();
42 * Return the parent statement context, or null if this is the root statement.
44 * @return context of parent of statement, or null if this is the root statement.
47 StmtContext<?, ?, ?> getParentContext();
50 * Return the statement argument in literal format.
52 * @return raw statement argument string
55 String rawStatementArgument();
58 A getStatementArgument();
61 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
62 * {@link Optional#empty()} is returned.
64 * @return Optional SchemaPath
66 @Nonnull Optional<SchemaPath> getSchemaPath();
68 boolean isConfiguration();
70 boolean isEnabledSemanticVersioning();
73 <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
74 Class<N> type, KT key) throws NamespaceNotAvailableException;
76 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
79 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
82 StmtContext<?, ?, ?> getRoot();
85 * Return declared substatements. These are the statements which are explicitly written in the source model.
87 * @return Collection of declared substatements
90 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
93 * Return effective substatements. These are the statements which are added as this statement's substatements
94 * complete their effective model phase.
96 * @return Collection of declared substatements
99 Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
101 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
102 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
105 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
106 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
110 * Builds {@link DeclaredStatement} for statement context.
115 * Builds {@link EffectiveStatement} for statement context.
119 boolean isSupportedToBuildEffective();
121 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
123 CopyHistory getCopyHistory();
125 boolean isSupportedByFeatures();
127 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
129 ModelProcessingPhase getCompletedPhase();
132 * Return version of root statement context.
134 * @return version of root statement context
136 @Nonnull YangVersion getRootVersion();
138 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
139 extends StmtContext<A, D, E> {
142 Mutable<?, ?, ?> getParentContext();
144 <K, V, KT extends K, VT extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, KT key,
145 VT value) throws NamespaceNotAvailableException;
149 Mutable<?, ?, ?> getRoot();
152 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
153 * child and recording copy type. Resulting object may only be added as a child of this statement.
155 * @param stmt Statement to be used as a template
156 * @param type Type of copy to record in history
157 * @param targetModule Optional new target module
158 * @return copy of statement considering {@link CopyType} (augment, uses)
160 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
161 * from an alien implementation.
162 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
164 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
165 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
168 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
169 * child and recording copy type. Resulting object may only be added as a child of this statement.
171 * @param stmt Statement to be used as a template
172 * @param type Type of copy to record in history
173 * @return copy of statement considering {@link CopyType} (augment, uses)
175 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
176 * from an alien implementation.
177 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
179 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
180 final StmtContext<X, Y, Z> stmt, final CopyType type) {
181 return childCopyOf(stmt, type, null);
185 Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
188 Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
191 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
192 * and will be executed even if its definition remains incomplete.
194 * @param phase Target phase in which the action will resolved.
195 * @return A new action builder.
196 * @throws NullPointerException if the specified phase is null
198 @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
201 * Adds s statement to namespace map with a key.
204 * {@link StatementNamespace} child that determines namespace to be added to
206 * of type according to namespace class specification
208 * to be added to namespace map
210 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
211 StmtContext<?, ?, ?> stmt);
214 * Set version of root statement context.
217 * of root statement context
219 void setRootVersion(YangVersion version);
222 * Add mutable statement to seal. Each mutable statement must be sealed
223 * as the last step of statement parser processing.
225 * @param mutableStatement
226 * mutable statement which should be sealed
228 void addMutableStmtToSeal(MutableStatement mutableStatement);
231 * Add required module. Based on these dependencies are collected required sources from library sources.
234 * SourceIdentifier of module required by current root
238 * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
239 * getter -- which makes it rather strange. At some point this method needs to be deprecated and its
240 * users migrated to use proper global namespace.
242 void addRequiredSource(SourceIdentifier dependency);
244 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
246 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
249 * Set identifier of current root context.
252 * of current root context
254 void setRootIdentifier(ModuleIdentifier identifier);
256 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
258 // FIXME: this seems to be unused, but looks useful.
259 void setCompletedPhase(ModelProcessingPhase completedPhase);