2 * Copyright (c) 2015 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.test;
11 import java.io.FileFilter;
13 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
14 import java.net.URISyntaxException;
17 import java.io.FileNotFoundException;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
21 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
22 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
23 import java.util.ArrayList;
24 import java.util.List;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import java.util.Collection;
29 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
30 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
31 import org.opendaylight.yangtools.yang.model.api.Module;
32 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
33 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
35 public class StmtTestUtils {
37 final public static FileFilter YANG_FILE_FILTER = new FileFilter() {
39 public boolean accept(File file) {
40 String name = file.getName().toLowerCase();
41 return name.endsWith(".yang") && file.isFile();
45 private static final Logger LOG = LoggerFactory
46 .getLogger(StmtTestUtils.class);
48 private StmtTestUtils() {
52 public static void log(Throwable e, String indent) {
53 LOG.debug(indent + e.getMessage());
55 Throwable[] suppressed = e.getSuppressed();
56 for (Throwable throwable : suppressed) {
57 log(throwable, indent + " ");
61 public static List<Module> findModules(final Set<Module> modules,
62 final String moduleName) {
63 List<Module> result = new ArrayList<>();
64 for (Module module : modules) {
65 if (module.getName().equals(moduleName)) {
72 public static void addSources(
73 CrossSourceStatementReactor.BuildAction reactor,
74 YangStatementSourceImpl... sources) {
75 for (YangStatementSourceImpl source : sources) {
76 reactor.addSource(source);
80 public static void printReferences(Module module, boolean isSubmodule,
82 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ")
84 Set<Module> submodules = module.getSubmodules();
85 for (Module submodule : submodules) {
86 printReferences(submodule, true, indent + " ");
87 printChilds(submodule.getChildNodes(), indent + " ");
91 public static void printChilds(Collection<DataSchemaNode> childNodes,
94 for (DataSchemaNode child : childNodes) {
95 LOG.debug(indent + "Child " + child.getQName().getLocalName());
96 if (child instanceof DataNodeContainer) {
97 printChilds(((DataNodeContainer) child).getChildNodes(), indent
103 public static SchemaContext parseYangSources(
104 StatementStreamSource... sources) throws SourceException,
107 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
109 reactor.addSources(sources);
111 return reactor.buildEffective();
114 public static SchemaContext parseYangSources(File... files)
115 throws SourceException, ReactorException, FileNotFoundException {
117 StatementStreamSource[] sources = new StatementStreamSource[files.length];
119 for (int i = 0; i < files.length; i++) {
120 sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
123 return parseYangSources(sources);
126 public static SchemaContext parseYangSources(Collection<File> files)
127 throws SourceException, ReactorException, FileNotFoundException {
128 return parseYangSources(files.toArray(new File[files.size()]));
131 public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
132 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
134 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
135 File testSourcesDir = new File(resourceDir.toURI());
137 return parseYangSources(testSourcesDir.listFiles(YANG_FILE_FILTER));