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 yangFilesDirectoryPath)
53 final var files = new File(TestUtils.class.getResource(yangFilesDirectoryPath).toURI())
54 .listFiles(StmtTestUtils.YANG_FILE_FILTER);
55 final var sources = new ArrayList<StatementStreamSource>(files.length);
56 for (var file : files) {
57 sources.add(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
62 public static EffectiveModelContext loadModules(final URI resourceDirectory)
63 throws ReactorException, IOException, YangSyntaxErrorException {
64 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
65 File[] files = new File(resourceDirectory).listFiles();
67 for (File file : files) {
68 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
69 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forPath(file.toPath())));
71 LOG.info("Ignoring non-yang file {}", file);
75 return reactor.buildEffective();
78 public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
79 throws IOException, ReactorException, YangSyntaxErrorException {
80 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
82 for (String resourceName : resourceNames) {
83 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
87 return reactor.buildEffective();
90 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
91 throws ReactorException, SAXException, IOException {
92 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
94 for (File file : new File(resourceDirectory).listFiles()) {
95 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
96 YinTextSchemaSource.forPath(file.toPath()))));
99 return reactor.buildEffective();
102 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
104 return RFC7950Reactors.defaultReactor().newBuild()
105 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
107 .getModules().iterator().next();
110 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
111 for (ModuleImport moduleImport : imports) {
112 if (moduleImport.getPrefix().equals(prefix)) {
119 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
121 for (TypeDefinition<?> td : typedefs) {
122 if (td.getQName().getLocalName().equals(name)) {
130 * Test if node has augmenting flag set to expected value. In case this is
131 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
138 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
139 assertEquals(expected, node.isAugmenting());
140 if (node instanceof DataNodeContainer) {
141 for (DataSchemaNode child : ((DataNodeContainer) node)
143 checkIsAugmenting(child, expected);
145 } else if (node instanceof ChoiceSchemaNode) {
146 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
147 checkIsAugmenting(caseNode, expected);
152 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
153 List<Module> result = new ArrayList<>();
154 for (Module module : modules) {
155 if (module.getName().equals(moduleName)) {
162 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
163 throws ReactorException {
164 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
167 public static EffectiveModelContext parseYangSources(final File... files)
168 throws ReactorException, IOException, YangSyntaxErrorException {
170 StatementStreamSource[] sources = new StatementStreamSource[files.length];
172 for (int i = 0; i < files.length; i++) {
173 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forPath(files[i].toPath()));
176 return parseYangSources(sources);
179 public static EffectiveModelContext parseYangSources(final Collection<File> files)
180 throws ReactorException, IOException, YangSyntaxErrorException {
181 return parseYangSources(files.toArray(new File[files.size()]));
184 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
185 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
187 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
188 File testSourcesDir = new File(resourceDir.toURI());
190 return parseYangSources(testSourcesDir.listFiles(
191 (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
194 public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
195 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
197 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
198 File testSourcesFile = new File(resourceFile.toURI());
200 return parseYangSources(testSourcesFile);