Bug 6868: If-feature argument may be boolean expression
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / StmtTestUtils.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.stmt;
10
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.util.ArrayList;
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.DataNodeContainer;
22 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
27 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
29 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
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.stmt.rfc6020.YinStatementSourceImpl;
34 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 public class StmtTestUtils {
39
40     final public static FileFilter YANG_FILE_FILTER = file -> {
41         final String name = file.getName().toLowerCase();
42         return name.endsWith(".yang") && file.isFile();
43     };
44
45     final public static FileFilter YIN_FILE_FILTER = file -> {
46         final String name = file.getName().toLowerCase();
47         return name.endsWith(".xml") && file.isFile();
48     };
49
50     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
51
52     private StmtTestUtils() {
53
54     }
55
56     public static void log(final Throwable e, final String indent) {
57         LOG.debug(indent + e.getMessage());
58
59         final Throwable[] suppressed = e.getSuppressed();
60         for (final Throwable throwable : suppressed) {
61             log(throwable, indent + "        ");
62         }
63     }
64
65     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
66         final List<Module> result = new ArrayList<>();
67         for (final Module module : modules) {
68             if (module.getName().equals(moduleName)) {
69                 result.add(module);
70             }
71         }
72         return result;
73     }
74
75     public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
76         for (final YangStatementSourceImpl source : sources) {
77             reactor.addSource(source);
78         }
79     }
80
81     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
82         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
83         final Set<Module> submodules = module.getSubmodules();
84         for (final Module submodule : submodules) {
85             printReferences(submodule, true, indent + "      ");
86             printChilds(submodule.getChildNodes(), indent + "            ");
87         }
88     }
89
90     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
91
92         for (final DataSchemaNode child : childNodes) {
93             LOG.debug(indent + "Child " + child.getQName().getLocalName());
94             if (child instanceof DataNodeContainer) {
95                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
96             }
97         }
98     }
99
100     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
101             ReactorException {
102         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
103     }
104
105     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures, final StatementStreamSource... sources)
106             throws SourceException, ReactorException {
107
108         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode, supportedFeatures);
109         reactor.addSources(sources);
110
111         return reactor.buildEffective();
112     }
113
114     public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
115             FileNotFoundException {
116         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
117     }
118
119     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures, final File... files) throws SourceException,
120             ReactorException, FileNotFoundException {
121
122         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
123
124         for (int i = 0; i < files.length; i++) {
125             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
126         }
127
128         return parseYangSources(statementParserMode, supportedFeatures, sources);
129     }
130
131     public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
132             FileNotFoundException {
133         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
134     }
135
136     public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
137             throws SourceException, ReactorException, FileNotFoundException {
138         return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
139     }
140
141     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
142             ReactorException, FileNotFoundException, URISyntaxException {
143         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
144     }
145
146     public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
147             FileNotFoundException, URISyntaxException {
148         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
149     }
150
151     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
152             throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
153         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
154     }
155
156     public static SchemaContext parseYangSource(final String yangSourcePath,
157             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
158             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
159         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
160         final File sourceFile = new File(source.toURI());
161         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
162     }
163
164     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
165             final StatementParserMode statementParserMode) throws SourceException, ReactorException,
166             FileNotFoundException, URISyntaxException {
167         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
168     }
169
170     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
171             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
172             ReactorException, FileNotFoundException, URISyntaxException {
173
174         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
175         final File testSourcesDir = new File(resourceDir.toURI());
176
177         return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
178     }
179
180     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
181             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
182
183         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
184         final File testSourcesDir = new File(resourceDir.toURI());
185
186         return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
187     }
188
189     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
190             ReactorException, FileNotFoundException {
191
192         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
193
194         for (int i = 0; i < files.length; i++) {
195             sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
196         }
197
198         return parseYinSources(statementParserMode, sources);
199     }
200
201     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
202             throws SourceException, ReactorException {
203
204         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
205         reactor.addSources(sources);
206
207         return reactor.buildEffective();
208     }
209
210     public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
211         ModuleImport requestedModuleImport = null;
212         final Set<ModuleImport> rootImports = rootModule.getImports();
213         for (final ModuleImport moduleImport : rootImports) {
214             if (moduleImport.getModuleName().equals(importedModuleName)) {
215                 requestedModuleImport = moduleImport;
216                 break;
217             }
218         }
219
220         final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
221                 requestedModuleImport.getRevision());
222         return importedModule;
223     }
224 }