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 java.util.function.Predicate;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
23 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.repo.api.IfFeaturePredicates;
28 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
29 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
30 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
31 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
34 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YinStatementSourceImpl;
36 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class StmtTestUtils {
42 final public static FileFilter YANG_FILE_FILTER = file -> {
43 final String name = file.getName().toLowerCase();
44 return name.endsWith(".yang") && file.isFile();
47 final public static FileFilter YIN_FILE_FILTER = file -> {
48 final String name = file.getName().toLowerCase();
49 return name.endsWith(".xml") && file.isFile();
52 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
54 private StmtTestUtils() {
58 public static void log(final Throwable e, final String indent) {
59 LOG.debug(indent + e.getMessage());
61 final Throwable[] suppressed = e.getSuppressed();
62 for (final Throwable throwable : suppressed) {
63 log(throwable, indent + " ");
67 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
68 final List<Module> result = new ArrayList<>();
69 for (final Module module : modules) {
70 if (module.getName().equals(moduleName)) {
77 public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
78 for (final YangStatementSourceImpl source : sources) {
79 reactor.addSource(source);
83 public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
84 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
85 final Set<Module> submodules = module.getSubmodules();
86 for (final Module submodule : submodules) {
87 printReferences(submodule, true, indent + " ");
88 printChilds(submodule.getChildNodes(), indent + " ");
92 public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
94 for (final DataSchemaNode child : childNodes) {
95 LOG.debug(indent + "Child " + child.getQName().getLocalName());
96 if (child instanceof DataNodeContainer) {
97 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
102 public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
104 return parseYangSources(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES, sources);
107 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate, final StatementStreamSource... sources)
108 throws SourceException, ReactorException {
110 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode, ifFeaturePredicate);
111 reactor.addSources(sources);
113 return reactor.buildEffective();
116 public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
117 FileNotFoundException {
118 return parseYangSources(StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES, files);
121 public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate, final File... files) throws SourceException,
122 ReactorException, FileNotFoundException {
124 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
126 for (int i = 0; i < files.length; i++) {
127 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
130 return parseYangSources(statementParserMode, ifFeaturePredicate, sources);
133 public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
134 FileNotFoundException {
135 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
138 public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
139 throws SourceException, ReactorException, FileNotFoundException {
140 return parseYangSources(statementParserMode, IfFeaturePredicates.ALL_FEATURES, files.toArray(new File[files.size()]));
143 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
144 ReactorException, FileNotFoundException, URISyntaxException {
145 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
148 public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
149 FileNotFoundException, URISyntaxException {
150 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, IfFeaturePredicates.ALL_FEATURES);
153 public static SchemaContext parseYangSource(final String yangSourcePath, final Predicate<QName> ifFeaturesPredicate)
154 throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
155 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, ifFeaturesPredicate);
158 public static SchemaContext parseYangSource(final String yangSourcePath,
159 final StatementParserMode statementParserMode, final Predicate<QName> ifFeaturePredicate)
160 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
161 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
162 final File sourceFile = new File(source.toURI());
163 return parseYangSources(statementParserMode, ifFeaturePredicate, sourceFile);
166 public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath, final StatementParserMode statementParserMode)
167 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
169 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
170 final File testSourcesDir = new File(resourceDir.toURI());
172 return parseYangSources(statementParserMode, IfFeaturePredicates.ALL_FEATURES, testSourcesDir.listFiles(YANG_FILE_FILTER));
175 public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
176 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
178 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
179 final File testSourcesDir = new File(resourceDir.toURI());
181 return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
184 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
185 ReactorException, FileNotFoundException {
187 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
189 for (int i = 0; i < files.length; i++) {
190 sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
193 return parseYinSources(statementParserMode, sources);
196 public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
197 throws SourceException, ReactorException {
199 final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
200 reactor.addSources(sources);
202 return reactor.buildEffective();
205 public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
206 ModuleImport requestedModuleImport = null;
207 final Set<ModuleImport> rootImports = rootModule.getImports();
208 for (final ModuleImport moduleImport : rootImports) {
209 if (moduleImport.getModuleName().equals(importedModuleName)) {
210 requestedModuleImport = moduleImport;
215 final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
216 requestedModuleImport.getRevision());
217 return importedModule;