2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.yangtools.yang.stmt;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
34 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
35 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
37 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
38 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
39 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class StmtTestUtils {
45 public static final FileFilter YANG_FILE_FILTER =
46 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
48 public static final FileFilter YIN_FILE_FILTER =
49 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
51 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
53 private StmtTestUtils() {
57 public static void log(final Throwable e, final String indent) {
58 LOG.debug(indent + e.getMessage());
60 final Throwable[] suppressed = e.getSuppressed();
61 for (final Throwable throwable : suppressed) {
62 log(throwable, indent + " ");
66 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
67 final List<Module> result = new ArrayList<>();
68 for (final Module module : modules) {
69 if (module.getName().equals(moduleName)) {
76 public static StatementStreamSource sourceForResource(final String resourceName) {
78 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
79 } catch (IOException | YangSyntaxErrorException e) {
80 throw new IllegalArgumentException("Failed to create source", e);
84 public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
85 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
86 final Set<Module> submodules = module.getSubmodules();
87 for (final Module submodule : submodules) {
88 printReferences(submodule, true, indent + " ");
89 printChilds(submodule.getChildNodes(), indent + " ");
93 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
95 for (final DataSchemaNode child : childNodes) {
96 LOG.debug(indent + "Child " + child.getQName().getLocalName());
97 if (child instanceof DataNodeContainer) {
98 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
103 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
105 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
108 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
109 final Set<QName> supportedFeatures, final StatementStreamSource... sources)
110 throws SourceException, ReactorException {
111 return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
114 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
115 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
116 throws SourceException, ReactorException {
117 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
118 statementParserMode, supportedFeatures);
119 reactor.addSources(sources);
121 return reactor.buildEffective();
124 public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
125 IOException, YangSyntaxErrorException {
126 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
129 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
130 final Set<QName> supportedFeatures, final File... files) throws SourceException,
131 ReactorException, IOException, YangSyntaxErrorException {
133 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
134 for (File file : files) {
135 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
138 return parseYangSources(statementParserMode, supportedFeatures, sources);
141 public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
142 IOException, YangSyntaxErrorException {
143 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
146 public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
147 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
148 return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
151 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
152 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
153 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
156 public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
157 URISyntaxException, IOException, YangSyntaxErrorException {
158 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
161 public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
162 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
163 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
166 public static SchemaContext parseYangSource(final String yangSourcePath,
167 final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
168 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
169 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
170 final File sourceFile = new File(source.toURI());
171 return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
174 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
175 final StatementParserMode statementParserMode) throws SourceException, ReactorException,
176 URISyntaxException, IOException, YangSyntaxErrorException {
177 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
180 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
181 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
182 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
184 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
185 final File testSourcesDir = new File(resourceDir.toURI());
187 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
190 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
191 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
193 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
194 final File testSourcesDir = new File(resourceDir.toURI());
196 return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
199 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files)
200 throws SourceException, ReactorException, FileNotFoundException {
202 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
204 for (int i = 0; i < files.length; i++) {
205 sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
208 return parseYinSources(statementParserMode, sources);
211 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
212 throws SourceException, ReactorException {
214 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
215 reactor.addSources(sources);
217 return reactor.buildEffective();
220 public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
221 ModuleImport requestedModuleImport = null;
222 final Set<ModuleImport> rootImports = rootModule.getImports();
223 for (final ModuleImport moduleImport : rootImports) {
224 if (moduleImport.getModuleName().equals(importedModuleName)) {
225 requestedModuleImport = moduleImport;
230 final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
231 requestedModuleImport.getRevision());
232 return importedModule;
235 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath, final String yangLibsDirectoryPath)
236 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
237 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
240 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
241 final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
242 ReactorException, IOException, YangSyntaxErrorException {
243 final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
244 final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
246 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
250 private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
251 final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
252 final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
253 for (int i = 0; i < yangFiles.length; i++) {
254 yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
257 final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
258 for (int i = 0; i < libFiles.length; i++) {
259 libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
262 return parseYangSources(yangSources, libSources, supportedFeatures);
265 private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
266 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
268 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
269 .newBuild(supportedFeatures);
270 reactor.addSources(yangSources);
271 reactor.addLibSources(libSources);
273 return reactor.buildEffective();