Use YangConstants.RFC6020_YANG_FILE_EXTENSION
[yangtools.git] / yang / yang-test-util / src / main / java / org / opendaylight / yangtools / yang / test / util / YangParserTestUtils.java
1 /*
2  * Copyright (c) 2016 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
9 package org.opendaylight.yangtools.yang.test.util;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.io.File;
14 import java.io.FileFilter;
15 import java.io.FileNotFoundException;
16 import java.io.InputStream;
17 import java.net.URI;
18 import java.net.URISyntaxException;
19 import java.util.ArrayList;
20 import java.util.Arrays;
21 import java.util.Collection;
22 import java.util.List;
23 import java.util.Set;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.YangConstants;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
28 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
33 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
34
35 /**
36  * Utility class which provides convenience methods for producing effective schema context based on the supplied
37  * yang/yin sources or paths to these sources
38  */
39 @Beta
40 public final class YangParserTestUtils {
41
42     private static final FileFilter YANG_FILE_FILTER = file -> {
43         final String name = file.getName().toLowerCase();
44         return name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
45     };
46
47     private YangParserTestUtils() {
48         throw new UnsupportedOperationException("Utility class should not be instantiated.");
49     }
50
51     /**
52      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
53      * default mode and all YANG features are supported.
54      *
55      * @param sources YANG sources to be parsed
56      *
57      * @return effective schema context
58      *
59      * @throws ReactorException if there is an error in one of the parsed YANG sources
60      *
61      * @deprecated Migration method only, do not use.
62      */
63     @Deprecated
64     public static SchemaContext parseYangSources(final YangStatementSourceImpl... sources) throws ReactorException {
65         return parseYangSources(StatementParserMode.DEFAULT_MODE, sources);
66     }
67
68     /**
69      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
70      * default mode.
71      *
72      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
73      *                          models are resolved
74      * @param sources YANG sources to be parsed
75      *
76      * @return effective schema context
77      * @throws ReactorException if there is an error in one of the parsed YANG sources
78      *
79      * @deprecated Migration method only, do not use.
80      */
81     @Deprecated
82     public static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
83             final YangStatementSourceImpl... sources) throws ReactorException {
84         return parseYangSources(supportedFeatures, StatementParserMode.DEFAULT_MODE, sources);
85     }
86
87     /**
88      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
89      *
90      * @param statementParserMode mode of statement parser
91      * @param sources YANG sources to be parsed
92      *
93      * @return effective schema context
94      * @throws ReactorException if there is an error in one of the parsed YANG sources
95      *
96      * @deprecated Migration method only, do not use.
97      */
98     @Deprecated
99     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
100             final YangStatementSourceImpl... sources) throws ReactorException {
101         return parseYangSources(null, statementParserMode, sources);
102     }
103
104     /**
105      * Creates a new effective schema context containing the specified YANG sources.
106      *
107      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
108      *                          models are resolved
109      * @param statementParserMode mode of statement parser
110      * @param sources YANG sources to be parsed
111      *
112      * @return effective schema context
113      *
114      * @throws ReactorException if there is an error in one of the parsed YANG sources
115      */
116     public static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
117             final StatementParserMode statementParserMode, final YangStatementSourceImpl... sources)
118                     throws ReactorException {
119         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
120                 statementParserMode, supportedFeatures);
121         reactor.addSources(sources);
122
123         return reactor.buildEffective();
124     }
125
126     /**
127      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
128      * default mode and all YANG features are supported.
129      *
130      * @param files YANG files to be parsed
131      *
132      * @return effective schema context
133      *
134      * @throws ReactorException if there is an error in one of the parsed YANG sources
135      * @throws FileNotFoundException if one of the specified files does not exist
136      */
137     public static SchemaContext parseYangSources(final File... files) throws ReactorException, FileNotFoundException {
138         return parseYangSources(StatementParserMode.DEFAULT_MODE, files);
139     }
140
141     /**
142      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
143      * default mode.
144      *
145      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
146      *                          models are resolved
147      * @param files YANG files to be parsed
148      *
149      * @return effective schema context
150      *
151      * @throws ReactorException if there is an error in one of the parsed YANG sources
152      * @throws FileNotFoundException if one of the specified files does not exist
153      */
154     public static SchemaContext parseYangSources(final Set<QName> supportedFeatures, final File... files)
155             throws ReactorException, FileNotFoundException {
156         return parseYangSources(supportedFeatures, StatementParserMode.DEFAULT_MODE, files);
157     }
158
159     /**
160      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
161      *
162      * @param statementParserMode mode of statement parser
163      * @param files YANG files to be parsed
164      *
165      * @return effective schema context
166      *
167      * @throws ReactorException if there is an error in one of the parsed YANG sources
168      * @throws FileNotFoundException if one of the specified files does not exist
169      */
170     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final File... files)
171             throws ReactorException, FileNotFoundException {
172         return parseYangSources(null, statementParserMode, files);
173     }
174
175     /**
176      * Creates a new effective schema context containing the specified YANG sources.
177      *
178      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
179      *                          models are resolved
180      * @param statementParserMode mode of statement parser
181      * @param files YANG files to be parsed
182      *
183      * @return effective schema context
184      *
185      * @throws ReactorException if there is an error in one of the parsed YANG sources
186      * @throws FileNotFoundException if one of the specified files does not exist
187      */
188     public static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
189             final StatementParserMode statementParserMode, final File... files) throws ReactorException,
190             FileNotFoundException {
191         final YangStatementSourceImpl[] sources = new YangStatementSourceImpl[files.length];
192         for (int i = 0; i < files.length; i++) {
193             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
194         }
195
196         return parseYangSources(supportedFeatures, statementParserMode, sources);
197     }
198
199     /**
200      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
201      * default mode and all YANG features are supported.
202      *
203      * @param files collection of YANG files to be parsed
204      *
205      * @return effective schema context
206      *
207      * @throws ReactorException if there is an error in one of the parsed YANG sources
208      * @throws FileNotFoundException if one of the specified files does not exist
209      */
210     public static SchemaContext parseYangSources(final Collection<File> files) throws ReactorException,
211             FileNotFoundException {
212         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
213     }
214
215     /**
216      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
217      * default mode.
218      *
219      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
220      *                          models are resolved
221      * @param files collection of YANG files to be parsed
222      *
223      * @return effective schema context
224      *
225      * @throws ReactorException if there is an error in one of the parsed YANG sources
226      * @throws FileNotFoundException if one of the specified files does not exist
227      */
228     public static SchemaContext parseYangSources(final Collection<File> files, final Set<QName> supportedFeatures)
229             throws ReactorException, FileNotFoundException {
230         return parseYangSources(files, supportedFeatures, StatementParserMode.DEFAULT_MODE);
231     }
232
233     /**
234      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
235      *
236      * @param statementParserMode mode of statement parser
237      * @param files collection of YANG files to be parsed
238      *
239      * @return effective schema context
240      *
241      * @throws ReactorException if there is an error in one of the parsed YANG sources
242      * @throws FileNotFoundException if one of the specified files does not exist
243      */
244     public static SchemaContext parseYangSources(final Collection<File> files,
245             final StatementParserMode statementParserMode) throws ReactorException, FileNotFoundException {
246         return parseYangSources(files, null, statementParserMode);
247     }
248
249     /**
250      * Creates a new effective schema context containing the specified YANG sources.
251      *
252      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
253      *                          models are resolved
254      * @param statementParserMode mode of statement parser
255      * @param files collection of YANG files to be parsed
256      *
257      * @return effective schema context
258      *
259      * @throws ReactorException if there is an error in one of the parsed YANG sources
260      * @throws FileNotFoundException if one of the specified files does not exist
261      */
262     public static SchemaContext parseYangSources(final Collection<File> files, final Set<QName> supportedFeatures,
263             final StatementParserMode statementParserMode) throws ReactorException, FileNotFoundException {
264         return parseYangSources(supportedFeatures, statementParserMode, files.toArray(new File[files.size()]));
265     }
266
267     /**
268      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
269      * default mode and all YANG features are supported.
270      *
271      * @param yangSourcesDirectoryPath relative path to the directory with YANG files to be parsed
272      *
273      * @return effective schema context
274      *
275      * @throws ReactorException if there is an error in one of the parsed YANG sources
276      * @throws FileNotFoundException if the specified directory does not exist
277      * @throws URISyntaxException if the specified directory does not exist
278      */
279     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws ReactorException,
280             FileNotFoundException, URISyntaxException {
281         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
282     }
283
284     /**
285      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
286      * default mode.
287      *
288      * @param yangSourcesDirectoryPath relative path to the directory with YANG files to be parsed
289      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
290      *                          models are resolved
291      *
292      * @return effective schema context
293      *
294      * @throws ReactorException if there is an error in one of the parsed YANG sources
295      * @throws FileNotFoundException if the specified directory does not exist
296      * @throws URISyntaxException if the specified directory does not exist
297      */
298     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
299             final Set<QName> supportedFeatures) throws ReactorException, FileNotFoundException,
300             URISyntaxException {
301         return parseYangSources(yangSourcesDirectoryPath, supportedFeatures, StatementParserMode.DEFAULT_MODE);
302     }
303
304     /**
305      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
306      *
307      * @param yangSourcesDirectoryPath relative path to the directory with YANG files to be parsed
308      * @param statementParserMode mode of statement parser
309      *
310      * @return effective schema context
311      *
312      * @throws ReactorException if there is an error in one of the parsed YANG sources
313      * @throws FileNotFoundException if the specified directory does not exist
314      * @throws URISyntaxException if the specified directory does not exist
315      */
316     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
317             final StatementParserMode statementParserMode) throws ReactorException, FileNotFoundException,
318             URISyntaxException {
319         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
320     }
321
322     /**
323      * Creates a new effective schema context containing the specified YANG sources.
324      *
325      * @param yangSourcesDirectoryPath relative path to the directory with YANG files to be parsed
326      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
327      *                          models are resolved
328      * @param statementParserMode mode of statement parser
329      *
330      * @return effective schema context
331      *
332      * @throws ReactorException if there is an error in one of the parsed YANG sources
333      * @throws FileNotFoundException if the specified directory does not exist
334      * @throws URISyntaxException if the specified directory does not exist
335      */
336     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
337             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode)
338                     throws ReactorException, FileNotFoundException, URISyntaxException {
339         final URI directoryPath = YangParserTestUtils.class.getResource(yangSourcesDirectoryPath).toURI();
340         final File dir = new File(directoryPath);
341
342         return parseYangSources(supportedFeatures, statementParserMode, dir.listFiles(YANG_FILE_FILTER));
343     }
344
345     /**
346      * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
347      * default mode and all YANG features are supported.
348      *
349      * @param yangSourcePath relative path to the YANG file to be parsed
350      *
351      * @return effective schema context
352      *
353      * @throws ReactorException if there is an error in the parsed YANG source
354      * @throws FileNotFoundException if the specified file does not exist
355      * @throws URISyntaxException if the specified file does not exist
356      */
357     public static SchemaContext parseYangSource(final String yangSourcePath) throws ReactorException,
358             FileNotFoundException, URISyntaxException {
359         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE);
360     }
361
362     /**
363      * Creates a new effective schema context containing the specified YANG source. Statement parser mode is set to
364      * default mode.
365      *
366      * @param yangSourcePath relative path to the YANG file to be parsed
367      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
368      *                          model are resolved
369      *
370      * @return effective schema context
371      *
372      * @throws ReactorException if there is an error in the parsed YANG source
373      * @throws FileNotFoundException if the specified file does not exist
374      * @throws URISyntaxException if the specified file does not exist
375      */
376     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
377             throws ReactorException, FileNotFoundException, URISyntaxException {
378         return parseYangSource(yangSourcePath, supportedFeatures, StatementParserMode.DEFAULT_MODE);
379     }
380
381     /**
382      * Creates a new effective schema context containing the specified YANG source. All YANG features are supported.
383      *
384      * @param yangSourcePath relative path to the YANG file to be parsed
385      * @param statementParserMode mode of statement parser
386      *
387      * @return effective schema context
388      *
389      * @throws ReactorException if there is an error in the parsed YANG source
390      * @throws FileNotFoundException if the specified file does not exist
391      * @throws URISyntaxException if the specified file does not exist
392      */
393     public static SchemaContext parseYangSource(final String yangSourcePath,
394             final StatementParserMode statementParserMode) throws ReactorException, FileNotFoundException,
395             URISyntaxException {
396         return parseYangSource(yangSourcePath, null, statementParserMode);
397     }
398
399     /**
400      * Creates a new effective schema context containing the specified YANG source.
401      *
402      * @param yangSourcePath relative path to the YANG file to be parsed
403      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
404      *                          model are resolved
405      * @param statementParserMode mode of statement parser
406      *
407      * @return effective schema context
408      *
409      * @throws ReactorException if there is an error in the parsed YANG source
410      * @throws FileNotFoundException if the specified file does not exist
411      * @throws URISyntaxException if the specified file does not exist
412      */
413     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures,
414             final StatementParserMode statementParserMode) throws ReactorException, FileNotFoundException,
415             URISyntaxException {
416         final URI sourcePath = YangParserTestUtils.class.getResource(yangSourcePath).toURI();
417         final File sourceFile = new File(sourcePath);
418         return parseYangSources(supportedFeatures, statementParserMode, sourceFile);
419     }
420
421     /**
422      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
423      * default mode and all YANG features are supported.
424      *
425      * @param yangDirs relative paths to the directories containing YANG files to be parsed
426      * @param yangFiles relative paths to the YANG files to be parsed
427      *
428      * @return effective schema context
429      *
430      * @throws ReactorException if there is an error in one of the parsed YANG sources
431      * @throws FileNotFoundException if one of the specified directories or files does not exist
432      * @throws URISyntaxException if one of the specified directories or files does not exist
433      */
434     public static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles)
435             throws FileNotFoundException, ReactorException, URISyntaxException {
436         return parseYangSources(yangDirs, yangFiles, StatementParserMode.DEFAULT_MODE);
437     }
438
439     /**
440      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
441      * default mode.
442      *
443      * @param yangDirs relative paths to the directories containing YANG files to be parsed
444      * @param yangFiles relative paths to the YANG files to be parsed
445      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
446      *                          models are resolved
447      *
448      * @return effective schema context
449      *
450      * @throws ReactorException if there is an error in one of the parsed YANG sources
451      * @throws FileNotFoundException if one of the specified directories or files does not exist
452      * @throws URISyntaxException if one of the specified directories or files does not exist
453      */
454     public static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles,
455             final Set<QName> supportedFeatures) throws FileNotFoundException, ReactorException,
456             URISyntaxException {
457         return parseYangSources(yangDirs, yangFiles, supportedFeatures, StatementParserMode.DEFAULT_MODE);
458     }
459
460     /**
461      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
462      *
463      * @param yangDirs relative paths to the directories containing YANG files to be parsed
464      * @param yangFiles relative paths to the YANG files to be parsed
465      * @param statementParserMode mode of statement parser
466      *
467      * @return effective schema context
468      *
469      * @throws ReactorException if there is an error in one of the parsed YANG sources
470      * @throws FileNotFoundException if one of the specified directories or files does not exist
471      * @throws URISyntaxException if one of the specified directories or files does not exist
472      */
473     public static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles,
474             final StatementParserMode statementParserMode) throws FileNotFoundException, ReactorException,
475             URISyntaxException {
476         return parseYangSources(yangDirs, yangFiles, null, statementParserMode);
477     }
478
479     /**
480      * Creates a new effective schema context containing the specified YANG sources.
481      *
482      * @param yangDirs relative paths to the directories containing YANG files to be parsed
483      * @param yangFiles relative paths to the YANG files to be parsed
484      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
485      *                          models are resolved
486      * @param statementParserMode mode of statement parser
487      *
488      * @return effective schema context
489      *
490      * @throws ReactorException if there is an error in one of the parsed YANG sources
491      * @throws FileNotFoundException if one of the specified directories or files does not exist
492      * @throws URISyntaxException if one of the specified directories or files does not exist
493      */
494     public static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles,
495             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode)
496             throws FileNotFoundException, ReactorException, URISyntaxException {
497         final List<File> allYangFiles = new ArrayList<>();
498         for (final String yangDir : yangDirs) {
499             allYangFiles.addAll(getYangFiles(yangDir));
500         }
501
502         for (final String yangFile : yangFiles) {
503             final URI filePath = YangParserTestUtils.class.getResource(yangFile).toURI();
504             allYangFiles.add(new File(filePath));
505         }
506
507         return parseYangSources(allYangFiles, supportedFeatures, statementParserMode);
508     }
509
510     private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath) throws URISyntaxException {
511         final URI directoryPath = YangParserTestUtils.class.getResource(yangSourcesDirectoryPath).toURI();
512         final File dir = new File(directoryPath);
513
514         return Arrays.asList(dir.listFiles(YANG_FILE_FILTER));
515     }
516
517     /**
518      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
519      * default mode and all YANG features are supported.
520      *
521      * @param filePaths relative paths to the YANG files to be parsed
522      *
523      * @return effective schema context
524      *
525      * @throws ReactorException if there is an error in one of the parsed YANG sources
526      */
527     public static SchemaContext parseYangSources(final List<String> filePaths) throws ReactorException {
528         return parseYangSources(filePaths, StatementParserMode.DEFAULT_MODE);
529     }
530
531     /**
532      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
533      * default mode.
534      *
535      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
536      *                          models are resolved
537      * @param filePaths relative paths to the YANG files to be parsed
538      *
539      * @return effective schema context
540      *
541      * @throws ReactorException if there is an error in one of the parsed YANG sources
542      */
543     public static SchemaContext parseYangSources(final List<String> filePaths, final Set<QName> supportedFeatures)
544             throws ReactorException {
545         return parseYangSources(filePaths, supportedFeatures, StatementParserMode.DEFAULT_MODE);
546     }
547
548     /**
549      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
550      *
551      * @param statementParserMode mode of statement parser
552      * @param filePaths relative paths to the YANG files to be parsed
553      *
554      * @return effective schema context
555      *
556      * @throws ReactorException if there is an error in one of the parsed YANG sources
557      */
558     public static SchemaContext parseYangSources(final List<String> filePaths,
559             final StatementParserMode statementParserMode) throws ReactorException {
560         return parseYangSources(filePaths, (Set<QName>) null, statementParserMode);
561     }
562
563     /**
564      * Creates a new effective schema context containing the specified YANG sources.
565      *
566      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
567      *                          models are resolved
568      * @param statementParserMode mode of statement parser
569      * @param filePaths relative paths to the YANG files to be parsed
570      *
571      * @return effective schema context
572      *
573      * @throws ReactorException if there is an error in one of the parsed YANG sources
574      */
575     public static SchemaContext parseYangSources(final List<String> filePaths,
576             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode)
577                     throws ReactorException {
578         final YangStatementSourceImpl[] sources = new YangStatementSourceImpl[filePaths.size()];
579
580         for (int i = 0; i < filePaths.size(); i++) {
581             sources[i] = new YangStatementSourceImpl(YangParserTestUtils.class.getResourceAsStream(filePaths.get(i)));
582         }
583
584         return parseYangSources(supportedFeatures, statementParserMode, sources);
585     }
586
587     /**
588      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
589      * default mode and all YANG features are supported.
590      *
591      * @param streams input streams containing YANG sources to be parsed
592      *
593      * @return effective schema context
594      *
595      * @throws ReactorException if there is an error in one of the parsed YANG sources
596      */
597     public static SchemaContext parseYangStreams(final List<InputStream> streams) throws ReactorException {
598         return parseYangStreams(streams, StatementParserMode.DEFAULT_MODE);
599     }
600
601     /**
602      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
603      * default mode.
604      *
605      * @param streams input streams containing YANG sources to be parsed
606      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
607      *                          models are resolved
608      *
609      * @return effective schema context
610      *
611      * @throws ReactorException if there is an error in one of the parsed YANG sources
612      */
613     public static SchemaContext parseYangStreams(final List<InputStream> streams, final Set<QName> supportedFeatures)
614             throws ReactorException {
615         return parseYangStreams(streams, supportedFeatures, StatementParserMode.DEFAULT_MODE);
616     }
617
618     /**
619      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
620      *
621      * @param streams input streams containing YANG sources to be parsed
622      * @param statementParserMode mode of statement parser
623      *
624      * @return effective schema context
625      *
626      * @throws ReactorException if there is an error in one of the parsed YANG sources
627      */
628     public static SchemaContext parseYangStreams(final List<InputStream> streams,
629             final StatementParserMode statementParserMode) throws ReactorException {
630         return parseYangStreams(streams, null, statementParserMode);
631     }
632
633     /**
634      * Creates a new effective schema context containing the specified YANG sources.
635      *
636      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
637      *                          models are resolved
638      * @param statementParserMode mode of statement parser
639      * @param streams input streams containing YANG sources to be parsed
640      *
641      * @return effective schema context
642      *
643      * @throws ReactorException if there is an error in one of the parsed YANG sources
644      */
645     public static SchemaContext parseYangStreams(final List<InputStream> streams, final Set<QName> supportedFeatures,
646             final StatementParserMode statementParserMode) throws ReactorException {
647         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
648                 statementParserMode, supportedFeatures);
649         return reactor.buildEffective(streams);
650     }
651
652     /**
653      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
654      * default mode and all YANG features are supported.
655      *
656      * @param clazz Resource lookup base
657      * @param resources Resource names to be looked up
658      *
659      * @return effective schema context
660      *
661      * @throws ReactorException if there is an error in one of the parsed YANG sources
662      */
663     public static SchemaContext parseYangResources(final Class<?> clazz, final String... resources)
664             throws ReactorException {
665         final List<InputStream> streams = new ArrayList<>(resources.length);
666         for (final String r : resources) {
667             final InputStream is = clazz.getResourceAsStream(r);
668             Preconditions.checkArgument(is != null, "Resource %s not found", r);
669             streams.add(is);
670         }
671
672         return parseYangStreams(streams);
673     }
674
675     /**
676      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
677      * default mode and all YANG features are supported.
678      *
679      * @param streams input streams containing YANG sources to be parsed
680      *
681      * @return effective schema context
682      *
683      * @throws ReactorException if there is an error in one of the parsed YANG sources
684      */
685     public static SchemaContext parseYangStreams(final InputStream... streams) throws ReactorException {
686         return parseYangStreams(Arrays.asList(streams));
687     }
688
689     /**
690      * Creates a new effective schema context containing the specified YANG sources. Statement parser mode is set to
691      * default mode.
692      *
693      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
694      *                          models are resolved
695      * @param streams input streams containing YANG sources to be parsed
696      *
697      * @return effective schema context
698      *
699      * @throws ReactorException if there is an error in one of the parsed YANG sources
700      */
701     public static SchemaContext parseYangStreams(final Set<QName> supportedFeatures,
702             final InputStream... streams) throws ReactorException {
703         return parseYangStreams(Arrays.asList(streams), supportedFeatures);
704     }
705
706     /**
707      * Creates a new effective schema context containing the specified YANG sources. All YANG features are supported.
708      *
709      * @param statementParserMode mode of statement parser
710      * @param streams input streams containing YANG sources to be parsed
711      *
712      * @return effective schema context
713      *
714      * @throws ReactorException if there is an error in one of the parsed YANG sources
715      */
716     public static SchemaContext parseYangStreams(final StatementParserMode statementParserMode,
717             final InputStream... streams) throws ReactorException {
718         return parseYangStreams(Arrays.asList(streams), statementParserMode);
719     }
720
721     /**
722      * Creates a new effective schema context containing the specified YANG sources.
723      *
724      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YANG
725      *                          models are resolved
726      * @param statementParserMode mode of statement parser
727      * @param streams input streams containing YANG sources to be parsed
728      *
729      * @return effective schema context
730      *
731      * @throws ReactorException if there is an error in one of the parsed YANG sources
732      */
733     public static SchemaContext parseYangStreams(final Set<QName> supportedFeatures,
734             final StatementParserMode statementParserMode, final InputStream... streams) throws ReactorException {
735         return parseYangStreams(Arrays.asList(streams), supportedFeatures, statementParserMode);
736     }
737
738     /**
739      * Creates a new effective schema context containing the specified YIN sources. Statement parser mode is set to
740      * default mode and all YANG features are supported.
741      *
742      * @param sources YIN sources to be parsed
743      *
744      * @return effective schema context
745      *
746      * @throws ReactorException if there is an error in one of the parsed YIN sources
747      */
748     public static SchemaContext parseYinSources(final YinStatementStreamSource... sources) throws ReactorException {
749         return parseYinSources(StatementParserMode.DEFAULT_MODE, sources);
750     }
751
752     /**
753      * Creates a new effective schema context containing the specified YIN sources. Statement parser mode is set to
754      * default mode.
755      *
756      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YIN
757      *                          models are resolved
758      * @param sources YIN sources to be parsed
759      *
760      * @return effective schema context
761      * @throws ReactorException if there is an error in one of the parsed YIN sources
762      */
763     public static SchemaContext parseYinSources(final Set<QName> supportedFeatures,
764             final YinStatementStreamSource... sources) throws ReactorException {
765         return parseYinSources(supportedFeatures, StatementParserMode.DEFAULT_MODE, sources);
766     }
767
768     /**
769      * Creates a new effective schema context containing the specified YIN sources. All YANG features are supported.
770      *
771      * @param statementParserMode mode of statement parser
772      * @param sources YIN sources to be parsed
773      *
774      * @return effective schema context
775      *
776      * @throws ReactorException if there is an error in one of the parsed YIN sources
777      */
778     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode,
779             final YinStatementStreamSource... sources) throws ReactorException {
780         return parseYinSources(null, statementParserMode, sources);
781     }
782
783     /**
784      * Creates a new effective schema context containing the specified YIN sources.
785      *
786      * @param supportedFeatures set of supported features based on which all if-feature statements in the parsed YIN
787      *                          models are resolved
788      * @param statementParserMode mode of statement parser
789      * @param sources YIN sources to be parsed
790      *
791      * @return effective schema context
792      *
793      * @throws ReactorException if there is an error in one of the parsed YIN sources
794      */
795     public static SchemaContext parseYinSources(final Set<QName> supportedFeatures,
796             final StatementParserMode statementParserMode, final YinStatementStreamSource... sources)
797                     throws ReactorException {
798         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
799                 statementParserMode, supportedFeatures);
800         reactor.addSources(sources);
801
802         return reactor.buildEffective();
803     }
804 }