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.rfc7950.reactor.RFC7950Reactors;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.xml.sax.SAXException;
45 public final class StmtTestUtils {
47 public static final FileFilter YANG_FILE_FILTER =
48 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
50 public static final FileFilter YIN_FILE_FILTER =
51 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
53 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
55 private StmtTestUtils() {
59 public static void log(final Throwable exception, final String indent) {
60 LOG.debug(indent + exception.getMessage());
62 final Throwable[] suppressed = exception.getSuppressed();
63 for (final Throwable throwable : suppressed) {
64 log(throwable, indent + " ");
68 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
69 final List<Module> result = new ArrayList<>();
70 for (final Module module : modules) {
71 if (module.getName().equals(moduleName)) {
78 public static StatementStreamSource sourceForResource(final String resourceName) {
80 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
81 } catch (IOException | YangSyntaxErrorException e) {
82 throw new IllegalArgumentException("Failed to create source", e);
86 public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
87 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
88 final Set<Module> submodules = module.getSubmodules();
89 for (final Module submodule : submodules) {
90 printReferences(submodule, true, indent + " ");
91 printChilds(submodule.getChildNodes(), indent + " ");
95 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
97 for (final DataSchemaNode child : childNodes) {
98 LOG.debug(indent + "Child " + child.getQName().getLocalName());
99 if (child instanceof DataNodeContainer) {
100 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
105 public static SchemaContext parseYangSource(final String yangSourcePath) throws ReactorException,
106 URISyntaxException, IOException, YangSyntaxErrorException {
107 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
110 public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
111 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
112 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
115 public static SchemaContext parseYangSource(final String yangSourcePath,
116 final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
117 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
118 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
119 final File sourceFile = new File(source.toURI());
120 return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
123 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws ReactorException {
124 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
127 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
128 final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
129 return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
132 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
133 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
134 throws ReactorException {
135 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild(statementParserMode)
136 .addSources(sources);
137 if (supportedFeatures != null) {
138 reactor.setSupportedFeatures(supportedFeatures);
141 return reactor.buildEffective();
144 public static SchemaContext parseYangSources(final File... files) throws ReactorException, IOException,
145 YangSyntaxErrorException {
146 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
149 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
150 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
151 YangSyntaxErrorException {
153 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
154 for (File file : files) {
155 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
158 return parseYangSources(statementParserMode, supportedFeatures, sources);
161 public static SchemaContext parseYangSources(final Collection<File> files) throws ReactorException, IOException,
162 YangSyntaxErrorException {
163 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
166 public static SchemaContext parseYangSources(final Collection<File> files,
167 final StatementParserMode statementParserMode) throws ReactorException, IOException,
168 YangSyntaxErrorException {
169 return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
172 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws
173 ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
174 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
177 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
178 final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
179 YangSyntaxErrorException {
180 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
183 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
184 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
185 URISyntaxException, IOException, YangSyntaxErrorException {
187 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
188 final File testSourcesDir = new File(resourceDir.toURI());
190 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
193 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
194 final String yangLibsDirectoryPath)
195 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
196 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
199 public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
200 final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
201 ReactorException, IOException, YangSyntaxErrorException {
202 final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
203 final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
205 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
209 private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
210 final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
211 final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
212 for (int i = 0; i < yangFiles.length; i++) {
213 yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
216 final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
217 for (int i = 0; i < libFiles.length; i++) {
218 libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
221 return parseYangSources(yangSources, libSources, supportedFeatures);
224 private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
225 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
227 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
228 .addSources(yangSources).addLibSources(libSources);
229 if (supportedFeatures != null) {
230 reactor.setSupportedFeatures(supportedFeatures);
233 return reactor.buildEffective();
236 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath,
237 final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
239 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
240 final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
241 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
242 for (int i = 0; i < files.length; i++) {
243 final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
245 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
246 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
249 return parseYinSources(statementParserMode, sources);
252 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode,
253 final StatementStreamSource... sources) throws ReactorException {
254 return RFC7950Reactors.defaultReactor().newBuild(statementParserMode).addSources(sources).buildEffective();
257 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
258 final String importedModuleName) {
259 ModuleImport requestedModuleImport = null;
260 final Set<ModuleImport> rootImports = rootModule.getImports();
261 for (final ModuleImport moduleImport : rootImports) {
262 if (moduleImport.getModuleName().equals(importedModuleName)) {
263 requestedModuleImport = moduleImport;
268 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())