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(
83 StmtTestUtils.class, resourceName));
84 } catch (IOException | YangSyntaxErrorException e) {
85 throw new IllegalArgumentException("Failed to create source", e);
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 + " ");
97 public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
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 + " ");
107 public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
108 URISyntaxException, IOException, YangSyntaxErrorException {
109 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
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);
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);
125 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
126 throws ReactorException {
127 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
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));
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);
144 return reactor.buildEffective();
147 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
148 YangSyntaxErrorException {
149 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
152 public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
153 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
154 YangSyntaxErrorException {
156 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
157 for (File file : files) {
158 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
161 return parseYangSources(statementParserMode, supportedFeatures, sources);
164 public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
165 IOException, YangSyntaxErrorException {
166 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
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()]));
175 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
176 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
177 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
180 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
181 final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
182 YangSyntaxErrorException {
183 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
186 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
187 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
188 URISyntaxException, IOException, YangSyntaxErrorException {
190 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
191 final File testSourcesDir = new File(resourceDir.toURI());
193 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
196 public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
197 final String yangLibsDirectoryPath)
198 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
199 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
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());
208 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
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]));
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]));
224 return parseYangSources(yangSources, libSources, supportedFeatures);
227 private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
228 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
230 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
231 .addSources(yangSources).addLibSources(libSources);
232 if (supportedFeatures != null) {
233 reactor.setSupportedFeatures(supportedFeatures);
236 return reactor.buildEffective();
239 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
240 final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
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());
248 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
249 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
252 return parseYinSources(statementParserMode, sources);
255 public static EffectiveModelContext parseYinSources(final StatementParserMode statementParserMode,
256 final StatementStreamSource... sources) throws ReactorException {
257 return RFC7950Reactors.defaultReactor().newBuild(statementParserMode).addSources(sources).buildEffective();
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;
270 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())