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;
11 import java.io.FileFilter;
12 import java.io.IOException;
13 import java.net.URISyntaxException;
15 import java.nio.file.Path;
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.Collection;
20 import org.opendaylight.yangtools.yang.common.QName;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.stmt.FeatureSet;
27 import org.opendaylight.yangtools.yang.model.spi.source.FileYinTextSource;
28 import org.opendaylight.yangtools.yang.model.spi.source.YangTextSource;
29 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
30 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
32 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
36 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
38 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.xml.sax.SAXException;
43 public final class StmtTestUtils {
45 public static final FileFilter YANG_FILE_FILTER =
46 file -> file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION) && file.isFile();
48 public static final FileFilter YIN_FILE_FILTER =
49 file -> file.getName().endsWith(YangConstants.RFC6020_YIN_FILE_EXTENSION) && file.isFile();
51 private static final Logger LOG = LoggerFactory.getLogger(StmtTestUtils.class);
53 private StmtTestUtils() {
57 public static void log(final Throwable exception, final String indent) {
58 LOG.debug("{}{}", indent, exception.getMessage());
60 final Throwable[] suppressed = exception.getSuppressed();
61 for (final Throwable throwable : suppressed) {
62 log(throwable, indent + " ");
66 public static YangStatementStreamSource sourceForResource(final String resourceName) {
68 return YangStatementStreamSource.create(YangTextSource.forPath(Path.of(
69 StmtTestUtils.class.getResource(resourceName).toURI())));
70 } catch (IOException | YangSyntaxErrorException | URISyntaxException e) {
71 throw new IllegalArgumentException("Failed to create source", e);
75 public static EffectiveModelContext parseYangSource(final String yangSourcePath, final Set<QName> supportedFeatures)
76 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
77 return parseYangSource(yangSourcePath, YangParserConfiguration.DEFAULT, supportedFeatures);
80 public static EffectiveModelContext parseYangSource(final String yangSourcePath,
81 final YangParserConfiguration config, final Set<QName> supportedFeatures)
82 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
83 return parseYangSources(config, supportedFeatures,
84 new File(StmtTestUtils.class.getResource(yangSourcePath).toURI()));
87 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
88 throws ReactorException {
89 return parseYangSources(YangParserConfiguration.DEFAULT, null, sources);
92 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
93 final Set<QName> supportedFeatures, final StatementStreamSource... sources) throws ReactorException {
94 return parseYangSources(config, supportedFeatures, Arrays.asList(sources));
97 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
98 final Set<QName> supportedFeatures, final Collection<? extends StatementStreamSource> sources)
99 throws ReactorException {
100 final BuildAction build = getReactor(config).newBuild().addSources(sources);
101 if (supportedFeatures != null) {
102 build.setSupportedFeatures(FeatureSet.of(supportedFeatures));
104 return build.buildEffective();
107 public static EffectiveModelContext parseYangSources(final File... files) throws ReactorException, IOException,
108 YangSyntaxErrorException {
109 return parseYangSources(YangParserConfiguration.DEFAULT, null, files);
112 public static EffectiveModelContext parseYangSources(final YangParserConfiguration config,
113 final Set<QName> supportedFeatures, final File... files) throws ReactorException, IOException,
114 YangSyntaxErrorException {
116 final Collection<YangStatementStreamSource> sources = new ArrayList<>(files.length);
117 for (File file : files) {
118 sources.add(YangStatementStreamSource.create(YangTextSource.forPath(file.toPath())));
121 return parseYangSources(config, supportedFeatures, sources);
124 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
125 final YangParserConfiguration config) throws ReactorException, URISyntaxException, IOException,
126 YangSyntaxErrorException {
127 return parseYangSources(yangSourcesDirectoryPath, null, config);
130 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath,
131 final Set<QName> supportedFeatures, final YangParserConfiguration config) throws ReactorException,
132 URISyntaxException, IOException, YangSyntaxErrorException {
134 final URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
135 final File testSourcesDir = new File(resourceDir.toURI());
137 return parseYangSources(config, supportedFeatures, testSourcesDir.listFiles(YANG_FILE_FILTER));
140 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath)
141 throws URISyntaxException, SAXException, IOException, ReactorException {
142 return parseYinSources(yinSourcesDirectoryPath, YangParserConfiguration.DEFAULT);
145 public static EffectiveModelContext parseYinSources(final String yinSourcesDirectoryPath,
146 final YangParserConfiguration config)
147 throws URISyntaxException, SAXException, IOException, ReactorException {
148 final var resourceDir = StmtTestUtils.class.getResource(yinSourcesDirectoryPath);
149 final var files = new File(resourceDir.toURI()).listFiles(YIN_FILE_FILTER);
150 final var sources = new StatementStreamSource[files.length];
151 for (int i = 0; i < files.length; i++) {
152 sources[i] = YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
153 new FileYinTextSource(files[i].toPath())));
156 return parseYinSources(config, sources);
159 public static EffectiveModelContext parseYinSources(final YangParserConfiguration config,
160 final StatementStreamSource... sources) throws ReactorException {
161 return getReactor(config)
167 public static Module findImportedModule(final SchemaContext context, final Module rootModule,
168 final String importedModuleName) {
169 ModuleImport requestedModuleImport = null;
170 for (final ModuleImport moduleImport : rootModule.getImports()) {
171 if (moduleImport.getModuleName().equals(importedModuleName)) {
172 requestedModuleImport = moduleImport;
177 return context.findModule(requestedModuleImport.getModuleName().getLocalName(),
178 requestedModuleImport.getRevision())
182 private static CrossSourceStatementReactor getReactor(final YangParserConfiguration config) {
183 return YangParserConfiguration.DEFAULT.equals(config) ? RFC7950Reactors.defaultReactor()
184 : RFC7950Reactors.defaultReactorBuilder(config).build();