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