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 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 <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
73 Class<N> type, KT key) throws NamespaceNotAvailableException;
75 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
78 <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
81 StmtContext<?, ?, ?> getRoot();
84 * Return declared substatements. These are the statements which are explicitly written in the source model.
86 * @return Collection of declared substatements
89 Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
92 * Return effective substatements. These are the statements which are added as this statement's substatements
93 * complete their effective model phase.
95 * @return Collection of declared substatements
98 Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
100 default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
101 return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
104 default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
105 return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
109 * Builds {@link DeclaredStatement} for statement context.
114 * Builds {@link EffectiveStatement} for statement context.
118 boolean isSupportedToBuildEffective();
120 Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
122 CopyHistory getCopyHistory();
124 boolean isSupportedByFeatures();
126 Optional<StmtContext<?, ?, ?>> getOriginalCtx();
128 ModelProcessingPhase getCompletedPhase();
131 * Return version of root statement context.
133 * @return version of root statement context
135 @Nonnull YangVersion getRootVersion();
137 interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
138 extends StmtContext<A, D, E> {
141 Mutable<?, ?, ?> getParentContext();
143 <K, V, KT extends K, VT extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, KT key,
144 VT value) throws NamespaceNotAvailableException;
148 Mutable<?, ?, ?> getRoot();
151 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
152 * child and recording copy type. Resulting object may only be added as a child of this statement.
154 * @param stmt Statement to be used as a template
155 * @param type Type of copy to record in history
156 * @param targetModule Optional new target module
157 * @return copy of statement considering {@link CopyType} (augment, uses)
159 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
160 * from an alien implementation.
161 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
163 <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
164 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
167 * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
168 * child and recording copy type. Resulting object may only be added as a child of this statement.
170 * @param stmt Statement to be used as a template
171 * @param type Type of copy to record in history
172 * @return copy of statement considering {@link CopyType} (augment, uses)
174 * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
175 * from an alien implementation.
176 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
178 default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
179 final StmtContext<X, Y, Z> stmt, final CopyType type) {
180 return childCopyOf(stmt, type, null);
184 Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
187 Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
190 * Create a new inference action to be executed during specified phase. The action cannot be cancelled
191 * and will be executed even if its definition remains incomplete.
193 * @param phase Target phase in which the action will resolved.
194 * @return A new action builder.
195 * @throws NullPointerException if the specified phase is null
197 @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
200 * Adds s statement to namespace map with a key.
203 * {@link StatementNamespace} child that determines namespace to be added to
205 * of type according to namespace class specification
207 * to be added to namespace map
209 <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
210 StmtContext<?, ?, ?> stmt);
213 * Set version of root statement context.
216 * of root statement context
218 void setRootVersion(YangVersion version);
221 * Add mutable statement to seal. Each mutable statement must be sealed
222 * as the last step of statement parser processing.
224 * @param mutableStatement
225 * mutable statement which should be sealed
227 void addMutableStmtToSeal(MutableStatement mutableStatement);
230 * Add required module. Based on these dependencies are collected
231 * required sources from library sources.
234 * ModuleIdentifier of module required by current root
237 void addRequiredModule(ModuleIdentifier dependency);
239 void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
241 void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
244 * Set identifier of current root context.
247 * of current root context
249 void setRootIdentifier(ModuleIdentifier identifier);
251 void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
253 // FIXME: this seems to be unused, but looks useful.
254 void setCompletedPhase(ModelProcessingPhase completedPhase);