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