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