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.nio.file.Path;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.List;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.YangConstants;
24 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
25 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.Module;
28 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
29 import org.opendaylight.yangtools.yang.model.api.ModuleLike;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.Submodule;
32 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
34 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
35 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
36 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
39 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
40 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
41 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
42 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47 import org.xml.sax.SAXException;
49 public final class StmtTestUtils {
51 public static final FileFilter YANG_FILE_FILTER =
52 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
54 public static final FileFilter YIN_FILE_FILTER =
55 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
57 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
59 private StmtTestUtils() {
63 public static void log(final Throwable exception, final String indent) {
64 LOG.debug("{}{}", indent, exception.getMessage());
66 final Throwable[] suppressed = exception.getSuppressed();
67 for (final Throwable throwable : suppressed) {
68 log(throwable, indent + " ");
72 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
73 final List<Module> result = new ArrayList<>();
74 for (final Module module : modules) {
75 if (module.getName().equals(moduleName)) {
82 public static YangStatementStreamSource sourceForResource(final String resourceName) {
84 return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
85 StmtTestUtils.class.getResource(resourceName).toURI())));
86 } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
87 throw new IllegalArgumentException("Failed to create source", e);
91 public static void printReferences(final ModuleLike module, final boolean isSubmodule, final String indent) {
92 LOG.debug("{}{} {}", indent, isSubmodule ? "Submodule" : "Module", module.getName());
93 for (final Submodule submodule : module.getSubmodules()) {
94 printReferences(submodule, true, indent + " ");
95 printChilds(submodule.getChildNodes(), indent + " ");
99 public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
101 for (final DataSchemaNode child : childNodes) {
102 LOG.debug("{}{} {}", indent, "Child", child.getQName().getLocalName());
103 if (child instanceof DataNodeContainer) {
104 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
109 public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
110 URISyntaxException, IOException, YangSyntaxErrorException {
111 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, null);
114 public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
115 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
116 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
119 public static EffectiveModelContext parseYangSource(final String yangSourcePath,
120 final YangParserConfiguration config, final Set<QName> supportedFeatures)
121 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
122 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
123 final File sourceFile = new File(source.toURI());
124 return parseYangSources(config, supportedFeatures, sourceFile);
127 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
128 throws ReactorException {
129 return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
132 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
133 final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
134 return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
137 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
138 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
139 throws ReactorException {
140 final BuildAction build = getReactor(config).newBuild().addSources(sources);
141 if (supportedFeatures != null) {
142 build.setSupportedFeatures(supportedFeatures);
144 return build.buildEffective();
147 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
148 YangSyntaxErrorException {
149 return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
152 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
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.forPath(file.toPath())));
161 return parseYangSources(config, supportedFeatures, sources);
164 public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
165 IOException, YangSyntaxErrorException {
166 return parseYangSources(files, YangParserConfiguration.DEFAULT);
169 public static EffectiveModelContext parseYangSources(final Collection<File> files,
170 final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
171 return parseYangSources(config, null, files.toArray(new File[0]));
174 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
175 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
176 return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
179 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
180 final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
181 YangSyntaxErrorException {
182 return parseYangSources(yangSourcesDirectoryPath, null, config);
185 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
186 final Set<QName> supportedFeatures, final YangParserConfiguration config) 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(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
195 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
196 throws URISyntaxException, SAXException, IOException, ReactorException {
197 return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
200 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
201 final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
203 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
204 final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
205 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
206 for (int i = 0; i < files.length; i++) {
207 final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
209 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
210 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
213 return parseYinSources(config, sources);
216 public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
217 final StatementStreamSource... sources) throws ReactorException {
218 return getReactor(config)
224 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
225 final String importedModuleName) {
226 ModuleImport requestedModuleImport = null;
227 for (final ModuleImport moduleImport : rootModule.getImports()) {
228 if (moduleImport.getModuleName().equals(importedModuleName)) {
229 requestedModuleImport = moduleImport;
234 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
238 private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
239 return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
240 : RFC7950Reactors.defaultReactorBuilder(config).build();