Bug 6868: If-feature argument may be boolean expression
[yangtools.git] / yang / yang-system-test / src / main / java / org / opendaylight / yangtools / yang / parser / system / test / SystemTestUtils.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.parser.system.test;
10
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.Collection;
17 import java.util.List;
18 import java.util.Set;
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
24 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
26 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
27
28 class SystemTestUtils {
29
30     static final FileFilter YANG_FILE_FILTER = new FileFilter() {
31         @Override
32         public boolean accept(final File file) {
33             final String name = file.getName().toLowerCase();
34             return name.endsWith(".yang") && file.isFile();
35         }
36     };
37
38     static SchemaContext parseYangSources(final Collection<File> files, final Set<QName> supportedFeatures)
39             throws ReactorException, FileNotFoundException {
40         return parseYangSources(files, StatementParserMode.DEFAULT_MODE, supportedFeatures);
41     }
42
43     static SchemaContext parseYangSources(final Collection<File> files,
44             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
45             throws ReactorException, FileNotFoundException {
46         return parseYangSources(supportedFeatures, statementParserMode, files.toArray(new File[files.size()]));
47     }
48
49     static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
50             final StatementParserMode statementParserMode, final File... files) throws ReactorException,
51             FileNotFoundException {
52         final YangStatementSourceImpl [] sources = new YangStatementSourceImpl[files.length];
53
54         for (int i = 0; i < files.length; i++) {
55             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
56         }
57
58         return parseYangSources(supportedFeatures, statementParserMode, sources);
59     }
60
61     static SchemaContext parseYangSources(final Set<QName> supportedFeatures,
62             final StatementParserMode statementParserMode, final YangStatementSourceImpl... sources)
63             throws ReactorException {
64         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
65                 statementParserMode, supportedFeatures);
66         reactor.addSources(sources);
67
68         return reactor.buildEffective();
69     }
70
71     static SchemaContext parseYangSources(final List<String> yangDirs, final List<String> yangFiles,
72             final Set<QName> supportedFeatures) throws FileNotFoundException, ReactorException {
73         final List<File> allYangFiles = new ArrayList<>();
74         for (final String yangDir : yangDirs) {
75             allYangFiles.addAll(getYangFiles(yangDir));
76         }
77
78         for (final String yangFile : yangFiles) {
79             allYangFiles.add(new File(yangFile));
80         }
81
82         return parseYangSources(allYangFiles, supportedFeatures);
83     }
84
85     private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath) {
86         final File testSourcesDir = new File(yangSourcesDirectoryPath);
87         return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));
88     }
89 }