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