Remove deprecated Yin/YangStatementSourceImpl
[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 com.google.common.io.Files;
12 import java.io.File;
13 import java.io.FileFilter;
14 import java.io.IOException;
15 import java.net.URISyntaxException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
21 import java.util.Set;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
30 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
31 import org.opendaylight.yangtools.yang.model.repo.api.StatementParserMode;
32 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
33 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
34 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc6020.repo.YinTextToDomTransformer;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
39 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
41 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.xml.sax.SAXException;
45
46 public class StmtTestUtils {
47
48     public static final FileFilter YANG_FILE_FILTER =
49             file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
50
51     public static final FileFilter YIN_FILE_FILTER =
52             file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
53
54     private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
55
56     private StmtTestUtils() {
57
58     }
59
60     public static void log(final Throwable e, final String indent) {
61         LOG.debug(indent + e.getMessage());
62
63         final Throwable[] suppressed = e.getSuppressed();
64         for (final Throwable throwable : suppressed) {
65             log(throwable, indent + "        ");
66         }
67     }
68
69     public static List<Module> findModules(final Set<Module> modules, final String moduleName) {
70         final List<Module> result = new ArrayList<>();
71         for (final Module module : modules) {
72             if (module.getName().equals(moduleName)) {
73                 result.add(module);
74             }
75         }
76         return result;
77     }
78
79     public static StatementStreamSource sourceForResource(final String resourceName) {
80         try {
81             return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
82         } catch (IOException | YangSyntaxErrorException e) {
83             throw new IllegalArgumentException("Failed to create source", e);
84         }
85     }
86
87     public static void printReferences(final Module module, final boolean isSubmodule, final String indent) {
88         LOG.debug(indent + (isSubmodule ? "Submodule " : "Module ") + module.getName());
89         final Set<Module> submodules = module.getSubmodules();
90         for (final Module submodule : submodules) {
91             printReferences(submodule, true, indent + "      ");
92             printChilds(submodule.getChildNodes(), indent + "            ");
93         }
94     }
95
96     public static void printChilds(final Collection<DataSchemaNode> childNodes, final String indent) {
97
98         for (final DataSchemaNode child : childNodes) {
99             LOG.debug(indent + "Child " + child.getQName().getLocalName());
100             if (child instanceof DataNodeContainer) {
101                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
102             }
103         }
104     }
105
106     public static SchemaContext parseYangSources(final StatementStreamSource... sources) throws SourceException,
107             ReactorException {
108         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
109     }
110
111     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
112             final Set<QName> supportedFeatures, final StatementStreamSource... sources)
113             throws SourceException, ReactorException {
114         return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
115     }
116
117     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
118             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
119             throws SourceException, ReactorException {
120         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(
121             statementParserMode);
122         reactor.addSources(sources);
123         if (supportedFeatures != null) {
124             reactor.setSupportedFeatures(supportedFeatures);
125         }
126
127         return reactor.buildEffective();
128     }
129
130     public static SchemaContext parseYangSources(final File... files) throws SourceException, ReactorException,
131             IOException, YangSyntaxErrorException {
132         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
133     }
134
135     public static SchemaContext parseYangSources(final StatementParserMode statementParserMode,
136             final Set<QName> supportedFeatures, final File... files) throws SourceException,
137             ReactorException, IOException, YangSyntaxErrorException {
138
139         final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
140         for (File file : files) {
141             sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
142         }
143
144         return parseYangSources(statementParserMode, supportedFeatures, sources);
145     }
146
147     public static SchemaContext parseYangSources(final Collection<File> files) throws SourceException, ReactorException,
148             IOException, YangSyntaxErrorException {
149         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
150     }
151
152     public static SchemaContext parseYangSources(final Collection<File> files, final StatementParserMode statementParserMode)
153             throws SourceException, ReactorException, IOException, YangSyntaxErrorException {
154         return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
155     }
156
157     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath) throws SourceException,
158             ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
159         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
160     }
161
162     public static SchemaContext parseYangSource(final String yangSourcePath) throws SourceException, ReactorException,
163             URISyntaxException, IOException, YangSyntaxErrorException {
164         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
165     }
166
167     public static SchemaContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
168             throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
169         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
170     }
171
172     public static SchemaContext parseYangSource(final String yangSourcePath,
173             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
174             throws SourceException, ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
175         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
176         final File sourceFile = new File(source.toURI());
177         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
178     }
179
180     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
181             final StatementParserMode statementParserMode) throws SourceException, ReactorException,
182             URISyntaxException, IOException, YangSyntaxErrorException {
183         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
184     }
185
186     public static SchemaContext parseYangSources(final String yangSourcesDirectoryPath,
187             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws SourceException,
188             ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
189
190         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
191         final File testSourcesDir = new File(resourceDir.toURI());
192
193         return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
194     }
195
196     public static SchemaContext parseYinSources(final String yinSourcesDirectoryPath, final StatementParserMode statementParserMode)
197             throws URISyntaxException, SAXException, IOException, ReactorException {
198         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
199         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
200         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
201         for (int i = 0; i < files.length; i++) {
202             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
203
204             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
205                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
206         }
207
208         return parseYinSources(statementParserMode, sources);
209     }
210
211     public static SchemaContext parseYinSources(final StatementParserMode statementParserMode, final StatementStreamSource... sources)
212             throws ReactorException {
213
214         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(statementParserMode);
215         reactor.addSources(sources);
216
217         return reactor.buildEffective();
218     }
219
220     public static Module findImportedModule(final SchemaContext context, final Module rootModule, final String importedModuleName) {
221         ModuleImport requestedModuleImport = null;
222         final Set<ModuleImport> rootImports = rootModule.getImports();
223         for (final ModuleImport moduleImport : rootImports) {
224             if (moduleImport.getModuleName().equals(importedModuleName)) {
225                 requestedModuleImport = moduleImport;
226                 break;
227             }
228         }
229
230         final Module importedModule = context.findModuleByName(requestedModuleImport.getModuleName(),
231                 requestedModuleImport.getRevision());
232         return importedModule;
233     }
234
235     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath, final String yangLibsDirectoryPath)
236             throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
237         return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
238     }
239
240     public static SchemaContext parseYangSources(final String yangFilesDirectoryPath,
241             final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
242             ReactorException, IOException, YangSyntaxErrorException {
243         final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
244         final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
245
246         return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
247                 supportedFeatures);
248     }
249
250     private static SchemaContext parseYangSources(final File[] yangFiles, final File[] libFiles,
251             final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
252         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
253         for (int i = 0; i < yangFiles.length; i++) {
254             yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
255         }
256
257         final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
258         for (int i = 0; i < libFiles.length; i++) {
259             libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
260         }
261
262         return parseYangSources(yangSources, libSources, supportedFeatures);
263     }
264
265     private static SchemaContext parseYangSources(final StatementStreamSource[] yangSources,
266             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
267
268         final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
269         reactor.addSources(yangSources);
270         reactor.addLibSources(libSources);
271         if (supportedFeatures != null) {
272             reactor.setSupportedFeatures(supportedFeatures);
273         }
274
275         return reactor.buildEffective();
276     }
277 }