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 java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.yangtools.yang.common.YangConstants;
23 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
24 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
26 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
27 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
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.YangSyntaxErrorException;
35 import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
36 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
37 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinStatementStreamSource;
38 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YinTextToDomTransformer;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
40 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
41 import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor.BuildAction;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.xml.sax.SAXException;
46 public final class TestUtils {
47 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
52 public static EffectiveModelContext loadModules(final URI resourceDirectory)
53 throws ReactorException, IOException, YangSyntaxErrorException {
54 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
55 File[] files = new File(resourceDirectory).listFiles();
57 for (File file : files) {
58 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
59 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
61 LOG.info("Ignoring non-yang file {}", file);
65 return reactor.buildEffective();
68 public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
69 throws IOException, ReactorException, YangSyntaxErrorException {
70 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
72 for (String resourceName : resourceNames) {
73 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
77 return reactor.buildEffective();
80 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
81 throws ReactorException, SAXException, IOException {
82 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
84 for (File file : new File(resourceDirectory).listFiles()) {
85 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
86 YinTextSchemaSource.forFile(file))));
89 return reactor.buildEffective();
92 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
94 return RFC7950Reactors.defaultReactor().newBuild()
95 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
97 .getModules().iterator().next();
100 @Deprecated(forRemoval = true)
101 public static Optional<? extends @NonNull Module> findModule(final SchemaContext context, final String moduleName) {
102 return context.getModules().stream().filter(module -> moduleName.equals(module.getName())).findAny();
105 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
106 for (ModuleImport moduleImport : imports) {
107 if (moduleImport.getPrefix().equals(prefix)) {
114 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
116 for (TypeDefinition<?> td : typedefs) {
117 if (td.getQName().getLocalName().equals(name)) {
125 * Test if node has augmenting flag set to expected value. In case this is
126 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
133 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
134 assertEquals(expected, node.isAugmenting());
135 if (node instanceof DataNodeContainer) {
136 for (DataSchemaNode child : ((DataNodeContainer) node)
138 checkIsAugmenting(child, expected);
140 } else if (node instanceof ChoiceSchemaNode) {
141 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
142 checkIsAugmenting(caseNode, expected);
147 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
148 List<Module> result = new ArrayList<>();
149 for (Module module : modules) {
150 if (module.getName().equals(moduleName)) {
157 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
158 throws ReactorException {
159 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
162 public static EffectiveModelContext parseYangSources(final File... files)
163 throws ReactorException, IOException, YangSyntaxErrorException {
165 StatementStreamSource[] sources = new StatementStreamSource[files.length];
167 for (int i = 0; i < files.length; i++) {
168 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
171 return parseYangSources(sources);
174 public static EffectiveModelContext parseYangSources(final Collection<File> files)
175 throws ReactorException, IOException, YangSyntaxErrorException {
176 return parseYangSources(files.toArray(new File[files.size()]));
179 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
180 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
182 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
183 File testSourcesDir = new File(resourceDir.toURI());
185 return parseYangSources(testSourcesDir.listFiles(
186 (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
189 public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
190 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
192 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
193 File testSourcesFile = new File(resourceFile.toURI());
195 return parseYangSources(testSourcesFile);