Remove deprecated Yin/YangStatementSourceImpl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / CrossSourceStatementReactor.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.stmt.reactor;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.EnumMap;
15 import java.util.Map;
16 import java.util.Optional;
17 import java.util.Set;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.common.QNameModule;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
25 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
26 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
27
28 public final class CrossSourceStatementReactor {
29     private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
30     private final Map<ValidationBundleType, Collection<?>> supportedValidation;
31
32     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
33             final Map<ValidationBundleType, Collection<?>> supportedValidation) {
34         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
35         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
36     }
37
38     /**
39      * Create a new {@link Builder}.
40      *
41      * @return A new builder.
42      */
43     public static Builder builder() {
44         return new Builder();
45     }
46
47     /**
48      * Start a new reactor build using the default statement parser mode with all features and deviations enabled.
49      *
50      * @return A new {@link BuildAction}.
51      */
52     public BuildAction newBuild() {
53         return newBuild(StatementParserMode.DEFAULT_MODE);
54     }
55
56     /**
57      * Start a new reactor build using the default statement parser mode and enabling only the specified features
58      * and all deviations.
59      *
60      * @param supportedFeatures The set of supported features in the final SchemaContext
61      * @return A new {@link BuildAction}.
62      *
63      * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
64      */
65     @Deprecated
66     public BuildAction newBuild(final Set<QName> supportedFeatures) {
67         final BuildAction buildAction = newBuild();
68         if (supportedFeatures != null) {
69             buildAction.setSupportedFeatures(supportedFeatures);
70         }
71
72         return buildAction;
73     }
74
75     /**
76      * Start a new reactor build using the default statement parser mode and enabling only the specified features
77      * and all deviations.
78      *
79      * @param supportedFeatures The set of supported features in the final SchemaContext, if present.
80      * @return A new {@link BuildAction}.
81      *
82      * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
83      */
84     @Deprecated
85     public BuildAction newBuild(final Optional<Set<QName>> supportedFeatures) {
86         final BuildAction buildAction = newBuild();
87         if (supportedFeatures.isPresent()) {
88             buildAction.setSupportedFeatures(supportedFeatures.get());
89         }
90
91         return buildAction;
92     }
93
94     /**
95      * Start a new reactor build using the specified statement parser mode and enabling all features and deviations.
96      *
97      * @param statementParserMode Parser mode to use
98      * @return A new {@link BuildAction}.
99      * @throws NullPointerException if statementParserMode is null
100      */
101     public BuildAction newBuild(final StatementParserMode statementParserMode) {
102         return new BuildAction(statementParserMode);
103     }
104
105     /**
106      * Start a new reactor build using the specified statement parser mode and enabling only the specified features
107      * and all deviations.
108      *
109      * @param statementParserMode Parser mode to use
110      * @param supportedFeatures The set of supported features in the final SchemaContext
111      * @return A new {@link BuildAction}.
112      * @throws NullPointerException if statementParserMode is null
113      *
114      * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures() on the created
115      *             BuildAction instead.
116      */
117     @Deprecated
118     public BuildAction newBuild(final StatementParserMode statementParserMode,
119             final Set<QName> supportedFeatures) {
120         final BuildAction buildAction = new BuildAction(statementParserMode);
121         if (supportedFeatures != null) {
122             buildAction.setSupportedFeatures(supportedFeatures);
123         }
124
125         return buildAction;
126     }
127
128     /**
129      * Start a new reactor build using the specified statement parser mode and enabling only the specified features
130      * and all deviations.
131      *
132      * @param statementParserMode Parser mode to use
133      * @param supportedFeatures The set of supported features in the final SchemaContext, or absent if all features
134      *                          encountered should be supported.
135      * @return A new {@link BuildAction}.
136      * @throws NullPointerException if statementParserMode is null
137      *
138      * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures() on the created
139      *             BuildAction instead.
140      */
141     @Deprecated
142     public BuildAction newBuild(final StatementParserMode statementParserMode,
143             final Optional<Set<QName>> supportedFeatures) {
144         final BuildAction buildAction = new BuildAction(statementParserMode);
145         if (supportedFeatures.isPresent()) {
146             buildAction.setSupportedFeatures(supportedFeatures.get());
147         }
148
149         return buildAction;
150     }
151
152     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
153         private final Map<ValidationBundleType, Collection<?>> validationBundles =
154                 new EnumMap<>(ValidationBundleType.class);
155         private final Map<ModelProcessingPhase, StatementSupportBundle> bundles =
156                 new EnumMap<>(ModelProcessingPhase.class);
157
158         public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
159             bundles.put(phase, bundle);
160             return this;
161         }
162
163         public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
164             validationBundles.put(type, validationBundle);
165             return this;
166         }
167
168         @Override
169         public CrossSourceStatementReactor build() {
170             return new CrossSourceStatementReactor(bundles, validationBundles);
171         }
172     }
173
174     public class BuildAction {
175         private final BuildGlobalContext context;
176         private boolean supportedFeaturesSet = false;
177         private boolean modulesDeviatedByModulesSet = false;
178
179         BuildAction(@Nonnull final StatementParserMode statementParserMode) {
180             this.context = new BuildGlobalContext(supportedTerminology,supportedValidation,
181                     Preconditions.checkNotNull(statementParserMode));
182         }
183
184         /**
185          * Add main source. All main sources are present in resulting SchemaContext.
186          *
187          * @param source
188          *            which should be added into main sources
189          */
190         public void addSource(final StatementStreamSource source) {
191             context.addSource(source);
192         }
193
194         /**
195          * Add main sources. All main sources are present in resulting SchemaContext.
196          *
197          * @param sources
198          *            which should be added into main sources
199          */
200         public void addSources(final StatementStreamSource... sources) {
201             addSources(Arrays.asList(sources));
202         }
203
204         public void addSources(final Collection<? extends StatementStreamSource> sources) {
205             for (final StatementStreamSource source : sources) {
206                 context.addSource(source);
207             }
208         }
209
210         /**
211          * Add library sources. Only library sources required by main sources are present in resulting SchemaContext.
212          * Any other library sources are ignored and this also applies to error reporting.
213          *
214          * <p>
215          * Library sources are not supported in semantic version mode currently.
216          *
217          * @param libSources
218          *            yang sources which should be added into library sources
219          */
220         public void addLibSources(final StatementStreamSource... libSources) {
221             addLibSources(Arrays.asList(libSources));
222         }
223
224         public void addLibSources(final Collection<StatementStreamSource> libSources) {
225             for (final StatementStreamSource libSource : libSources) {
226                 context.addLibSource(libSource);
227             }
228         }
229
230         /**
231          * Set supported features based on which all if-feature statements in the
232          * parsed YANG modules will be resolved.
233          *
234          * @param supportedFeatures
235          *            Set of supported features in the final SchemaContext.
236          *            If the set is empty, no features encountered will be supported.
237          */
238         public void setSupportedFeatures(@Nonnull final Set<QName> supportedFeatures) {
239             Preconditions.checkState(!supportedFeaturesSet, "Supported features should be set only once.");
240             context.setSupportedFeatures(Preconditions.checkNotNull(supportedFeatures));
241             supportedFeaturesSet = true;
242         }
243
244         /**
245          * Set YANG modules which can be deviated by specified modules during the parsing process.
246          * Map key (QNameModule) denotes a module which can be deviated by the modules in the Map value.
247          *
248          * @param modulesDeviatedByModules
249          *            Map of YANG modules (Map key) which can be deviated by specified modules (Map value) in the final
250          *            SchemaContext. If the map is empty, no deviations encountered will be supported.
251          */
252         public void setModulesWithSupportedDeviations(
253                 @Nonnull final Map<QNameModule, Set<QNameModule>> modulesDeviatedByModules) {
254             Preconditions.checkState(!modulesDeviatedByModulesSet,
255                     "Modules with supported deviations should be set only once.");
256             context.setModulesDeviatedByModules(Preconditions.checkNotNull(modulesDeviatedByModules));
257             modulesDeviatedByModulesSet = true;
258         }
259
260         /**
261          * Build the effective model context.
262          */
263         public EffectiveModelContext build() throws ReactorException {
264             return context.build();
265         }
266
267         public EffectiveSchemaContext buildEffective() throws ReactorException {
268             return context.buildEffective();
269         }
270     }
271 }