Bug 6867: Extend yang statement parser to support different yang versions
[yangtools.git] / yang / yang-parser-impl / src / test / java / org / opendaylight / yangtools / yang / stmt / StmtTestUtils.java
1 /*
2  * Copyright (c) 2016 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;
10
11 import java.io.File;
12 import java.io.FileFilter;
13 import java.io.FileNotFoundException;
14 import java.net.URISyntaxException;
15 import java.net.URL;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.Set;
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;
36
37 public class StmtTestUtils {
38
39     final public static FileFilter YANG_FILE_FILTER = file -> {
40         final String name = file.getName().toLowerCase();
41         return name.endsWith(".yang") && file.isFile();
42     };
43
44     final public static FileFilter YIN_FILE_FILTER = file -> {
45         final String name = file.getName().toLowerCase();
46         return name.endsWith(".xml") && file.isFile();
47     };
48
49     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
50
51     private StmtTestUtils() {
52
53     }
54
55     public static void log(final Throwable e, final String indent) {
56         LOG.debug(indent + e.getMessage());
57
58         final Throwable[] suppressed = e.getSuppressed();
59         for (final Throwable throwable : suppressed) {
60             log(throwable, indent + "        ");
61         }
62     }
63
64     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
65         final List<Module> result = new ArrayList<>();
66         for (final Module module : modules) {
67             if (module.getName().equals(moduleName)) {
68                 result.add(module);
69             }
70         }
71         return result;
72     }
73
74     public static void addSources(final CrossSourceStatementReactor.BuildAction reactor, final YangStatementSourceImpl... sources) {
75         for (final YangStatementSourceImpl source : sources) {
76             reactor.addSource(source);
77         }
78     }
79
80     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
81         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
82         final Set<Module> submodules = module.getSubmodules();
83         for (final Module submodule : submodules) {
84             printReferences(submodule, true, indent + "      ");
85             printChilds(submodule.getChildNodes(), indent + "            ");
86         }
87     }
88
89     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
90
91         for (final DataSchemaNode child : childNodes) {
92             LOG.debug(indent + "Child " + child.getQName().getLocalName());
93             if (child instanceof DataNodeContainer) {
94                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
95             }
96         }
97     }
98
99     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
100             ReactorException {
101         return parseYangSources(StatementParserMode.DEFAULT_MODE, sources);
102     }
103
104     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
105             throws SourceException, ReactorException {
106
107         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
108         reactor.addSources(sources);
109
110         return reactor.buildEffective();
111     }
112
113     public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
114             FileNotFoundException {
115         return parseYangSources(StatementParserMode.DEFAULT_MODE, files);
116     }
117
118     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
119             ReactorException, FileNotFoundException {
120
121         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
122
123         for (int i = 0; i < files.length; i++) {
124             sources[i] = new YangStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
125         }
126
127         return parseYangSources(statementParserMode, sources);
128     }
129
130     public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
131             FileNotFoundException {
132         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
133     }
134
135     public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
136             throws SourceException, ReactorException, FileNotFoundException {
137         return parseYangSources(statementParserMode, files.toArray(new File[files.size()]));
138     }
139
140     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
141             ReactorException, FileNotFoundException, URISyntaxException {
142         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
143     }
144
145     public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
146             FileNotFoundException, URISyntaxException {
147         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE);
148     }
149
150     public static SchemaContext parseYangSource(final String yangSourcePath, final StatementParserMode statementParserMode)
151             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
152         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
153         final File sourceFile = new File(source.toURI());
154         return parseYangSources(statementParserMode, sourceFile);
155     }
156
157     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath, final StatementParserMode statementParserMode)
158             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
159
160         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
161         final File testSourcesDir = new File(resourceDir.toURI());
162
163         return parseYangSources(statementParserMode, testSourcesDir.listFiles(YANG_FILE_FILTER));
164     }
165
166     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
167             throws SourceException, ReactorException, FileNotFoundException, URISyntaxException {
168
169         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
170         final File testSourcesDir = new File(resourceDir.toURI());
171
172         return parseYinSources(statementParserMode, testSourcesDir.listFiles(YIN_FILE_FILTER));
173     }
174
175     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final File... files) throws SourceException,
176             ReactorException, FileNotFoundException {
177
178         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
179
180         for (int i = 0; i < files.length; i++) {
181             sources[i] = new YinStatementSourceImpl(new NamedFileInputStream(files[i], files[i].getPath()));
182         }
183
184         return parseYinSources(statementParserMode, sources);
185     }
186
187     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
188             throws SourceException, ReactorException {
189
190         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
191         reactor.addSources(sources);
192
193         return reactor.buildEffective();
194     }
195
196     public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
197         ModuleImport requestedModuleImport = null;
198         final Set<ModuleImport> rootImports = rootModule.getImports();
199         for (final ModuleImport moduleImport : rootImports) {
200             if (moduleImport.getModuleName().equals(importedModuleName)) {
201                 requestedModuleImport = moduleImport;
202                 break;
203             }
204         }
205
206         final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
207                 requestedModuleImport.getRevision());
208         return importedModule;
209     }
210 }