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.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;
45 public final class StmtTestUtils {
47 public static final FileFilter YANG_FILE_FILTER =
48 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
50 public static final FileFilter YIN_FILE_FILTER =
51 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
53 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
55 private StmtTestUtils() {
59 public static void log(final Throwable exception, final String indent) {
60 LOG.debug("{}{}", indent, exception.getMessage());
62 final Throwable[] suppressed = exception.getSuppressed();
63 for (final Throwable throwable : suppressed) {
64 log(throwable, indent + " ");
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)) {
78 public static StatementStreamSource sourceForResource(final String resourceName) {
80 return YangStatementStreamSource.create(YangTextSchemaSource.forResource(resourceName));
81 } catch (IOException | YangSyntaxErrorException e) {
82 throw new IllegalArgumentException("Failed to create source", e);
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 + " ");
94 public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
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 + " ");
104 public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
105 URISyntaxException, IOException, YangSyntaxErrorException {
106 return parseYangSource(yangSourcePath, StatementParserMode.DEFAULT_MODE, null);
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);
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);
122 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
123 throws ReactorException {
124 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, sources);
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));
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);
141 return reactor.buildEffective();
144 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
145 YangSyntaxErrorException {
146 return parseYangSources(StatementParserMode.DEFAULT_MODE, null, files);
149 public static EffectiveModelContext parseYangSources(final StatementParserMode statementParserMode,
150 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
151 YangSyntaxErrorException {
153 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
154 for (File file : files) {
155 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
158 return parseYangSources(statementParserMode, supportedFeatures, sources);
161 public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
162 IOException, YangSyntaxErrorException {
163 return parseYangSources(files, StatementParserMode.DEFAULT_MODE);
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()]));
172 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
173 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
174 return parseYangSources(yangSourcesDirectoryPath, StatementParserMode.DEFAULT_MODE);
177 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
178 final StatementParserMode statementParserMode) throws ReactorException, URISyntaxException, IOException,
179 YangSyntaxErrorException {
180 return parseYangSources(yangSourcesDirectoryPath, null, statementParserMode);
183 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
184 final Set<QName> supportedFeatures, final StatementParserMode statementParserMode) throws ReactorException,
185 URISyntaxException, IOException, YangSyntaxErrorException {
187 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
188 final File testSourcesDir = new File(resourceDir.toURI());
190 return parseYangSources(statementParserMode, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
193 public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
194 final String yangLibsDirectoryPath)
195 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
196 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
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());
205 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
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]));
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]));
221 return parseYangSources(yangSources, libSources, supportedFeatures);
224 private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
225 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
227 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
228 .addSources(yangSources).addLibSources(libSources);
229 if (supportedFeatures != null) {
230 reactor.setSupportedFeatures(supportedFeatures);
233 return reactor.buildEffective();
236 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
237 final StatementParserMode statementParserMode) throws URISyntaxException, SAXException, IOException,
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());
245 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
246 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
249 return parseYinSources(statementParserMode, sources);
252 public static EffectiveModelContext parseYinSources(final StatementParserMode statementParserMode,
253 final StatementStreamSource... sources) throws ReactorException {
254 return RFC7950Reactors.defaultReactor().newBuild(statementParserMode).addSources(sources).buildEffective();
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;
267 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())