2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableMap;
12 import com.google.common.io.ByteSource;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.EnumMap;
18 import java.util.List;
20 import java.util.Optional;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.QNameModule;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
33 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public final class CrossSourceStatementReactor {
40 private static final Logger LOG = LoggerFactory.getLogger(CrossSourceStatementReactor.class);
42 private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
43 private final Map<ValidationBundleType, Collection<?>> supportedValidation;
45 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
46 final Map<ValidationBundleType, Collection<?>> supportedValidation) {
47 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
48 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
52 * Create a new {@link Builder}.
54 * @return A new builder.
56 public static Builder builder() {
61 * Start a new reactor build using the default statement parser mode with all features and deviations enabled.
63 * @return A new {@link BuildAction}.
65 public BuildAction newBuild() {
66 return newBuild(StatementParserMode.DEFAULT_MODE);
70 * Start a new reactor build using the default statement parser mode and enabling only the specified features
73 * @param supportedFeatures The set of supported features in the final SchemaContext
74 * @return A new {@link BuildAction}.
76 * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
79 public BuildAction newBuild(final Set<QName> supportedFeatures) {
80 final BuildAction buildAction = newBuild();
81 if (supportedFeatures != null) {
82 buildAction.setSupportedFeatures(supportedFeatures);
89 * Start a new reactor build using the default statement parser mode and enabling only the specified features
92 * @param supportedFeatures The set of supported features in the final SchemaContext, if present.
93 * @return A new {@link BuildAction}.
95 * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
98 public BuildAction newBuild(final Optional<Set<QName>> supportedFeatures) {
99 final BuildAction buildAction = newBuild();
100 if (supportedFeatures.isPresent()) {
101 buildAction.setSupportedFeatures(supportedFeatures.get());
108 * Start a new reactor build using the specified statement parser mode and enabling all features and deviations.
110 * @param statementParserMode Parser mode to use
111 * @return A new {@link BuildAction}.
112 * @throws NullPointerException if statementParserMode is null
114 public BuildAction newBuild(final StatementParserMode statementParserMode) {
115 return new BuildAction(statementParserMode);
119 * Start a new reactor build using the specified statement parser mode and enabling only the specified features
120 * and all deviations.
122 * @param statementParserMode Parser mode to use
123 * @param supportedFeatures The set of supported features in the final SchemaContext
124 * @return A new {@link BuildAction}.
125 * @throws NullPointerException if statementParserMode is null
127 * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures()
128 * on the created BuildAction instead.
131 public BuildAction newBuild(final StatementParserMode statementParserMode,
132 final Set<QName> supportedFeatures) {
133 final BuildAction buildAction = new BuildAction(statementParserMode);
134 if (supportedFeatures != null) {
135 buildAction.setSupportedFeatures(supportedFeatures);
142 * Start a new reactor build using the specified statement parser mode and enabling only the specified features
143 * and all deviations.
145 * @param statementParserMode Parser mode to use
146 * @param supportedFeatures The set of supported features in the final SchemaContext, or absent if all features
147 * encountered should be supported.
148 * @return A new {@link BuildAction}.
149 * @throws NullPointerException if statementParserMode is null
151 * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures()
152 * on the created BuildAction instead.
155 public BuildAction newBuild(final StatementParserMode statementParserMode,
156 final Optional<Set<QName>> supportedFeatures) {
157 final BuildAction buildAction = new BuildAction(statementParserMode);
158 if (supportedFeatures.isPresent()) {
159 buildAction.setSupportedFeatures(supportedFeatures.get());
165 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
166 private final Map<ValidationBundleType, Collection<?>> validationBundles =
167 new EnumMap<>(ValidationBundleType.class);
168 private final Map<ModelProcessingPhase, StatementSupportBundle> bundles =
169 new EnumMap<>(ModelProcessingPhase.class);
171 public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
172 bundles.put(phase, bundle);
176 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
177 validationBundles.put(type, validationBundle);
182 public CrossSourceStatementReactor build() {
183 return new CrossSourceStatementReactor(bundles, validationBundles);
187 public class BuildAction {
188 private final BuildGlobalContext context;
189 private boolean supportedFeaturesSet = false;
190 private boolean modulesDeviatedByModulesSet = false;
192 BuildAction(@Nonnull final StatementParserMode statementParserMode) {
193 this.context = new BuildGlobalContext(supportedTerminology,supportedValidation,
194 Preconditions.checkNotNull(statementParserMode));
198 * Add main source. All main sources are present in resulting
202 * which should be added into main sources
204 public void addSource(final StatementStreamSource source) {
205 context.addSource(source);
209 * Add main sources. All main sources are present in resulting
213 * which should be added into main sources
215 public void addSources(final StatementStreamSource... sources) {
216 addSources(Arrays.asList(sources));
219 public void addSources(final Collection<? extends StatementStreamSource> sources) {
220 for (final StatementStreamSource source : sources) {
221 context.addSource(source);
226 * Add library sources. Only library sources required by main sources
227 * are present in resulting SchemaContext. Any other library sources are
228 * ignored and this also applies to error reporting.
230 * Library sources are not supported in semantic version mode currently.
233 * yang sources which should be added into library sources
235 public void addLibSources(final StatementStreamSource... libSources) {
236 for (final StatementStreamSource libSource : libSources) {
237 context.addLibSource(libSource);
242 * Set supported features based on which all if-feature statements in the
243 * parsed YANG modules will be resolved.
245 * @param supportedFeatures
246 * Set of supported features in the final SchemaContext.
247 * If the set is empty, no features encountered will be supported.
249 public void setSupportedFeatures(@Nonnull final Set<QName> supportedFeatures) {
250 Preconditions.checkState(!supportedFeaturesSet, "Supported features should be set only once.");
251 context.setSupportedFeatures(Preconditions.checkNotNull(supportedFeatures));
252 supportedFeaturesSet = true;
256 * Set YANG modules which can be deviated by specified modules during the parsing process.
257 * Map key (QNameModule) denotes a module which can be deviated by the modules in the Map value.
259 * @param modulesDeviatedByModules
260 * Map of YANG modules (Map key) which can be deviated by specified modules (Map value) in the final
261 * SchemaContext. If the map is empty, no deviations encountered will be supported.
263 public void setModulesWithSupportedDeviations(
264 @Nonnull final Map<QNameModule, Set<QNameModule>> modulesDeviatedByModules) {
265 Preconditions.checkState(!modulesDeviatedByModulesSet,
266 "Modules with supported deviations should be set only once.");
267 context.setModulesDeviatedByModules(Preconditions.checkNotNull(modulesDeviatedByModules));
268 modulesDeviatedByModulesSet = true;
272 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
273 * @throws ReactorException
275 public EffectiveModelContext build() throws ReactorException {
276 return context.build();
279 public EffectiveSchemaContext buildEffective() throws ReactorException {
280 return context.buildEffective();
284 * @deprecated Use {@link #addSources(Collection)} and {@link #buildEffective()} instead.
287 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
289 for (final ByteSource source : yangByteSources) {
290 if (source instanceof YangTextSchemaSource) {
292 addSource(YangStatementStreamSource.create((YangTextSchemaSource) source));
293 } catch (YangSyntaxErrorException e) {
294 throw new IOException("Source " + source + " failed to parse", e);
297 addSource(new YangStatementSourceImpl(source.openStream()));
301 return buildEffective();
305 * @deprecated Use {@link #addSources(Collection)} and {@link #buildEffective()} instead.
308 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
309 for (final InputStream yangInputStream : yangInputStreams) {
310 addSource(new YangStatementSourceImpl(yangInputStream));
313 return buildEffective();