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;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
30 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
31 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
39 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.xml.sax.SAXException;
44 public final class StmtTestUtils {
46 public static final FileFilter YANG_FILE_FILTER =
47 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
49 public static final FileFilter YIN_FILE_FILTER =
50 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
52 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
54 private StmtTestUtils() {
58 public static void log(final Throwable exception, final String indent) {
59 LOG.debug("{}{}", indent, exception.getMessage());
61 final Throwable[] suppressed = exception.getSuppressed();
62 for (final Throwable throwable : suppressed) {
63 log(throwable, indent + " ");
67 public static YangStatementStreamSource sourceForResource(final String resourceName) {
69 return YangStatementStreamSource.create(YangTextSchemaSource.forPath(Path.of(
70 StmtTestUtils.class.getResource(resourceName).toURI())));
71 } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
72 throw new IllegalArgumentException("Failed to create source", e);
76 public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
77 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
78 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
81 public static EffectiveModelContext parseYangSource(final String yangSourcePath,
82 final YangParserConfiguration config, final Set<QName> supportedFeatures)
83 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
84 return parseYangSources(config, supportedFeatures,
85 new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
88 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
89 throws ReactorException {
90 return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
93 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
94 final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
95 return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
98 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
99 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
100 throws ReactorException {
101 final BuildAction build = getReactor(config).newBuild().addSources(sources);
102 if (supportedFeatures != null) {
103 build.setSupportedFeatures(supportedFeatures);
105 return build.buildEffective();
108 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
109 YangSyntaxErrorException {
110 return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
113 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
114 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
115 YangSyntaxErrorException {
117 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
118 for (File file : files) {
119 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
122 return parseYangSources(config, supportedFeatures, sources);
125 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
126 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
127 return parseYangSources(yangSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
130 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
131 final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
132 YangSyntaxErrorException {
133 return parseYangSources(yangSourcesDirectoryPath, null, config);
136 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
137 final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
138 URISyntaxException, IOException, YangSyntaxErrorException {
140 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
141 final File testSourcesDir = new File(resourceDir.toURI());
143 return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
146 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
147 throws URISyntaxException, SAXException, IOException, ReactorException {
148 return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
151 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
152 final YangParserConfiguration config) throws URISyntaxException, SAXException, IOException,
154 final URL resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
155 final File[] files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
156 final StatementStreamSource[] sources = new StatementStreamSource[files.length];
157 for (int i = 0; i < files.length; i++) {
158 final SourceIdentifier identifier = YinTextSchemaSource.identifierFromFilename(files[i].getName());
160 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
161 YinTextSchemaSource.delegateForByteSource(identifier, Files.asByteSource(files[i]))));
164 return parseYinSources(config, sources);
167 public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
168 final StatementStreamSource... sources) throws ReactorException {
169 return getReactor(config)
175 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
176 final String importedModuleName) {
177 ModuleImport requestedModuleImport = null;
178 for (final ModuleImport moduleImport : rootModule.getImports()) {
179 if (moduleImport.getModuleName().equals(importedModuleName)) {
180 requestedModuleImport = moduleImport;
185 return context.findModule(requestedModuleImport.getModuleName(), requestedModuleImport.getRevision())
189 private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
190 return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
191 : RFC7950Reactors.defaultReactorBuilder(config).build();