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 static com.google.common.base.Preconditions.checkState;
11 import static java.util.Objects.requireNonNull;
13 import com.google.common.collect.ImmutableMap;
14 import com.google.common.collect.SetMultimap;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.EnumMap;
20 import javax.annotation.Nonnull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
27 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
28 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
30 public final class CrossSourceStatementReactor {
31 private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
32 private final Map<ValidationBundleType, Collection<?>> supportedValidation;
34 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
35 final Map<ValidationBundleType, Collection<?>> supportedValidation) {
36 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
37 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
41 * Create a new {@link Builder}.
43 * @return A new builder.
45 public static Builder builder() {
50 * Start a new reactor build using the default statement parser mode with all features and deviations enabled.
52 * @return A new {@link BuildAction}.
54 public BuildAction newBuild() {
55 return newBuild(StatementParserMode.DEFAULT_MODE);
59 * Start a new reactor build using the specified statement parser mode and enabling all features and deviations.
61 * @param statementParserMode Parser mode to use
62 * @return A new {@link BuildAction}.
63 * @throws NullPointerException if statementParserMode is null
65 public BuildAction newBuild(final StatementParserMode statementParserMode) {
66 return new BuildAction(statementParserMode);
69 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
70 private final Map<ValidationBundleType, Collection<?>> validationBundles =
71 new EnumMap<>(ValidationBundleType.class);
72 private final Map<ModelProcessingPhase, StatementSupportBundle> bundles =
73 new EnumMap<>(ModelProcessingPhase.class);
75 public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
76 bundles.put(phase, bundle);
80 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
81 validationBundles.put(type, validationBundle);
86 public CrossSourceStatementReactor build() {
87 return new CrossSourceStatementReactor(bundles, validationBundles);
91 public class BuildAction {
92 private final BuildGlobalContext context;
93 private boolean supportedFeaturesSet = false;
94 private boolean modulesDeviatedByModulesSet = false;
96 BuildAction(@Nonnull final StatementParserMode statementParserMode) {
97 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation,
98 requireNonNull(statementParserMode));
102 * Add main source. All main sources are present in resulting SchemaContext.
105 * which should be added into main sources
106 * @return This build action, for fluent use.
108 public BuildAction addSource(final StatementStreamSource source) {
109 context.addSource(source);
114 * Add main sources. All main sources are present in resulting SchemaContext.
117 * which should be added into main sources
118 * @return This build action, for fluent use.
120 public BuildAction addSources(final StatementStreamSource... sources) {
121 addSources(Arrays.asList(sources));
125 public BuildAction addSources(final Collection<? extends StatementStreamSource> sources) {
126 for (final StatementStreamSource source : sources) {
127 context.addSource(source);
133 * Add library sources. Only library sources required by main sources are present in resulting SchemaContext.
134 * Any other library sources are ignored and this also applies to error reporting.
137 * Library sources are not supported in semantic version mode currently.
140 * yang sources which should be added into library sources
141 * @return This build action, for fluent use.
143 public BuildAction addLibSources(final StatementStreamSource... libSources) {
144 addLibSources(Arrays.asList(libSources));
148 public BuildAction addLibSources(final Collection<StatementStreamSource> libSources) {
149 for (final StatementStreamSource libSource : libSources) {
150 context.addLibSource(libSource);
156 * Set supported features based on which all if-feature statements in the
157 * parsed YANG modules will be resolved.
159 * @param supportedFeatures
160 * Set of supported features in the final SchemaContext.
161 * If the set is empty, no features encountered will be supported.
162 * @return This build action, for fluent use.
164 public BuildAction setSupportedFeatures(@Nonnull final Set<QName> supportedFeatures) {
165 checkState(!supportedFeaturesSet, "Supported features should be set only once.");
166 context.setSupportedFeatures(requireNonNull(supportedFeatures));
167 supportedFeaturesSet = true;
172 * Set YANG modules which can be deviated by specified modules during the parsing process.
173 * Map key (QNameModule) denotes a module which can be deviated by the modules in the Map value.
175 * @param modulesDeviatedByModules
176 * Map of YANG modules (Map key) which can be deviated by specified modules (Map value) in the final
177 * SchemaContext. If the map is empty, no deviations encountered will be supported.
178 * @return This build action, for fluent use.
180 public BuildAction setModulesWithSupportedDeviations(
181 @Nonnull final SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
182 checkState(!modulesDeviatedByModulesSet, "Modules with supported deviations should be set only once.");
183 context.setModulesDeviatedByModules(requireNonNull(modulesDeviatedByModules));
184 modulesDeviatedByModulesSet = true;
189 * Build the effective model context.
191 public EffectiveModelContext build() throws ReactorException {
192 return context.build();
195 public EffectiveSchemaContext buildEffective() throws ReactorException {
196 return context.buildEffective();