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;
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.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;
27 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
28 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
30 public class CrossSourceStatementReactor {
32 private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
33 private final Map<ValidationBundleType, Collection<?>> supportedValidation;
35 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
36 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
37 this.supportedValidation = ImmutableMap.of();
40 CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
41 final Map<ValidationBundleType, Collection<?>> supportedValidation) {
42 this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
43 this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
46 public static Builder builder() {
50 public final BuildAction newBuild() {
51 return newBuild(StatementParserMode.DEFAULT_MODE);
54 public final BuildAction newBuild(final Set<QName> supportedFeatures) {
55 return new BuildAction(StatementParserMode.DEFAULT_MODE, supportedFeatures);
58 public final BuildAction newBuild(final StatementParserMode statementParserMode) {
59 return new BuildAction(statementParserMode, null);
62 public final BuildAction newBuild(final StatementParserMode statementParserMode,
63 final Set<QName> supportedFeatures) {
64 return new BuildAction(statementParserMode, supportedFeatures);
67 public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
69 final Map<ModelProcessingPhase, StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
70 final Map<ValidationBundleType, Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
72 public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
73 bundles.put(phase, bundle);
77 public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
78 validationBundles.put(type, validationBundle);
83 public CrossSourceStatementReactor build() {
84 return new CrossSourceStatementReactor(bundles, validationBundles);
88 public class BuildAction {
89 private final BuildGlobalContext context;
91 public BuildAction() {
92 this(StatementParserMode.DEFAULT_MODE);
95 public BuildAction(final StatementParserMode statementParserMode) {
96 this(statementParserMode, null);
99 public BuildAction(final Set<QName> supportedFeatures) {
100 this(StatementParserMode.DEFAULT_MODE, supportedFeatures);
103 public BuildAction(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures) {
104 this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, statementParserMode,
108 public void addSource(final StatementStreamSource source) {
109 context.addSource(source);
112 public void addSources(final StatementStreamSource... sources) {
113 for (final StatementStreamSource source : sources) {
114 context.addSource(source);
119 * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
120 * @throws ReactorException
122 public EffectiveModelContext build() throws ReactorException {
123 return context.build();
126 public EffectiveSchemaContext buildEffective() throws ReactorException {
127 return context.buildEffective();
130 public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
132 for (final ByteSource yangByteSource : yangByteSources) {
133 addSource(new YangStatementSourceImpl(yangByteSource.openStream()));
136 return buildEffective();
139 public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
140 for (final InputStream yangInputStream : yangInputStreams) {
141 addSource(new YangStatementSourceImpl(yangInputStream));
144 return buildEffective();