Bug 8307: Add the option for activating deviation statements
[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 com.google.common.base.Preconditions;
12 import java.io.File;
13 import java.io.FileFilter;
14 import java.io.FileNotFoundException;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
23 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
24 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
25 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
26 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
27 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
28
29 class SystemTestUtils {
30
31     static final FileFilter YANG_FILE_FILTER = new FileFilter() {
32         @Override
33         public boolean accept(final File file) {
34             final String name = file.getName().toLowerCase();
35             return name.endsWith(".yang") && file.isFile();
36         }
37     };
38
39     static SchemaContext parseYangSources(final List<String> yangLibDirs, final List<String> yangTestFiles,
40             final Set<QName> supportedFeatures, final boolean recursiveSearch) throws FileNotFoundException, ReactorException {
41         /*
42          * Current dir "." should be always present implicitly in the list of
43          * directories where dependencies are searched for
44          */
45         if (!yangLibDirs.contains(".")) {
46             yangLibDirs.add(".");
47         }
48
49         final List<File> libFiles = new ArrayList<>();
50         for (final String yangLibDir : yangLibDirs) {
51             libFiles.addAll(getYangFiles(yangLibDir, recursiveSearch));
52         }
53
54         final List<File> testFiles = new ArrayList<>();
55         for (final String yangTestFile : yangTestFiles) {
56             testFiles.add(new File(yangTestFile));
57         }
58
59         return parseYangSources(supportedFeatures, testFiles, libFiles);
60     }
61
62     static SchemaContext parseYangSources(final Set<QName> supportedFeatures, final List<File> testFiles,
63             final List<File> libFiles) throws FileNotFoundException, ReactorException {
64         final StatementStreamSource[] testSources = getYangStatementSources(testFiles);
65         final StatementStreamSource[] libSources = getYangStatementSources(libFiles);
66         return parseYangSources(testSources, libSources, supportedFeatures);
67     }
68
69     static SchemaContext parseYangSources(final StatementStreamSource[] testSources,
70             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
71         Preconditions.checkArgument(testSources != null && testSources.length > 0, "No yang sources");
72
73         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
74         reactor.addSources(testSources);
75         reactor.addLibSources(libSources);
76
77         if (supportedFeatures != null) {
78             reactor.setSupportedFeatures(supportedFeatures);
79         }
80
81         return reactor.buildEffective();
82     }
83
84     private static StatementStreamSource[] getYangStatementSources(final List<File> yangFiles)
85             throws FileNotFoundException {
86         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.size()];
87         for (int i = 0; i < yangFiles.size(); i++) {
88             yangSources[i] = new YangStatementSourceImpl(new NamedFileInputStream(yangFiles.get(i), yangFiles.get(i)
89                     .getPath()));
90         }
91         return yangSources;
92     }
93
94     private static Collection<File> getYangFiles(final String yangSourcesDirectoryPath, final boolean recursiveSearch)
95             throws FileNotFoundException {
96         final File testSourcesDir = new File(yangSourcesDirectoryPath);
97         if (testSourcesDir == null || !testSourcesDir.isDirectory()) {
98             throw new FileNotFoundException(String.format("%s no such directory", yangSourcesDirectoryPath));
99         }
100
101         return recursiveSearch ? searchYangFiles(testSourcesDir) : Arrays.asList(testSourcesDir.listFiles(YANG_FILE_FILTER));
102     }
103
104     private static List<File> searchYangFiles(final File dir) {
105         Preconditions.checkNotNull(dir);
106         Preconditions.checkArgument(dir.isDirectory(), "File %s is not a directory", dir.getPath());
107
108         final List<File> yangFiles = new ArrayList<>();
109         for (final File file : dir.listFiles()) {
110             if (file.isDirectory()) {
111                 yangFiles.addAll(searchYangFiles(file));
112             } else if (file.isFile() && file.getName().toLowerCase().endsWith(".yang")) {
113                 yangFiles.add(file);
114             }
115         }
116
117         return yangFiles;
118     }
119 }