Bug 4540: Yang parser exceptions should follow consistent path
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / test / StmtTestUtils.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.yangtools.yang.stmt.test;
10
11 import java.io.FileFilter;
12
13 import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.io.File;
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;
28 import java.util.Set;
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;
34
35 public class StmtTestUtils {
36
37     final public static FileFilter YANG_FILE_FILTER = new FileFilter() {
38         @Override
39         public boolean accept(File file) {
40             String name = file.getName().toLowerCase();
41             return name.endsWith(".yang") && file.isFile();
42         }
43     };
44
45     private static final Logger LOG = LoggerFactory
46             .getLogger(StmtTestUtils.class);
47
48     private StmtTestUtils() {
49
50     }
51
52     public static void log(Throwable e, String indent) {
53         LOG.debug(indent + e.getMessage());
54
55         Throwable[] suppressed = e.getSuppressed();
56         for (Throwable throwable : suppressed) {
57             log(throwable, indent + "        ");
58         }
59     }
60
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)) {
66                 result.add(module);
67             }
68         }
69         return result;
70     }
71
72     public static void addSources(
73             CrossSourceStatementReactor.BuildAction reactor,
74             YangStatementSourceImpl... sources) {
75         for (YangStatementSourceImpl source : sources) {
76             reactor.addSource(source);
77         }
78     }
79
80     public static void printReferences(Module module, boolean isSubmodule,
81             String indent) {
82         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ")
83                 + module.getName());
84         Set<Module> submodules = module.getSubmodules();
85         for (Module submodule : submodules) {
86             printReferences(submodule, true, indent + "      ");
87             printChilds(submodule.getChildNodes(), indent + "            ");
88         }
89     }
90
91     public static void printChilds(Collection<DataSchemaNode> childNodes,
92             String indent) {
93
94         for (DataSchemaNode child : childNodes) {
95             LOG.debug(indent + "Child " + child.getQName().getLocalName());
96             if (child instanceof DataNodeContainer) {
97                 printChilds(((DataNodeContainer) child).getChildNodes(), indent
98                         + "      ");
99             }
100         }
101     }
102
103     public static SchemaContext parseYangSources(
104             StatementStreamSource... sources) throws SourceException,
105             ReactorException {
106
107         CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR
108                 .newBuild();
109         reactor.addSources(sources);
110
111         return reactor.buildEffective();
112     }
113
114     public static SchemaContext parseYangSources(File... files)
115             throws SourceException, ReactorException, FileNotFoundException {
116
117         StatementStreamSource[] sources = new StatementStreamSource[files.length];
118
119         for (int i = 0; i < files.length; i++) {
120             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
121         }
122
123         return parseYangSources(sources);
124     }
125
126     public static SchemaContext parseYangSources(Collection<File> files)
127             throws SourceException, ReactorException, FileNotFoundException {
128         return parseYangSources(files.toArray(new File[files.size()]));
129     }
130
131     public static SchemaContext parseYangSources(String yangSourcesDirectoryPath)
132             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
133
134         URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
135         File testSourcesDir = new File(resourceDir.toURI());
136
137         return parseYangSources(testSourcesDir.listFiles(YANG_FILE_FILTER));
138     }
139 }