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 = new FileFilter() {
41 public boolean accept(File file) {
42 String name = file.getName().toLowerCase();
43 return name.endsWith(".yang") && file.isFile();
47 final public static FileFilter YIN_FILE_FILTER = new FileFilter() {
49 public boolean accept(File file) {
50 String name = file.getName().toLowerCase();
51 return name.endsWith(".xml") && file.isFile();
55 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
57 private StmtTestUtils() {
61 public static void log(Throwable e, String indent) {
62 LOG.debug(indent + e.getMessage());
64 Throwable[] suppressed = e.getSuppressed();
65 for (Throwable throwable : suppressed) {
66 log(throwable, indent + " ");
70 public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
71 List<Module> result = new ArrayList<>();
72 for (Module module : modules) {
73 if (module.getName().equals(moduleName)) {
80 public static void addSources(CrossSourceStatementReactor.BuildAction reactor, YangStatementSourceImpl... sources) {
81 for (YangStatementSourceImpl source : sources) {
82 reactor.addSource(source);
86 public static void printReferences(Module module, boolean isSubmodule, String indent) {
87 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
88 Set<Module> submodules = module.getSubmodules();
89 for (Module submodule : submodules) {
90 printReferences(submodule, true, indent + " ");
91 printChilds(submodule.getChildNodes(), indent + " ");
95 public static void printChilds(Collection<DataSchemaNode> childNodes, String indent) {
97 for (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 parseYangSources(StatementStreamSource... sources) throws SourceException,
107 return parseYangSources(StatementParserMode.DEFAULT_MODE, sources);
110 public static SchemaContext parseYangSources(StatementParserMode statementParserMode, StatementStreamSource... sources)
111 throws SourceException, ReactorException {
113 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
114 reactor.addSources(sources);
116 return reactor.buildEffective();
119 public static SchemaContext parseYangSources(File... files) throws SourceException, ReactorException,
120 FileNotFoundException {
121 return parseYangSources(StatementParserMode.DEFAULT_MODE, files);
124 public static SchemaContext parseYangSources(StatementParserMode statementParserMode, File... files) throws SourceException,
125 ReactorException, FileNotFoundException {
127 StatementStreamSource[] sources = new StatementStreamSource[files.length];
129 for (int i = 0; i < files.length; i++) {
130 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
133 return parseYangSources(statementParserMode, sources);
136 public static SchemaContext parseYangSources(Collection<File> files) throws SourceException, ReactorException,
137 FileNotFoundException {
138 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
141 public static SchemaContext parseYangSources(Collection<File> files, StatementParserMode statementParserMode)
142 throws SourceException, ReactorException, FileNotFoundException {
143 return parseYangSources(statementParserMode, files.toArray(new File[files.size()]));
146 public static SchemaContext parseYangSources(String yangSourcesDirectoryPath) throws SourceException,
147 ReactorException, FileNotFoundException, URISyntaxException {
148 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
151 public static SchemaContext parseYangSources(String yangSourcesDirectoryPath, StatementParserMode statementParserMode)
152 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
154 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
155 File testSourcesDir = new File(resourceDir.toURI());
157 return parseYangSources(statementParserMode, testSourcesDir.listFiles(YANG_FILE_FILTER));
160 public static SchemaContext parseYinSources(String yinSourcesDirectoryPath, StatementParserMode statementParserMode)
161 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
163 URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
164 File testSourcesDir = new File(resourceDir.toURI());
166 return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
169 public static SchemaContext parseYinSources(StatementParserMode statementParserMode, File... files) throws SourceException,
170 ReactorException, FileNotFoundException {
172 StatementStreamSource[] sources = new StatementStreamSource[files.length];
174 for (int i = 0; i < files.length; i++) {
175 sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
178 return parseYinSources(statementParserMode, sources);
181 public static SchemaContext parseYinSources(StatementParserMode statementParserMode, StatementStreamSource... sources)
182 throws SourceException, ReactorException {
184 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
185 reactor.addSources(sources);
187 return reactor.buildEffective();
190 public static Module findImportedModule(SchemaContext context, Module rootModule, String importedModuleName) {
191 ModuleImport requestedModuleImport = null;
192 Set<ModuleImport> rootImports = rootModule.getImports();
193 for (ModuleImport moduleImport : rootImports) {
194 if (moduleImport.getModuleName().equals(importedModuleName)) {
195 requestedModuleImport = moduleImport;
200 Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
201 requestedModuleImport.getRevision());
202 return importedModule;