2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.stmt;
10 import com.google.common.io.Files;
12 import java.io.FileFilter;
13 import java.io.IOException;
14 import java.net.URISyntaxException;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
19 import java.util.List;
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;
47 public final class StmtTestUtils {
49 public static final FileFilter YANG_FILE_FILTER =
50 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
52 public static final FileFilter YIN_FILE_FILTER =
53 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
55 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
57 private StmtTestUtils() {
61 public static void log(final Throwable exception, final String indent) {
62 LOG.debug("{}{}", indent, exception.getMessage());
64 final Throwable[] suppressed = exception.getSuppressed();
65 for (final Throwable throwable : suppressed) {
66 log(throwable, indent + " ");
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)) {
80 public static StatementStreamSource sourceForResource(final String resourceName) {
82 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
83 } catch (IOException | YangSyntaxErrorException e) {
84 throw new IllegalArgumentException("Failed to create source", e);
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 + " ");
96 public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
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 + " ");
106 public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
107 URISyntaxException, IOException, YangSyntaxErrorException {
108 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
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);
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);
124 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
125 throws ReactorException {
126 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
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));
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);
143 return reactor.buildEffective();
146 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
147 YangSyntaxErrorException {
148 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
151 public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
152 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
153 YangSyntaxErrorException {
155 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
156 for (File file : files) {
157 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
160 return parseYangSources(statementParserMode, supportedFeatures, sources);
163 public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
164 IOException, YangSyntaxErrorException {
165 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
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()]));
174 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
175 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
176 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
179 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
180 final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
181 YangSyntaxErrorException {
182 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
185 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
186 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
187 URISyntaxException, IOException, YangSyntaxErrorException {
189 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
190 final File testSourcesDir = new File(resourceDir.toURI());
192 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
195 public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
196 final String yangLibsDirectoryPath)
197 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
198 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
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());
207 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
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]));
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]));
223 return parseYangSources(yangSources, libSources, supportedFeatures);
226 private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
227 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
229 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
230 .addSources(yangSources).addLibSources(libSources);
231 if (supportedFeatures != null) {
232 reactor.setSupportedFeatures(supportedFeatures);
235 return reactor.buildEffective();
238 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
239 final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
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());
247 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
248 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
251 return parseYinSources(statementParserMode, sources);
254 public static EffectiveModelContext parseYinSources(final StatementParserMode statementParserMode,
255 final StatementStreamSource... sources) throws ReactorException {
256 return RFC7950Reactors.defaultReactor().newBuild(statementParserMode).addSources(sources).buildEffective();
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;
269 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())