04af52672575dce330b31c8b9673f0079dfe5823
[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.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 final 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 Collection<? extends 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         for (final Module submodule : module.getSubmodules()) {
89             printReferences(submodule, true, indent + "      ");
90             printChilds(submodule.getChildNodes(), indent + "            ");
91         }
92     }
93
94     public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
95
96         for (final DataSchemaNode child : childNodes) {
97             LOG.debug("{}{} {}", indent, "Child", child.getQName().getLocalName());
98             if (child instanceof DataNodeContainer) {
99                 printChilds(((DataNodeContainer) child).getChildNodes(), indent + "      ");
100             }
101         }
102     }
103
104     public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
105             URISyntaxException, IOException, YangSyntaxErrorException {
106         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
107     }
108
109     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
110             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
111         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
112     }
113
114     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
115             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
116                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
117         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
118         final File sourceFile = new File(source.toURI());
119         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
120     }
121
122     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
123             throws ReactorException {
124         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
125     }
126
127     public static EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
145             YangSyntaxErrorException {
146         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
147     }
148
149     public static EffectiveModelContext 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 EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
162             IOException, YangSyntaxErrorException {
163         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
164     }
165
166     public static EffectiveModelContext 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 EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
173             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
174         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
175     }
176
177     public static EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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 EffectiveModelContext 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         for (final ModuleImport moduleImport : rootModule.getImports()) {
261             if (moduleImport.getModuleName().equals(importedModuleName)) {
262                 requestedModuleImport = moduleImport;
263                 break;
264             }
265         }
266
267         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
268                 .orElse(null);
269     }
270 }