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.collect.ImmutableMap;
11 import com.google.common.io.ByteSource;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.Collection;
15 import java.util.EnumMap;
16 import java.util.List;
18 import java.util.function.Predicate;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
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;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
29 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
31 public class CrossSourceStatementReactor {
33 private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
34 private final Map<ValidationBundleType, Collection<?>> supportedValidation;
36 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
37 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
38 this.supportedValidation = ImmutableMap.of();
41 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
42 final Map<ValidationBundleType, Collection<?>> supportedValidation) {
43 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
44 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
47 public static Builder builder() {
51 public final BuildAction newBuild() {
52 return newBuild(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
55 public final BuildAction newBuild(final Predicate<QName> isFeatureSupported) {
56 return new BuildAction(StatementParserMode.DEFAULT_MODE, isFeatureSupported);
59 public final BuildAction newBuild(final StatementParserMode statementParserMode) {
60 return new BuildAction(statementParserMode, IfFeaturePredicates.ALL_FEATURES);
63 public final BuildAction newBuild(final StatementParserMode statementParserMode,
64 final Predicate<QName> isFeatureSupported) {
65 return new BuildAction(statementParserMode, isFeatureSupported);
68 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
70 final Map<ModelProcessingPhase, StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
71 final Map<ValidationBundleType, Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
73 public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
74 bundles.put(phase, bundle);
78 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
79 validationBundles.put(type, validationBundle);
84 public CrossSourceStatementReactor build() {
85 return new CrossSourceStatementReactor(bundles, validationBundles);
89 public class BuildAction {
90 private final BuildGlobalContext context;
92 public BuildAction() {
93 this(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
96 public BuildAction(final StatementParserMode statementParserMode) {
97 this(statementParserMode, IfFeaturePredicates.ALL_FEATURES);
100 public BuildAction(final Predicate<QName> isFeatureSupported) {
101 this(StatementParserMode.DEFAULT_MODE, isFeatureSupported);
104 public BuildAction(final StatementParserMode statementParserMode, final Predicate<QName> isFeatureSupported) {
105 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, statementParserMode,
109 public void addSource(final StatementStreamSource source) {
110 context.addSource(source);
113 public void addSources(final StatementStreamSource... sources) {
114 for (final StatementStreamSource source : sources) {
115 context.addSource(source);
120 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
121 * @throws ReactorException
123 public EffectiveModelContext build() throws ReactorException {
124 return context.build();
127 public EffectiveSchemaContext buildEffective() throws ReactorException {
128 return context.buildEffective();
131 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
133 for (final ByteSource yangByteSource : yangByteSources) {
134 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
137 return buildEffective();
140 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
141 for (final InputStream yangInputStream : yangInputStreams) {
142 addSource(new YangStatementSourceImpl(yangInputStream));
145 return buildEffective();