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 static org.junit.Assert.assertEquals;
13 import java.io.IOException;
15 import java.net.URISyntaxException;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.List;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.opendaylight.yangtools.yang.common.YangConstants;
22 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
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.TypeDefinition;
30 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
31 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
32 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
33 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
34 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
38 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
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 TestUtils {
45 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
51 public static @NonNull List<StatementStreamSource> loadSources(final String resourceDirectory)
53 return loadSources(TestUtils.class, resourceDirectory);
56 public static @NonNull List<StatementStreamSource> loadSources(final Class<?> cls, final String resourceDirectory)
58 final var files = new File(cls.getResource(resourceDirectory).toURI())
59 .listFiles(StmtTestUtils.YANG_FILE_FILTER);
60 final var sources = new ArrayList<StatementStreamSource>(files.length);
61 for (var file : files) {
62 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
67 public static EffectiveModelContext loadModules(final String resourceDirectory) throws Exception {
68 return loadModules(TestUtils.class, resourceDirectory);
71 public static EffectiveModelContext loadModules(final Class<?> cls, final String resourceDirectory)
73 return loadModules(cls.getResource(resourceDirectory).toURI());
76 public static EffectiveModelContext loadModules(final URI resourceDirectory)
77 throws ReactorException, IOException, YangSyntaxErrorException {
78 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
79 File[] files = new File(resourceDirectory).listFiles();
81 for (File file : files) {
82 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
83 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
85 LOG.info("Ignoring non-yang file {}", file);
89 return reactor.buildEffective();
92 public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
93 throws IOException, ReactorException, YangSyntaxErrorException {
94 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
96 for (String resourceName : resourceNames) {
97 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
101 return reactor.buildEffective();
104 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
105 throws ReactorException, SAXException, IOException {
106 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
108 for (File file : new File(resourceDirectory).listFiles()) {
109 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
110 YinTextSchemaSource.forPath(file.toPath()))));
113 return reactor.buildEffective();
116 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
118 return RFC7950Reactors.defaultReactor().newBuild()
119 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
121 .getModules().iterator().next();
124 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
125 for (ModuleImport moduleImport : imports) {
126 if (moduleImport.getPrefix().equals(prefix)) {
133 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
135 for (TypeDefinition<?> td : typedefs) {
136 if (td.getQName().getLocalName().equals(name)) {
144 * Test if node has augmenting flag set to expected value. In case this is
145 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
152 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
153 assertEquals(expected, node.isAugmenting());
154 if (node instanceof DataNodeContainer) {
155 for (DataSchemaNode child : ((DataNodeContainer) node)
157 checkIsAugmenting(child, expected);
159 } else if (node instanceof ChoiceSchemaNode) {
160 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
161 checkIsAugmenting(caseNode, expected);
166 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
167 List<Module> result = new ArrayList<>();
168 for (Module module : modules) {
169 if (module.getName().equals(moduleName)) {
176 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
177 throws ReactorException {
178 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
181 public static EffectiveModelContext parseYangSources(final File... files)
182 throws ReactorException, IOException, YangSyntaxErrorException {
184 StatementStreamSource[] sources = new StatementStreamSource[files.length];
186 for (int i = 0; i < files.length; i++) {
187 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
190 return parseYangSources(sources);
193 public static EffectiveModelContext parseYangSources(final Collection<File> files)
194 throws ReactorException, IOException, YangSyntaxErrorException {
195 return parseYangSources(files.toArray(new File[files.size()]));
198 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
199 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
201 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
202 File testSourcesDir = new File(resourceDir.toURI());
204 return parseYangSources(testSourcesDir.listFiles(
205 (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
208 public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
209 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
211 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
212 File testSourcesFile = new File(resourceFile.toURI());
214 return parseYangSources(testSourcesFile);