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.net.URISyntaxException;
15 import java.io.FileInputStream;
16 import java.io.FileNotFoundException;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
19 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
20 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
21 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
22 import java.util.ArrayList;
23 import java.util.List;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import java.util.Collection;
28 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
32 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl;
34 public class StmtTestUtils {
36 private static final Logger LOG = LoggerFactory
37 .getLogger(StmtTestUtils.class);
39 private StmtTestUtils() {
43 public static void log(Throwable e, String indent) {
44 LOG.debug(indent + e.getMessage());
46 Throwable[] suppressed = e.getSuppressed();
47 for (Throwable throwable : suppressed) {
48 log(throwable, indent + " ");
52 public static List<Module> findModules(final Set<Module> modules,
53 final String moduleName) {
54 List<Module> result = new ArrayList<>();
55 for (Module module : modules) {
56 if (module.getName().equals(moduleName)) {
63 public static void addSources(
64 CrossSourceStatementReactor.BuildAction reactor,
65 YangStatementSourceImpl... sources) {
66 for (YangStatementSourceImpl source : sources) {
67 reactor.addSource(source);
71 public static void printReferences(Module module, boolean isSubmodule,
73 LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ")
75 Set<Module> submodules = module.getSubmodules();
76 for (Module submodule : submodules) {
77 printReferences(submodule, true, indent + " ");
78 printChilds(submodule.getChildNodes(), indent + " ");
82 public static void printChilds(Collection<DataSchemaNode> childNodes,
85 for (DataSchemaNode child : childNodes) {
86 LOG.debug(indent + "Child " + child.getQName().getLocalName());
87 if (child instanceof DataNodeContainer) {
88 printChilds(((DataNodeContainer) child).getChildNodes(), indent
94 public static SchemaContext parseYangSources(
95 StatementStreamSource... sources) throws SourceException,
98 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
100 reactor.addSources(sources);
102 return reactor.buildEffective();
105 public static SchemaContext parseYangSources(File... files)
106 throws SourceException, ReactorException, FileNotFoundException {
108 StatementStreamSource[] sources = new StatementStreamSource[files.length];
110 for (int i = 0; i < files.length; i++) {
111 sources[i] = new YangStatementSourceImpl(new FileInputStream(
115 return parseYangSources(sources);
118 public static SchemaContext parseYangSources(Collection<File> files)
119 throws SourceException, ReactorException, FileNotFoundException {
120 return parseYangSources(files.toArray(new File[files.size()]));
123 public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
124 throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
126 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
127 File testSourcesDir = new File(resourceDir.toURI());
129 return parseYangSources(testSourcesDir.listFiles());