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 org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.QNameModule;
23 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
24 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
26 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
27 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
29 public final class CrossSourceStatementReactor {
30 private final ImmutableMap<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
31 private final ImmutableMap<ValidationBundleType, Collection<?>> supportedValidation;
33 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
34 final Map<ValidationBundleType, Collection<?>> supportedValidation) {
35 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
36 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
40 * Create a new {@link Builder}.
42 * @return A new builder.
44 public static @NonNull Builder builder() {
49 * Start a new reactor build using the default statement parser mode with all features and deviations enabled.
51 * @return A new {@link BuildAction}.
53 public @NonNull BuildAction newBuild() {
54 return new BuildAction(supportedTerminology, supportedValidation);
57 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
58 private final Map<ValidationBundleType, Collection<?>> validationBundles =
59 new EnumMap<>(ValidationBundleType.class);
60 private final Map<ModelProcessingPhase, StatementSupportBundle> bundles =
61 new EnumMap<>(ModelProcessingPhase.class);
63 public @NonNull Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
64 bundles.put(phase, bundle);
68 public @NonNull Builder setValidationBundle(final ValidationBundleType type,
69 final Collection<?> validationBundle) {
70 validationBundles.put(type, validationBundle);
75 public CrossSourceStatementReactor build() {
76 return new CrossSourceStatementReactor(bundles, validationBundles);
80 public static class BuildAction {
81 private final BuildGlobalContext context;
82 private boolean supportedFeaturesSet = false;
83 private boolean modulesDeviatedByModulesSet = false;
85 BuildAction(final ImmutableMap<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
86 final ImmutableMap<ValidationBundleType, Collection<?>> supportedValidation) {
87 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation);
91 * Add main source. All main sources are present in resulting SchemaContext.
93 * @param source which should be added into main sources
94 * @return This build action, for fluent use.
95 * @throws NullPointerException if @{code source} is null
97 public @NonNull BuildAction addSource(final StatementStreamSource source) {
98 context.addSource(source);
103 * Add main sources. All main sources are present in resulting SchemaContext.
105 * @param sources which should be added into main sources
106 * @return This build action, for fluent use.
107 * @throws NullPointerException if @{code sources} is null or contains a null element
109 public @NonNull BuildAction addSources(final StatementStreamSource... sources) {
110 addSources(Arrays.asList(sources));
115 * Add main sources. All main sources are present in resulting SchemaContext.
117 * @param sources which should be added into main sources
118 * @return This build action, for fluent use.
119 * @throws NullPointerException if @{code sources} is null or contains a null element
121 public @NonNull BuildAction addSources(final @NonNull Collection<? extends StatementStreamSource> sources) {
122 for (final StatementStreamSource source : sources) {
123 context.addSource(requireNonNull(source));
129 * Add a library source. Only library sources required by main sources are present in resulting SchemaContext.
130 * Any other library sources are ignored and this also applies to error reporting.
133 * Library sources are not supported in semantic version mode currently.
135 * @param libSource source which should be added into library sources
136 * @return This build action, for fluent use.
137 * @throws NullPointerException if @{code libSource} is null
139 public @NonNull BuildAction addLibSource(final StatementStreamSource libSource) {
140 context.addLibSource(libSource);
145 * Add library sources. Only library sources required by main sources are present in resulting SchemaContext.
146 * Any other library sources are ignored and this also applies to error reporting.
149 * Library sources are not supported in semantic version mode currently.
151 * @param libSources sources which should be added into library sources
152 * @return This build action, for fluent use.
153 * @throws NullPointerException if @{code libSources} is null or contains a null element
155 public @NonNull BuildAction addLibSources(final StatementStreamSource... libSources) {
156 addLibSources(Arrays.asList(libSources));
161 * Add library sources. Only library sources required by main sources are present in resulting SchemaContext.
162 * Any other library sources are ignored and this also applies to error reporting.
165 * Library sources are not supported in semantic version mode currently.
167 * @param libSources sources which should be added into library sources
168 * @return This build action, for fluent use.
169 * @throws NullPointerException if @{code libSources} is null or contains a null element
171 public @NonNull BuildAction addLibSources(final Collection<StatementStreamSource> libSources) {
172 for (final StatementStreamSource libSource : libSources) {
173 context.addLibSource(libSource);
179 * Set supported features based on which all if-feature statements in the
180 * parsed YANG modules will be resolved.
182 * @param supportedFeatures
183 * Set of supported features in the final SchemaContext.
184 * If the set is empty, no features encountered will be supported.
185 * @return This build action, for fluent use.
187 public @NonNull BuildAction setSupportedFeatures(final @NonNull Set<QName> supportedFeatures) {
188 checkState(!supportedFeaturesSet, "Supported features should be set only once.");
189 context.setSupportedFeatures(requireNonNull(supportedFeatures));
190 supportedFeaturesSet = true;
195 * Set YANG modules which can be deviated by specified modules during the parsing process.
196 * Map key (QNameModule) denotes a module which can be deviated by the modules in the Map value.
198 * @param modulesDeviatedByModules
199 * Map of YANG modules (Map key) which can be deviated by specified modules (Map value) in the final
200 * SchemaContext. If the map is empty, no deviations encountered will be supported.
201 * @return This build action, for fluent use.
203 public @NonNull BuildAction setModulesWithSupportedDeviations(
204 final @NonNull SetMultimap<QNameModule, QNameModule> modulesDeviatedByModules) {
205 checkState(!modulesDeviatedByModulesSet, "Modules with supported deviations should be set only once.");
206 context.setModulesDeviatedByModules(requireNonNull(modulesDeviatedByModules));
207 modulesDeviatedByModulesSet = true;
212 * Build the effective model context.
214 public ReactorDeclaredModel build() throws ReactorException {
215 return context.build();
218 public EffectiveSchemaContext buildEffective() throws ReactorException {
219 return context.buildEffective();