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.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;
38 public class StmtTestUtils {
40 final public static FileFilter YANG_FILE_FILTER = file -> {
41 final String name = file.getName().toLowerCase();
42 return name.endsWith(".yang") && file.isFile();
45 final public static FileFilter YIN_FILE_FILTER = file -> {
46 final String name = file.getName().toLowerCase();
47 return name.endsWith(".xml") && file.isFile();
50 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
52 private StmtTestUtils() {
56 public static void log(final Throwable e, final String indent) {
57 LOG.debug(indent + e.getMessage());
59 final Throwable[] suppressed = e.getSuppressed();
60 for (final Throwable throwable : suppressed) {
61 log(throwable, indent + " ");
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)) {
75 public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
76 for (final YangStatementSourceImpl source : sources) {
77 reactor.addSource(source);
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 + " ");
90 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
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 + " ");
100 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
102 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
105 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures, final StatementStreamSource... sources)
106 throws SourceException, ReactorException {
108 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode, supportedFeatures);
109 reactor.addSources(sources);
111 return reactor.buildEffective();
114 public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
115 FileNotFoundException {
116 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
119 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Set<QName> supportedFeatures, final File... files) throws SourceException,
120 ReactorException, FileNotFoundException {
122 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
124 for (int i = 0; i < files.length; i++) {
125 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
128 return parseYangSources(statementParserMode, supportedFeatures, sources);
131 public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
132 FileNotFoundException {
133 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
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()]));
141 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
142 ReactorException, FileNotFoundException, URISyntaxException {
143 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
146 public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
147 FileNotFoundException, URISyntaxException {
148 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
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);
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);
164 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
165 final StatementParserMode statementParserMode) throws SourceException, ReactorException,
166 FileNotFoundException, URISyntaxException {
167 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
170 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
171 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
172 ReactorException, FileNotFoundException, URISyntaxException {
174 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
175 final File testSourcesDir = new File(resourceDir.toURI());
177 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
180 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
181 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
183 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
184 final File testSourcesDir = new File(resourceDir.toURI());
186 return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
189 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
190 ReactorException, FileNotFoundException {
192 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
194 for (int i = 0; i < files.length; i++) {
195 sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
198 return parseYinSources(statementParserMode, sources);
201 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
202 throws SourceException, ReactorException {
204 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
205 reactor.addSources(sources);
207 return reactor.buildEffective();
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;
220 final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
221 requestedModuleImport.getRevision());
222 return importedModule;