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.repo.api.SourceIdentifier;
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.api.YangParserConfiguration;
35 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
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;
43 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 import org.xml.sax.SAXException;
48 public final class StmtTestUtils {
50 public static final FileFilter YANG_FILE_FILTER =
51 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
53 public static final FileFilter YIN_FILE_FILTER =
54 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
56 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
58 private StmtTestUtils() {
62 public static void log(final Throwable exception, final String indent) {
63 LOG.debug("{}{}", indent, exception.getMessage());
65 final Throwable[] suppressed = exception.getSuppressed();
66 for (final Throwable throwable : suppressed) {
67 log(throwable, indent + " ");
71 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
72 final List<Module> result = new ArrayList<>();
73 for (final Module module : modules) {
74 if (module.getName().equals(moduleName)) {
81 public static YangStatementStreamSource sourceForResource(final String resourceName) {
83 return YangStatementStreamSource.create(YangTextSchemaSource.forFile(new File(
84 StmtTestUtils.class.getResource(resourceName).toURI())));
85 } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
86 throw new IllegalArgumentException("Failed to create source", e);
90 public static void printReferences(final ModuleLike module, final boolean isSubmodule, final String indent) {
91 LOG.debug("{}{} {}", indent, isSubmodule ? "Submodule" : "Module", module.getName());
92 for (final Submodule submodule : module.getSubmodules()) {
93 printReferences(submodule, true, indent + " ");
94 printChilds(submodule.getChildNodes(), indent + " ");
98 public static void printChilds(final Collection<? extends DataSchemaNode> childNodes, final String indent) {
100 for (final DataSchemaNode child : childNodes) {
101 LOG.debug("{}{} {}", indent, "Child", child.getQName().getLocalName());
102 if (child instanceof DataNodeContainer) {
103 printChilds(((DataNodeContainer) child).getChildNodes(), indent + " ");
108 public static EffectiveModelContext parseYangSource(final String yangSourcePath) throws ReactorException,
109 URISyntaxException, IOException, YangSyntaxErrorException {
110 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, null);
113 public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
114 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
115 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
118 public static EffectiveModelContext parseYangSource(final String yangSourcePath,
119 final YangParserConfiguration config, final Set<QName> supportedFeatures)
120 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
121 final URL source = StmtTestUtils.class.getResource(yangSourcePath);
122 final File sourceFile = new File(source.toURI());
123 return parseYangSources(config, supportedFeatures, sourceFile);
126 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
127 throws ReactorException {
128 return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
131 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
132 final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
133 return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
136 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
137 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
138 throws ReactorException {
139 final BuildAction build = getReactor(config).newBuild().addSources(sources);
140 if (supportedFeatures != null) {
141 build.setSupportedFeatures(supportedFeatures);
143 return build.buildEffective();
146 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
147 YangSyntaxErrorException {
148 return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
151 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
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(config, supportedFeatures, sources);
163 public static EffectiveModelContext parseYangSources(final Collection<File> files) throws ReactorException,
164 IOException, YangSyntaxErrorException {
165 return parseYangSources(files, YangParserConfiguration.DEFAULT);
168 public static EffectiveModelContext parseYangSources(final Collection<File> files,
169 final YangParserConfiguration config) throws ReactorException, IOException, YangSyntaxErrorException {
170 return parseYangSources(config, null, files.toArray(new File[files.size()]));
173 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
174 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
175 return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
178 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
179 final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
180 YangSyntaxErrorException {
181 return parseYangSources(yangSourcesDirectoryPath, null, config);
184 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
185 final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
186 URISyntaxException, IOException, YangSyntaxErrorException {
188 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
189 final File testSourcesDir = new File(resourceDir.toURI());
191 return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
194 public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
195 final String yangLibsDirectoryPath)
196 throws URISyntaxException, ReactorException, IOException, YangSyntaxErrorException {
197 return parseYangSources(yangFilesDirectoryPath, yangLibsDirectoryPath, null);
200 public static EffectiveModelContext parseYangSources(final String yangFilesDirectoryPath,
201 final String yangLibsDirectoryPath, final Set<QName> supportedFeatures) throws URISyntaxException,
202 ReactorException, IOException, YangSyntaxErrorException {
203 final File yangsDir = new File(StmtTestUtils.class.getResource(yangFilesDirectoryPath).toURI());
204 final File libsDir = new File(StmtTestUtils.class.getResource(yangLibsDirectoryPath).toURI());
206 return parseYangSources(yangsDir.listFiles(YANG_FILE_FILTER), libsDir.listFiles(YANG_FILE_FILTER),
210 private static EffectiveModelContext parseYangSources(final File[] yangFiles, final File[] libFiles,
211 final Set<QName> supportedFeatures) throws ReactorException, IOException, YangSyntaxErrorException {
212 final StatementStreamSource[] yangSources = new StatementStreamSource[yangFiles.length];
213 for (int i = 0; i < yangFiles.length; i++) {
214 yangSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(yangFiles[i]));
217 final StatementStreamSource[] libSources = new StatementStreamSource[libFiles.length];
218 for (int i = 0; i < libFiles.length; i++) {
219 libSources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(libFiles[i]));
222 return parseYangSources(yangSources, libSources, supportedFeatures);
225 private static EffectiveModelContext parseYangSources(final StatementStreamSource[] yangSources,
226 final StatementStreamSource[] libSources, final Set<QName> supportedFeatures) throws ReactorException {
228 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild()
229 .addSources(yangSources).addLibSources(libSources);
230 if (supportedFeatures != null) {
231 reactor.setSupportedFeatures(supportedFeatures);
234 return reactor.buildEffective();
237 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
238 throws URISyntaxException, SAXException, IOException, ReactorException {
239 return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
242 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
243 final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
245 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
246 final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
247 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
248 for (int i = 0; i < files.length; i++) {
249 final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
251 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
252 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
255 return parseYinSources(config, sources);
258 public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
259 final StatementStreamSource... sources) throws ReactorException {
260 return getReactor(config)
266 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
267 final String importedModuleName) {
268 ModuleImport requestedModuleImport = null;
269 for (final ModuleImport moduleImport : rootModule.getImports()) {
270 if (moduleImport.getModuleName().equals(importedModuleName)) {
271 requestedModuleImport = moduleImport;
276 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
280 private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
281 return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
282 : RFC7950Reactors.defaultReactorBuilder(config).build();