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