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