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