Separate out Module and Submodule interfaces
[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(resourceName));
83         } catch (IOException | YangSyntaxErrorException e) {
84             throw new IllegalArgumentException("Failed to create source", e);
85         }
86     }
87
88     public static void printReferences(final ModuleLike module, final boolean isSubmodule, final String indent) {
89         LOG.debug("{}{} {}", indent, isSubmodule ? "Submodule" : "Module", module.getName());
90         for (final Submodule submodule : module.getSubmodules()) {
91             printReferences(submodule, true, indent + "      ");
92             printChilds(submodule.getChildNodes(), indent + "            ");
93         }
94     }
95
96     public static void printChilds(final Collection<? extends 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 EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
107             URISyntaxException, IOException, YangSyntaxErrorException {
108         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
109     }
110
111     public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
112             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
113         return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, supportedFeatures);
114     }
115
116     public static EffectiveModelContext parseYangSource(final String yangSourcePath,
117             final StatementParserMode statementParserMode, final Set<QName> supportedFeatures)
118                     throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
119         final URL source = StmtTestUtils.class.getResource(yangSourcePath);
120         final File sourceFile = new File(source.toURI());
121         return parseYangSources(statementParserMode, supportedFeatures, sourceFile);
122     }
123
124     public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
125             throws ReactorException {
126         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
127     }
128
129     public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
130             final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
131         return parseYangSources(statementParserMode, supportedFeatures, Arrays.asList(sources));
132     }
133
134     public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
135             final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
136             throws ReactorException {
137         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild(statementParserMode)
138                 .addSources(sources);
139         if (supportedFeatures != null) {
140             reactor.setSupportedFeatures(supportedFeatures);
141         }
142
143         return reactor.buildEffective();
144     }
145
146     public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
147             YangSyntaxErrorException {
148         return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
149     }
150
151     public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
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(statementParserMode, supportedFeatures, sources);
161     }
162
163     public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
164             IOException, YangSyntaxErrorException {
165         return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
166     }
167
168     public static EffectiveModelContext parseYangSources(final Collection<File> files,
169             final StatementParserMode statementParserMode) throws ReactorException, IOException,
170             YangSyntaxErrorException {
171         return parseYangSources(statementParserMode, null, files.toArray(new File[files.size()]));
172     }
173
174     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
175             throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
176         return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
177     }
178
179     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
180             final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
181             YangSyntaxErrorException {
182         return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
183     }
184
185     public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
186             final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
187             URISyntaxException, IOException, YangSyntaxErrorException {
188
189         final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
190         final File testSourcesDir = new File(resourceDir.toURI());
191
192         return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
193     }
194
195     public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
196             final String yangLibsDirectoryPath)
197             throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
198         return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
199     }
200
201     public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
202             final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
203             ReactorException, IOException, YangSyntaxErrorException {
204         final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
205         final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
206
207         return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
208                 supportedFeatures);
209     }
210
211     private static EffectiveModelContext parseYangSources(final File[] yangFiles, final File[] libFiles,
212             final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
213         final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
214         for (int i = 0; i < yangFiles.length; i++) {
215             yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
216         }
217
218         final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
219         for (int i = 0; i < libFiles.length; i++) {
220             libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
221         }
222
223         return parseYangSources(yangSources, libSources, supportedFeatures);
224     }
225
226     private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
227             final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
228
229         final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
230                 .addSources(yangSources).addLibSources(libSources);
231         if (supportedFeatures != null) {
232             reactor.setSupportedFeatures(supportedFeatures);
233         }
234
235         return reactor.buildEffective();
236     }
237
238     public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
239             final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
240             ReactorException {
241         final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
242         final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
243         final StatementStreamSource[] sources = new StatementStreamSource[files.length];
244         for (int i = 0; i < files.length; i++) {
245             final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
246
247             sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
248                 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
249         }
250
251         return parseYinSources(statementParserMode, sources);
252     }
253
254     public static EffectiveModelContext parseYinSources(final StatementParserMode statementParserMode,
255             final StatementStreamSource... sources) throws ReactorException {
256         return RFC7950Reactors.defaultReactor().newBuild(statementParserMode).addSources(sources).buildEffective();
257     }
258
259     public static Module findImportedModule(final SchemaContext context, final Module rootModule,
260             final String importedModuleName) {
261         ModuleImport requestedModuleImport = null;
262         for (final ModuleImport moduleImport : rootModule.getImports()) {
263             if (moduleImport.getModuleName().equals(importedModuleName)) {
264                 requestedModuleImport = moduleImport;
265                 break;
266             }
267         }
268
269         return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
270                 .orElse(null);
271     }
272 }