Fix code smells in 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.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     /**
72      * Return a value associated with specified key within a namespace.
73      *
74      * @param type Namespace type
75      * @param key Key
76      * @param <K> namespace key type
77      * @param <V> namespace value type
78      * @param <N> namespace type
79      * @param <T> key type
80      * @return Value, or null if there is no element
81      * @throws NamespaceNotAvailableException when the namespace is not available.
82      */
83     @Nonnull
84     <K, V, T extends K, N extends IdentifierNamespace<K, V>> V getFromNamespace(Class<N> type, T key) ;
85
86     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromNamespace(
87             Class<N> type);
88
89     <K, V, N extends IdentifierNamespace<K, V>> Map<K, V> getAllFromCurrentStmtCtxNamespace(Class<N> type);
90
91     @Nonnull
92     StmtContext<?, ?, ?> getRoot();
93
94     /**
95      * Return declared substatements. These are the statements which are explicitly written in the source model.
96      *
97      * @return Collection of declared substatements
98      */
99     @Nonnull
100     Collection<? extends StmtContext<?, ?, ?>> declaredSubstatements();
101
102     /**
103      * Return effective substatements. These are the statements which are added as this statement's substatements
104      * complete their effective model phase.
105      *
106      * @return Collection of declared substatements
107      */
108     @Nonnull
109     Collection<? extends StmtContext<?, ?, ?>> effectiveSubstatements();
110
111     default Iterable<? extends StmtContext<?, ?, ?>> allSubstatements() {
112         return Iterables.concat(declaredSubstatements(), effectiveSubstatements());
113     }
114
115     default Stream<? extends StmtContext<?, ?, ?>> allSubstatementsStream() {
116         return Streams.concat(declaredSubstatements().stream(), effectiveSubstatements().stream());
117     }
118
119     /**
120      * Builds {@link DeclaredStatement} for statement context.
121      */
122     D buildDeclared();
123
124     /**
125      * Builds {@link EffectiveStatement} for statement context.
126      */
127     E buildEffective();
128
129     boolean isSupportedToBuildEffective();
130
131     Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
132
133     CopyHistory getCopyHistory();
134
135     boolean isSupportedByFeatures();
136
137     Optional<StmtContext<?, ?, ?>> getOriginalCtx();
138
139     ModelProcessingPhase getCompletedPhase();
140
141     /**
142      * Return version of root statement context.
143      *
144      * @return version of root statement context
145      */
146     @Nonnull YangVersion getRootVersion();
147
148     interface Mutable<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
149             extends StmtContext<A, D, E> {
150
151         @Override
152         Mutable<?, ?, ?> getParentContext();
153
154         /**
155          * Associate a value with a key within a namespace.
156          *
157          * @param type Namespace type
158          * @param key Key
159          * @param value value
160          * @param <K> namespace key type
161          * @param <V> namespace value type
162          * @param <N> namespace type
163          * @param <T> key type
164          * @param <U> value type
165          * @throws NamespaceNotAvailableException when the namespace is not available.
166          */
167         <K, V, T extends K, U extends V, N extends IdentifierNamespace<K, V>> void addToNs(Class<N> type, T key,
168                 U value);
169
170         @Nonnull
171         @Override
172         Mutable<?, ?, ?> getRoot();
173
174         /**
175          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
176          * child and recording copy type. Resulting object may only be added as a child of this statement.
177          *
178          * @param stmt Statement to be used as a template
179          * @param type Type of copy to record in history
180          * @param targetModule Optional new target module
181          * @return copy of statement considering {@link CopyType} (augment, uses)
182          *
183          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
184          *                                  from an alien implementation.
185          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
186          */
187         <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
188                 StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
189
190         /**
191          * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
192          * child and recording copy type. Resulting object may only be added as a child of this statement.
193          *
194          * @param stmt Statement to be used as a template
195          * @param type Type of copy to record in history
196          * @return copy of statement considering {@link CopyType} (augment, uses)
197          *
198          * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
199          *                                  from an alien implementation.
200          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
201          */
202         default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
203                 final StmtContext<X, Y, Z> stmt, final CopyType type) {
204             return childCopyOf(stmt, type, null);
205         }
206
207         @Nonnull
208         Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
209
210         @Nonnull
211         Collection<? extends Mutable<?, ?, ?>> mutableEffectiveSubstatements();
212
213         /**
214          * Create a new inference action to be executed during specified phase. The action cannot be cancelled
215          * and will be executed even if its definition remains incomplete.
216          *
217          * @param phase Target phase in which the action will resolved.
218          * @return A new action builder.
219          * @throws NullPointerException if the specified phase is null
220          */
221         @Nonnull ModelActionBuilder newInferenceAction(@Nonnull ModelProcessingPhase phase);
222
223         /**
224          * Adds s statement to namespace map with a key.
225          *
226          * @param namespace
227          *            {@link StatementNamespace} child that determines namespace to be added to
228          * @param key
229          *            of type according to namespace class specification
230          * @param stmt
231          *            to be added to namespace map
232          */
233         <K, KT extends K, N extends StatementNamespace<K, ?, ?>> void addContext(Class<N> namespace, KT key,
234                 StmtContext<?, ?, ?> stmt);
235
236         /**
237          * Set version of root statement context.
238          *
239          * @param version
240          *            of root statement context
241          */
242         void setRootVersion(YangVersion version);
243
244         /**
245          * Add mutable statement to seal. Each mutable statement must be sealed
246          * as the last step of statement parser processing.
247          *
248          * @param mutableStatement
249          *            mutable statement which should be sealed
250          */
251         void addMutableStmtToSeal(MutableStatement mutableStatement);
252
253         /**
254          * Add required module. Based on these dependencies are collected required sources from library sources.
255          *
256          * @param dependency
257          *            SourceIdentifier of module required by current root
258          *            context
259          */
260         /*
261          * FIXME: this method is used solely during SOURCE_PRE_LINKAGE reactor phase and does not have a corresponding
262          *        getter -- which makes it rather strange. At some point this method needs to be deprecated and its
263          *        users migrated to use proper global namespace.
264          */
265         void addRequiredSource(SourceIdentifier dependency);
266
267         void addAsEffectOfStatement(StmtContext<?, ?, ?> ctx);
268
269         void addAsEffectOfStatement(Collection<? extends StmtContext<?, ?, ?>> ctxs);
270
271         /**
272          * Set identifier of current root context.
273          *
274          * @param identifier
275          *            of current root context, must not be null
276          */
277         void setRootIdentifier(SourceIdentifier identifier);
278
279         void setIsSupportedToBuildEffective(boolean isSupportedToBuild);
280
281         // FIXME: this seems to be unused, but looks useful.
282         void setCompletedPhase(ModelProcessingPhase completedPhase);
283     }
284 }