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.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 raw statement argument string
52 String rawStatementArgument();
55 A getStatementArgument();
58 * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
59 * {@link Optional#empty()} is returned.
61 * @return Optional SchemaPath
63 @Nonnull Optional<SchemaPath> getSchemaPath();
65 boolean isConfiguration();
67 boolean isEnabledSemanticVersioning();
70 <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
71 Class<N> type, KT key) throws NamespaceNotAvailableException;
73 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
76 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
79 StmtContext<?, ?, ?> getRoot();
82 * Return declared substatements. These are the statements which are explicitly written in the source model.
84 * @return Collection of declared substatements
87 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
90 * Return effective substatements. These are the statements which are added as this statement's substatements
91 * complete their effective model phase.
93 * @return Collection of declared substatements
96 Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
98 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
99 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
102 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
103 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
107 * Builds {@link DeclaredStatement} for statement context.
112 * Builds {@link EffectiveStatement} for statement context
116 boolean isSupportedToBuildEffective();
118 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
120 CopyHistory getCopyHistory();
122 boolean isSupportedByFeatures();
124 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
126 ModelProcessingPhase getCompletedPhase();
129 * Return version of root statement context.
131 * @return version of root statement context
133 @Nonnull YangVersion getRootVersion();
135 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
136 extends StmtContext<A, D, E> {
139 Mutable<?, ?, ?> getParentContext();
141 <K, V, KT extends K, VT extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, KT key,
142 VT value) throws NamespaceNotAvailableException;
146 Mutable<?, ?, ?> getRoot();
149 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
150 * child and recording copy type. Resulting object may only be added as a child of this statement.
152 * @param stmt Statement to be used as a template
153 * @param type Type of copy to record in history
154 * @param targetModule Optional new target module
155 * @return copy of statement considering {@link CopyType} (augment, uses)
157 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
158 * from an alien implementation.
159 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
161 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
162 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
165 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
166 * child and recording copy type. Resulting object may only be added as a child of this statement.
168 * @param stmt Statement to be used as a template
169 * @param type Type of copy to record in history
170 * @return copy of statement considering {@link CopyType} (augment, uses)
172 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
173 * from an alien implementation.
174 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
176 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
177 final StmtContext<X, Y, Z> stmt, final CopyType type) {
178 return childCopyOf(stmt, type, null);
182 Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
185 Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
188 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
189 * and will be executed even if its definition remains incomplete.
191 * @param phase Target phase in which the action will resolved.
192 * @return A new action builder.
193 * @throws NullPointerException if the specified phase is null
195 @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
198 * adds statement to namespace map with the key
201 * {@link StatementNamespace} child that determines namespace to be added to
203 * of type according to namespace class specification
205 * to be added to namespace map
207 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
208 StmtContext<?, ?, ?> stmt);
211 * Set version of root statement context.
214 * of root statement context
216 void setRootVersion(YangVersion version);
219 * Add mutable statement to seal. Each mutable statement must be sealed
220 * as the last step of statement parser processing.
222 * @param mutableStatement
223 * mutable statement which should be sealed
225 void addMutableStmtToSeal(MutableStatement mutableStatement);
228 * Add required module. Based on these dependencies are collected
229 * required sources from library sources.
232 * ModuleIdentifier of module required by current root
235 void addRequiredModule(ModuleIdentifier dependency);
237 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
239 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
242 * Set identifier of current root context.
245 * of current root context
247 void setRootIdentifier(ModuleIdentifier identifier);
249 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
251 // FIXME: this seems to be unused, but looks useful.
252 void setCompletedPhase(ModelProcessingPhase completedPhase);