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.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
20 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
21 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
26 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
27 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
28 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
29 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
30 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
31 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
33 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class StmtTestUtils {
39 final public static FileFilter YANG_FILE_FILTER = file -> {
40 final String name = file.getName().toLowerCase();
41 return name.endsWith(".yang") && file.isFile();
44 final public static FileFilter YIN_FILE_FILTER = file -> {
45 final String name = file.getName().toLowerCase();
46 return name.endsWith(".xml") && file.isFile();
49 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
51 private StmtTestUtils() {
55 public static void log(final Throwable e, final String indent) {
56 LOG.debug(indent + e.getMessage());
58 final Throwable[] suppressed = e.getSuppressed();
59 for (final Throwable throwable : suppressed) {
60 log(throwable, indent + " ");
64 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
65 final List<Module> result = new ArrayList<>();
66 for (final Module module : modules) {
67 if (module.getName().equals(moduleName)) {
74 public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
75 for (final YangStatementSourceImpl source : sources) {
76 reactor.addSource(source);
80 public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
81 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
82 final Set<Module> submodules = module.getSubmodules();
83 for (final Module submodule : submodules) {
84 printReferences(submodule, true, indent + " ");
85 printChilds(submodule.getChildNodes(), indent + " ");
89 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
91 for (final DataSchemaNode child : childNodes) {
92 LOG.debug(indent + "Child " + child.getQName().getLocalName());
93 if (child instanceof DataNodeContainer) {
94 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
99 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
101 return parseYangSources(StatementParserMode.DEFAULT_MODE, sources);
104 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
105 throws SourceException, ReactorException {
107 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
108 reactor.addSources(sources);
110 return reactor.buildEffective();
113 public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
114 FileNotFoundException {
115 return parseYangSources(StatementParserMode.DEFAULT_MODE, files);
118 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
119 ReactorException, FileNotFoundException {
121 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
123 for (int i = 0; i < files.length; i++) {
124 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
127 return parseYangSources(statementParserMode, sources);
130 public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
131 FileNotFoundException {
132 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
135 public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
136 throws SourceException, ReactorException, FileNotFoundException {
137 return parseYangSources(statementParserMode, files.toArray(new File[files.size()]));
140 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
141 ReactorException, FileNotFoundException, URISyntaxException {
142 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
145 public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
146 FileNotFoundException, URISyntaxException {
147 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE);
150 public static SchemaContext parseYangSource(final String yangSourcePath, final StatementParserMode statementParserMode)
151 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
152 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
153 final File sourceFile = new File(source.toURI());
154 return parseYangSources(statementParserMode, sourceFile);
157 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath, final StatementParserMode statementParserMode)
158 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
160 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
161 final File testSourcesDir = new File(resourceDir.toURI());
163 return parseYangSources(statementParserMode, testSourcesDir.listFiles(YANG_FILE_FILTER));
166 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
167 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
169 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
170 final File testSourcesDir = new File(resourceDir.toURI());
172 return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
175 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
176 ReactorException, FileNotFoundException {
178 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
180 for (int i = 0; i < files.length; i++) {
181 sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
184 return parseYinSources(statementParserMode, sources);
187 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
188 throws SourceException, ReactorException {
190 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
191 reactor.addSources(sources);
193 return reactor.buildEffective();
196 public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
197 ModuleImport requestedModuleImport = null;
198 final Set<ModuleImport> rootImports = rootModule.getImports();
199 for (final ModuleImport moduleImport : rootImports) {
200 if (moduleImport.getModuleName().equals(importedModuleName)) {
201 requestedModuleImport = moduleImport;
206 final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
207 requestedModuleImport.getRevision());
208 return importedModule;