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