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;
11 import com.google.common.io.Files;
13 import java.io.FileFilter;
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.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
34 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinTextToDomTransformer;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.xml.sax.SAXException;
46 public class StmtTestUtils {
48 public static final FileFilter YANG_FILE_FILTER =
49 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
51 public static final FileFilter YIN_FILE_FILTER =
52 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
54 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
56 private StmtTestUtils() {
60 public static void log(final Throwable e, final String indent) {
61 LOG.debug(indent + e.getMessage());
63 final Throwable[] suppressed = e.getSuppressed();
64 for (final Throwable throwable : suppressed) {
65 log(throwable, indent + " ");
69 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
70 final List<Module> result = new ArrayList<>();
71 for (final Module module : modules) {
72 if (module.getName().equals(moduleName)) {
79 public static StatementStreamSource sourceForResource(final String resourceName) {
81 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
82 } catch (IOException | YangSyntaxErrorException e) {
83 throw new IllegalArgumentException("Failed to create source", e);
87 public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
88 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
89 final Set<Module> submodules = module.getSubmodules();
90 for (final Module submodule : submodules) {
91 printReferences(submodule, true, indent + " ");
92 printChilds(submodule.getChildNodes(), indent + " ");
96 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
98 for (final DataSchemaNode child : childNodes) {
99 LOG.debug(indent + "Child " + child.getQName().getLocalName());
100 if (child instanceof DataNodeContainer) {
101 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
106 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
108 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
111 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
112 final Set<QName> supportedFeatures, final StatementStreamSource... sources)
113 throws SourceException, ReactorException {
114 return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
117 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
118 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
119 throws SourceException, ReactorException {
120 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
121 statementParserMode);
122 reactor.addSources(sources);
123 if (supportedFeatures != null) {
124 reactor.setSupportedFeatures(supportedFeatures);
127 return reactor.buildEffective();
130 public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
131 IOException, YangSyntaxErrorException {
132 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
135 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
136 final Set<QName> supportedFeatures, final File... files) throws SourceException,
137 ReactorException, IOException, YangSyntaxErrorException {
139 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
140 for (File file : files) {
141 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
144 return parseYangSources(statementParserMode, supportedFeatures, sources);
147 public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
148 IOException, YangSyntaxErrorException {
149 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
152 public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
153 throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
154 return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
157 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
158 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
159 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
162 public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
163 URISyntaxException, IOException, YangSyntaxErrorException {
164 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
167 public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
168 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
169 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
172 public static SchemaContext parseYangSource(final String yangSourcePath,
173 final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
174 throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
175 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
176 final File sourceFile = new File(source.toURI());
177 return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
180 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
181 final StatementParserMode statementParserMode) throws SourceException, ReactorException,
182 URISyntaxException, IOException, YangSyntaxErrorException {
183 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
186 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
187 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
188 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
190 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
191 final File testSourcesDir = new File(resourceDir.toURI());
193 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
196 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
197 throws URISyntaxException, SAXException, IOException, ReactorException {
198 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
199 final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
200 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
201 for (int i = 0; i < files.length; i++) {
202 final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
204 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
205 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
208 return parseYinSources(statementParserMode, sources);
211 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode,
212 final StatementStreamSource... sources)
213 throws ReactorException {
215 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
216 .newBuild(statementParserMode);
217 reactor.addSources(sources);
219 return reactor.buildEffective();
222 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
223 final String importedModuleName) {
224 ModuleImport requestedModuleImport = null;
225 final Set<ModuleImport> rootImports = rootModule.getImports();
226 for (final ModuleImport moduleImport : rootImports) {
227 if (moduleImport.getModuleName().equals(importedModuleName)) {
228 requestedModuleImport = moduleImport;
233 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
237 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
238 final String yangLibsDirectoryPath)
239 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
240 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
243 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
244 final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
245 ReactorException, IOException, YangSyntaxErrorException {
246 final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
247 final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
249 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
253 private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
254 final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
255 final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
256 for (int i = 0; i < yangFiles.length; i++) {
257 yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
260 final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
261 for (int i = 0; i < libFiles.length; i++) {
262 libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
265 return parseYangSources(yangSources, libSources, supportedFeatures);
268 private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
269 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
271 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
272 reactor.addSources(yangSources);
273 reactor.addLibSources(libSources);
274 if (supportedFeatures != null) {
275 reactor.setSupportedFeatures(supportedFeatures);
278 return reactor.buildEffective();