939d6f2d2b5c5690f6d3c1a43977ba2525394b1b
[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.SchemaPath;
21 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
22 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
23 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
24 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
25 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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 the statement argument in literal format.
50      *
51      * @return raw statement argument string
52      */
53     @Nullable
54     String rawStatementArgument();
55
56     @Nullable
57     A getStatementArgument();
58
59     /**
60      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
61      * {@link Optional#empty()} is returned.
62      *
63      * @return Optional SchemaPath
64      */
65     @Nonnull Optional<SchemaPath> getSchemaPath();
66
67     boolean isConfiguration();
68
69     boolean isEnabledSemanticVersioning();
70
71     @Nonnull
72     <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
73             Class<N> type, KT key) throws NamespaceNotAvailableException;
74
75     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
76             Class<N> type);
77
78     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
79
80     @Nonnull
81     StmtContext<?, ?, ?> getRoot();
82
83     /**
84      * Return declared substatements. These are the statements which are explicitly written in the source model.
85      *
86      * @return Collection of declared substatements
87      */
88     @Nonnull
89     Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
90
91     /**
92      * Return effective substatements. These are the statements which are added as this statement's substatements
93      * complete their effective model phase.
94      *
95      * @return Collection of declared substatements
96      */
97     @Nonnull
98     Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
99
100     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
101         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
102     }
103
104     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
105         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
106     }
107
108     /**
109      * Builds {@link DeclaredStatement} for statement context.
110      */
111     D buildDeclared();
112
113     /**
114      * Builds {@link EffectiveStatement} for statement context.
115      */
116     E buildEffective();
117
118     boolean isSupportedToBuildEffective();
119
120     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
121
122     CopyHistory getCopyHistory();
123
124     boolean isSupportedByFeatures();
125
126     Optional<StmtContext<?, ?, ?>> getOriginalCtx();
127
128     ModelProcessingPhase getCompletedPhase();
129
130     /**
131      * Return version of root statement context.
132      *
133      * @return version of root statement context
134      */
135     @Nonnull YangVersion getRootVersion();
136
137     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
138             extends StmtContext<A, D, E> {
139
140         @Override
141         Mutable<?, ?, ?> getParentContext();
142
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;
145
146         @Nonnull
147         @Override
148         Mutable<?, ?, ?> getRoot();
149
150         /**
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.
153          *
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)
158          *
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
162          */
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);
165
166         /**
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.
169          *
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)
173          *
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
177          */
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);
181         }
182
183         @Nonnull
184         Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
185
186         @Nonnull
187         Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
188
189         /**
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.
192          *
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
196          */
197         @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
198
199         /**
200          * Adds s statement to namespace map with a key.
201          *
202          * @param namespace
203          *            {@link StatementNamespace} child that determines namespace to be added to
204          * @param key
205          *            of type according to namespace class specification
206          * @param stmt
207          *            to be added to namespace map
208          */
209         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
210                 StmtContext<?, ?, ?> stmt);
211
212         /**
213          * Set version of root statement context.
214          *
215          * @param version
216          *            of root statement context
217          */
218         void setRootVersion(YangVersion version);
219
220         /**
221          * Add mutable statement to seal. Each mutable statement must be sealed
222          * as the last step of statement parser processing.
223          *
224          * @param mutableStatement
225          *            mutable statement which should be sealed
226          */
227         void addMutableStmtToSeal(MutableStatement mutableStatement);
228
229         /**
230          * Add required module. Based on these dependencies are collected required sources from library sources.
231          *
232          * @param dependency
233          *            SourceIdentifier of module required by current root
234          *            context
235          */
236         /*
237          * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
238          *        getter -- which makes it rather strange. At some point this method needs to be deprecated and its
239          *        users migrated to use proper global namespace.
240          */
241         void addRequiredSource(SourceIdentifier dependency);
242
243         void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
244
245         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
246
247         /**
248          * Set identifier of current root context.
249          *
250          * @param identifier
251          *            of current root context, must not be null
252          */
253         void setRootIdentifier(SourceIdentifier identifier);
254
255         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
256
257         // FIXME: this seems to be unused, but looks useful.
258         void setCompletedPhase(ModelProcessingPhase completedPhase);
259     }
260 }