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 java.util.Arrays;
13 import java.util.Collection;
14 import java.util.EnumMap;
16 import java.util.Optional;
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;
28 public final class CrossSourceStatementReactor {
29 private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
30 private final Map<ValidationBundleType, Collection<?>> supportedValidation;
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);
39 * Create a new {@link Builder}.
41 * @return A new builder.
43 public static Builder builder() {
48 * Start a new reactor build using the default statement parser mode with all features and deviations enabled.
50 * @return A new {@link BuildAction}.
52 public BuildAction newBuild() {
53 return newBuild(StatementParserMode.DEFAULT_MODE);
57 * Start a new reactor build using the default statement parser mode and enabling only the specified features
60 * @param supportedFeatures The set of supported features in the final SchemaContext
61 * @return A new {@link BuildAction}.
63 * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
66 public BuildAction newBuild(final Set<QName> supportedFeatures) {
67 final BuildAction buildAction = newBuild();
68 if (supportedFeatures != null) {
69 buildAction.setSupportedFeatures(supportedFeatures);
76 * Start a new reactor build using the default statement parser mode and enabling only the specified features
79 * @param supportedFeatures The set of supported features in the final SchemaContext, if present.
80 * @return A new {@link BuildAction}.
82 * @deprecated Use {@link #newBuild()} and then call setSupportedFeatures() on the created BuildAction instead.
85 public BuildAction newBuild(final Optional<Set<QName>> supportedFeatures) {
86 final BuildAction buildAction = newBuild();
87 if (supportedFeatures.isPresent()) {
88 buildAction.setSupportedFeatures(supportedFeatures.get());
95 * Start a new reactor build using the specified statement parser mode and enabling all features and deviations.
97 * @param statementParserMode Parser mode to use
98 * @return A new {@link BuildAction}.
99 * @throws NullPointerException if statementParserMode is null
101 public BuildAction newBuild(final StatementParserMode statementParserMode) {
102 return new BuildAction(statementParserMode);
106 * Start a new reactor build using the specified statement parser mode and enabling only the specified features
107 * and all deviations.
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
114 * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures() on the created
115 * BuildAction instead.
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);
129 * Start a new reactor build using the specified statement parser mode and enabling only the specified features
130 * and all deviations.
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
138 * @deprecated Use {@link #newBuild(StatementParserMode)} and then call setSupportedFeatures() on the created
139 * BuildAction instead.
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());
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);
158 public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
159 bundles.put(phase, bundle);
163 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
164 validationBundles.put(type, validationBundle);
169 public CrossSourceStatementReactor build() {
170 return new CrossSourceStatementReactor(bundles, validationBundles);
174 public class BuildAction {
175 private final BuildGlobalContext context;
176 private boolean supportedFeaturesSet = false;
177 private boolean modulesDeviatedByModulesSet = false;
179 BuildAction(@Nonnull final StatementParserMode statementParserMode) {
180 this.context = new BuildGlobalContext(supportedTerminology,supportedValidation,
181 Preconditions.checkNotNull(statementParserMode));
185 * Add main source. All main sources are present in resulting SchemaContext.
188 * which should be added into main sources
190 public void addSource(final StatementStreamSource source) {
191 context.addSource(source);
195 * Add main sources. All main sources are present in resulting SchemaContext.
198 * which should be added into main sources
200 public void addSources(final StatementStreamSource... sources) {
201 addSources(Arrays.asList(sources));
204 public void addSources(final Collection<? extends StatementStreamSource> sources) {
205 for (final StatementStreamSource source : sources) {
206 context.addSource(source);
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.
215 * Library sources are not supported in semantic version mode currently.
218 * yang sources which should be added into library sources
220 public void addLibSources(final StatementStreamSource... libSources) {
221 addLibSources(Arrays.asList(libSources));
224 public void addLibSources(final Collection<StatementStreamSource> libSources) {
225 for (final StatementStreamSource libSource : libSources) {
226 context.addLibSource(libSource);
231 * Set supported features based on which all if-feature statements in the
232 * parsed YANG modules will be resolved.
234 * @param supportedFeatures
235 * Set of supported features in the final SchemaContext.
236 * If the set is empty, no features encountered will be supported.
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;
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.
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.
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;
261 * Build the effective model context.
263 public EffectiveModelContext build() throws ReactorException {
264 return context.build();
267 public EffectiveSchemaContext buildEffective() throws ReactorException {
268 return context.buildEffective();