BUG-4688: Rename StmtContext.Mutable.addRequiredModule()
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / StmtContext.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.spi.meta;
9
10 import com.google.common.collect.Iterables;
11 import com.google.common.collect.Streams;
12 import java.util.Collection;
13 import java.util.Map;
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;
29
30 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
31
32     @Nonnull
33     StatementSource getStatementSource();
34
35     @Nonnull
36     StatementSourceReference getStatementSourceReference();
37
38     @Nonnull
39     StatementDefinition getPublicDefinition();
40
41     /**
42      * Return the parent statement context, or null if this is the root statement.
43      *
44      * @return context of parent of statement, or null if this is the root statement.
45      */
46     @Nullable
47     StmtContext<?, ?, ?> getParentContext();
48
49     /**
50      * Return the statement argument in literal format.
51      *
52      * @return raw statement argument string
53      */
54     @Nullable
55     String rawStatementArgument();
56
57     @Nullable
58     A getStatementArgument();
59
60     /**
61      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
62      * {@link Optional#empty()} is returned.
63      *
64      * @return Optional SchemaPath
65      */
66     @Nonnull Optional<SchemaPath> getSchemaPath();
67
68     boolean isConfiguration();
69
70     boolean isEnabledSemanticVersioning();
71
72     @Nonnull
73     <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
74             Class<N> type, KT key) throws NamespaceNotAvailableException;
75
76     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
77             Class<N> type);
78
79     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
80
81     @Nonnull
82     StmtContext<?, ?, ?> getRoot();
83
84     /**
85      * Return declared substatements. These are the statements which are explicitly written in the source model.
86      *
87      * @return Collection of declared substatements
88      */
89     @Nonnull
90     Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
91
92     /**
93      * Return effective substatements. These are the statements which are added as this statement's substatements
94      * complete their effective model phase.
95      *
96      * @return Collection of declared substatements
97      */
98     @Nonnull
99     Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
100
101     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
102         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
103     }
104
105     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
106         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
107     }
108
109     /**
110      * Builds {@link DeclaredStatement} for statement context.
111      */
112     D buildDeclared();
113
114     /**
115      * Builds {@link EffectiveStatement} for statement context.
116      */
117     E buildEffective();
118
119     boolean isSupportedToBuildEffective();
120
121     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
122
123     CopyHistory getCopyHistory();
124
125     boolean isSupportedByFeatures();
126
127     Optional<StmtContext<?, ?, ?>> getOriginalCtx();
128
129     ModelProcessingPhase getCompletedPhase();
130
131     /**
132      * Return version of root statement context.
133      *
134      * @return version of root statement context
135      */
136     @Nonnull YangVersion getRootVersion();
137
138     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
139             extends StmtContext<A, D, E> {
140
141         @Override
142         Mutable<?, ?, ?> getParentContext();
143
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;
146
147         @Nonnull
148         @Override
149         Mutable<?, ?, ?> getRoot();
150
151         /**
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.
154          *
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)
159          *
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
163          */
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);
166
167         /**
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.
170          *
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)
174          *
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
178          */
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);
182         }
183
184         @Nonnull
185         Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
186
187         @Nonnull
188         Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
189
190         /**
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.
193          *
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
197          */
198         @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
199
200         /**
201          * Adds s statement to namespace map with a key.
202          *
203          * @param namespace
204          *            {@link StatementNamespace} child that determines namespace to be added to
205          * @param key
206          *            of type according to namespace class specification
207          * @param stmt
208          *            to be added to namespace map
209          */
210         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
211                 StmtContext<?, ?, ?> stmt);
212
213         /**
214          * Set version of root statement context.
215          *
216          * @param version
217          *            of root statement context
218          */
219         void setRootVersion(YangVersion version);
220
221         /**
222          * Add mutable statement to seal. Each mutable statement must be sealed
223          * as the last step of statement parser processing.
224          *
225          * @param mutableStatement
226          *            mutable statement which should be sealed
227          */
228         void addMutableStmtToSeal(MutableStatement mutableStatement);
229
230         /**
231          * Add required module. Based on these dependencies are collected required sources from library sources.
232          *
233          * @param dependency
234          *            SourceIdentifier of module required by current root
235          *            context
236          */
237         /*
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.
241          */
242         void addRequiredSource(SourceIdentifier dependency);
243
244         void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
245
246         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
247
248         /**
249          * Set identifier of current root context.
250          *
251          * @param identifier
252          *            of current root context
253          */
254         void setRootIdentifier(ModuleIdentifier identifier);
255
256         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
257
258         // FIXME: this seems to be unused, but looks useful.
259         void setCompletedPhase(ModelProcessingPhase completedPhase);
260     }
261 }