52bd1aa1c965af39a60cb069cbbea0c9203b9725
[yangtools.git] / yang / yang-parser-impl / 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.base.Optional;
11 import java.util.Collection;
12 import java.util.Map;
13 import javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.common.QNameModule;
16 import org.opendaylight.yangtools.yang.common.YangVersion;
17 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
18 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
19 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
20 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
21 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
22 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
23 import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
24 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
26 import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
27
28 public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
29
30     @Nonnull
31     StatementSource getStatementSource();
32
33     @Nonnull
34     StatementSourceReference getStatementSourceReference();
35
36     @Nonnull
37     StatementDefinition getPublicDefinition();
38
39     /**
40      * @return context of parent of statement
41      */
42     @Nullable
43     StmtContext<?, ?, ?> getParentContext();
44
45     /**
46      * @return raw statement argument string
47      */
48     @Nullable
49     String rawStatementArgument();
50
51     @Nullable
52     A getStatementArgument();
53
54     /**
55      * Return the {@link SchemaPath} of this statement. Not all statements have a SchemaPath, in which case
56      * {@link Optional#absent()} is returned.
57      *
58      * @return Optional SchemaPath
59      */
60     @Nonnull Optional<SchemaPath> getSchemaPath();
61
62     boolean isConfiguration();
63
64     /**
65      * Checks whether this statement is placed within a 'yang-data' extension statement.
66      * Some YANG statements are constrained when used within a 'yang-data' statement.
67      * See the following link for more information - https://tools.ietf.org/html/rfc8040#section-8
68      *
69      * @return true if it is placed within a 'yang-data' extension statement, otherwise false
70      */
71     boolean isInYangDataExtensionBody();
72
73     boolean isEnabledSemanticVersioning();
74
75     @Nonnull
76     <K, V, KT extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(
77             Class<N> type, KT key) throws NamespaceNotAvailableException;
78
79     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
80             Class<N> type);
81
82     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
83
84     @Nonnull
85     StmtContext<?, ?, ?> getRoot();
86
87     /**
88      * Return declared substatements. These are the statements which are explicitly written in the source model.
89      *
90      * @return Collection of declared substatements
91      */
92     @Nonnull
93     Collection<StatementContextBase<?, ?, ?>> declaredSubstatements();
94
95     /**
96      * Return effective substatements. These are the statements which are added as this statement's substatements
97      * complete their effective model phase.
98      *
99      * @return Collection of declared substatements
100      */
101     @Nonnull
102     Collection<StatementContextBase<?, ?, ?>> effectiveSubstatements();
103
104     /**
105      * Builds {@link DeclaredStatement} for statement context.
106      */
107     D buildDeclared();
108
109     /**
110      * Builds {@link EffectiveStatement} for statement context
111      */
112     E buildEffective();
113
114     boolean isSupportedToBuildEffective();
115
116     void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
117
118     Collection<StatementContextBase<?, ?, ?>> getEffectOfStatement();
119
120     void addAsEffectOfStatement(StatementContextBase<?, ?, ?> ctx);
121
122     void addAsEffectOfStatement(Collection<StatementContextBase<?, ?, ?>> ctxs);
123
124     StatementContextBase<?, ?, ?> createCopy(
125             StatementContextBase<?, ?, ?> newParent, CopyType typeOfCopy)
126             throws SourceException;
127
128     StatementContextBase<?, ?, ?> createCopy(QNameModule newQNameModule,
129             StatementContextBase<?, ?, ?> newParent, CopyType typeOfCopy)
130             throws SourceException;
131
132     CopyHistory getCopyHistory();
133
134     enum SupportedByFeatures {
135         UNDEFINED, SUPPORTED, NOT_SUPPORTED
136     }
137
138     SupportedByFeatures getSupportedByFeatures();
139
140     void appendCopyHistory(CopyType typeOfCopy, CopyHistory toAppend);
141
142     StatementContextBase<?, ?, ?> getOriginalCtx();
143
144     void setOriginalCtx(StatementContextBase<?, ?, ?> originalCtx);
145
146     boolean isRootContext();
147
148     void setOrder(int order);
149
150     int getOrder();
151
152     void setCompletedPhase(ModelProcessingPhase completedPhase);
153
154     ModelProcessingPhase getCompletedPhase();
155
156     /**
157      * Return version of root statement context.
158      *
159      * @return version of root statement context
160      */
161     @Nonnull YangVersion getRootVersion();
162
163     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
164             extends StmtContext<A, D, E> {
165
166         @Override
167         StmtContext.Mutable<?, ?, ?> getParentContext();
168
169         <K, V, KT extends K, VT extends V, N extends IdentifierNamespace<K, V>> void addToNs(
170                 Class<N> type, KT key, VT value)
171                 throws NamespaceNotAvailableException;
172
173         @Nonnull
174         @Override
175         StmtContext.Mutable<?, ?, ?> getRoot();
176
177         /**
178          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
179          * and will be executed even if its definition remains incomplete.
180          *
181          * @param phase Target phase in which the action will resolved.
182          * @return A new action builder.
183          * @throws NullPointerException if the specified phase is null
184          */
185         @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
186
187         /**
188          * adds statement to namespace map with the key
189          *
190          * @param namespace
191          *            {@link StatementNamespace} child that determines namespace to be added to
192          * @param key
193          *            of type according to namespace class specification
194          * @param stmt
195          *            to be added to namespace map
196          */
197         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(
198                 Class<N> namespace, KT key, StmtContext<?, ?, ?> stmt);
199
200         void setSupportedByFeatures(boolean isSupported);
201
202         /**
203          * Set version of root statement context.
204          *
205          * @param version
206          *            of root statement context
207          */
208         void setRootVersion(YangVersion version);
209
210         /**
211          * Add mutable statement to seal. Each mutable statement must be sealed
212          * as the last step of statement parser processing.
213          *
214          * @param mutableStatement
215          *            mutable statement which should be sealed
216          */
217         void addMutableStmtToSeal(MutableStatement mutableStatement);
218
219         /**
220          * Add required module. Based on these dependencies are collected
221          * required sources from library sources.
222          *
223          * @param dependency
224          *            ModuleIdentifier of module required by current root
225          *            context
226          */
227         void addRequiredModule(ModuleIdentifier dependency);
228
229         /**
230          * Set identifier of current root context.
231          *
232          * @param identifier
233          *            of current root context
234          */
235         void setRootIdentifier(ModuleIdentifier identifier);
236     }
237 }