BUG-7052: introduce YangStatementStreamSource
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / CrossSourceStatementReactor.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
9
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;
17 import java.util.Map;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
22 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
23 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
24 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
25 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
32
33 public class CrossSourceStatementReactor {
34
35     private final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology;
36     private final Map<ValidationBundleType, Collection<?>> supportedValidation;
37
38     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology) {
39         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
40         this.supportedValidation = ImmutableMap.of();
41     }
42
43     CrossSourceStatementReactor(final Map<ModelProcessingPhase, StatementSupportBundle> supportedTerminology,
44             final Map<ValidationBundleType, Collection<?>> supportedValidation) {
45         this.supportedTerminology = ImmutableMap.copyOf(supportedTerminology);
46         this.supportedValidation = ImmutableMap.copyOf(supportedValidation);
47     }
48
49     public static Builder builder() {
50         return new Builder();
51     }
52
53     public final BuildAction newBuild() {
54         return newBuild(StatementParserMode.DEFAULT_MODE);
55     }
56
57     public final BuildAction newBuild(final Set<QName> supportedFeatures) {
58         return new BuildAction(StatementParserMode.DEFAULT_MODE, supportedFeatures);
59     }
60
61     public final BuildAction newBuild(final StatementParserMode statementParserMode) {
62         return new BuildAction(statementParserMode, null);
63     }
64
65     public final BuildAction newBuild(final StatementParserMode statementParserMode,
66             final Set<QName> supportedFeatures) {
67         return new BuildAction(statementParserMode, supportedFeatures);
68     }
69
70     public static class Builder implements org.opendaylight.yangtools.concepts.Builder<CrossSourceStatementReactor> {
71
72         final Map<ModelProcessingPhase, StatementSupportBundle> bundles = new EnumMap<>(ModelProcessingPhase.class);
73         final Map<ValidationBundleType, Collection<?>> validationBundles = new EnumMap<>(ValidationBundleType.class);
74
75         public Builder setBundle(final ModelProcessingPhase phase, final StatementSupportBundle bundle) {
76             bundles.put(phase, bundle);
77             return this;
78         }
79
80         public Builder setValidationBundle(final ValidationBundleType type, final Collection<?> validationBundle) {
81             validationBundles.put(type, validationBundle);
82             return this;
83         }
84
85         @Override
86         public CrossSourceStatementReactor build() {
87             return new CrossSourceStatementReactor(bundles, validationBundles);
88         }
89     }
90
91     public class BuildAction {
92         private final BuildGlobalContext context;
93
94         public BuildAction() {
95             this(StatementParserMode.DEFAULT_MODE);
96         }
97
98         public BuildAction(final StatementParserMode statementParserMode) {
99             this(statementParserMode, null);
100         }
101
102         public BuildAction(final Set<QName> supportedFeatures) {
103             this(StatementParserMode.DEFAULT_MODE, supportedFeatures);
104         }
105
106         public BuildAction(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures) {
107             this.context = new BuildGlobalContext(supportedTerminology, supportedValidation, statementParserMode,
108                     supportedFeatures);
109         }
110
111         /**
112          * Add main source. All main sources are present in resulting
113          * SchemaContext.
114          *
115          * @param source
116          *            which should be added into main sources
117          */
118         public void addSource(final StatementStreamSource source) {
119             context.addSource(source);
120         }
121
122         /**
123          * Add main sources. All main sources are present in resulting
124          * SchemaContext.
125          *
126          * @param sources
127          *            which should be added into main sources
128          */
129         public void addSources(final StatementStreamSource... sources) {
130             for (final StatementStreamSource source : sources) {
131                 context.addSource(source);
132             }
133         }
134
135         /**
136          * Add library sources. Only library sources required by main sources
137          * are present in resulting SchemaContext. Any other library sources are
138          * ignored and this also applies to error reporting.
139          *
140          * Library sources are not supported in semantic version mode currently.
141          *
142          * @param libSources
143          *            yang sources which should be added into library sources
144          */
145         public void addLibSources(final StatementStreamSource... libSources) {
146             for (final StatementStreamSource libSource : libSources) {
147                 context.addLibSource(libSource);
148             }
149         }
150
151         /**
152          * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException
153          * @throws ReactorException
154          */
155         public EffectiveModelContext build() throws ReactorException {
156             return context.build();
157         }
158
159         public EffectiveSchemaContext buildEffective() throws ReactorException {
160             return context.buildEffective();
161         }
162
163         public SchemaContext buildEffective(final Collection<ByteSource> yangByteSources) throws ReactorException,
164                 IOException {
165             for (final ByteSource source : yangByteSources) {
166                 if (source instanceof YangTextSchemaSource) {
167                     try {
168                         addSource(YangStatementStreamSource.create((YangTextSchemaSource) source));
169                     } catch (YangSyntaxErrorException e) {
170                         throw new IOException("Source " + source + " failed to parse", e);
171                     }
172                 } else {
173                     addSource(new YangStatementSourceImpl(source.openStream()));
174                 }
175             }
176
177             return buildEffective();
178         }
179
180         public SchemaContext buildEffective(final List<InputStream> yangInputStreams) throws ReactorException {
181             for (final InputStream yangInputStream : yangInputStreams) {
182                 addSource(new YangStatementSourceImpl(yangInputStream));
183             }
184
185             return buildEffective();
186         }
187     }
188 }