Bug 7480 - yang-system-test: -p directories are for dependency search
[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.parser.spi.meta.ReactorException;
22 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
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 List<String> yangLibDirs, final List<String> yangTestFiles,
39             final Set<QName> supportedFeatures) throws FileNotFoundException, ReactorException {
40         /*
41          * Current dir "." should be always present implicitly in the list of
42          * directories where dependencies are searched for
43          */
44         if (!yangLibDirs.contains(".")) {
45             yangLibDirs.add(".");
46         }
47
48         final List<File> libFiles = new ArrayList<>();
49         for (final String yangLibDir : yangLibDirs) {
50             libFiles.addAll(getYangFiles(yangLibDir));
51         }
52
53         final List<File> testFiles = new ArrayList<>();
54         for (final String yangTestFile : yangTestFiles) {
55             testFiles.add(new File(yangTestFile));
56         }
57
58         return parseYangSources(supportedFeatures, testFiles, libFiles);
59     }
60
61     static SchemaContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles,
62             final List<File> libFiles) throws FileNotFoundException, ReactorException {
63         final StatementStreamSource[] testSources = getYangStatementSources(testFiles);
64         final StatementStreamSource[] libSources = getYangStatementSources(libFiles);
65         return parseYangSources(testSources, libSources, supportedFeatures);
66     }
67
68     static SchemaContext parseYangSources(final StatementStreamSource[] testSources,
69             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
70
71         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
72                 .newBuild(supportedFeatures);
73         reactor.addSources(testSources);
74         reactor.addLibSources(libSources);
75
76         return reactor.buildEffective();
77     }
78
79     private static StatementStreamSource[] getYangStatementSources(final List<File> yangFiles)
80             throws FileNotFoundException {
81         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.size()];
82         for (int i = 0; i < yangFiles.size(); i++) {
83             yangSources[i] = new YangStatementSourceImpl(new NamedFileInputStream(yangFiles.get(i), yangFiles.get(i)
84                     .getPath()));
85         }
86         return yangSources;
87     }
88
89     private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath) throws FileNotFoundException {
90         final File testSourcesDir = new File(yangSourcesDirectoryPath);
91         if (testSourcesDir == null || !testSourcesDir.isDirectory()) {
92             throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath));
93         }
94         return Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));
95     }
96 }