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