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.opendaylight.yangtools.yang.common.YangConstants;
21 import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
22 import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
23 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
24 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
29 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
30 import org.opendaylight.yangtools.yang.model.repo.api.YinTextSchemaSource;
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.BuildAction;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.xml.sax.SAXException;
43 public final class TestUtils {
44 private static final Logger LOG = LoggerFactory.getLogger(TestUtils.class);
49 public static EffectiveModelContext loadModules(final URI resourceDirectory)
50 throws ReactorException, IOException, YangSyntaxErrorException {
51 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
52 File[] files = new File(resourceDirectory).listFiles();
54 for (File file : files) {
55 if (file.getName().endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)) {
56 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forFile(file)));
58 LOG.info("Ignoring non-yang file {}", file);
62 return reactor.buildEffective();
65 public static EffectiveModelContext loadModuleResources(final Class<?> refClass, final String... resourceNames)
66 throws IOException, ReactorException, YangSyntaxErrorException {
67 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
69 for (String resourceName : resourceNames) {
70 reactor.addSource(YangStatementStreamSource.create(YangTextSchemaSource.forResource(refClass,
74 return reactor.buildEffective();
77 public static EffectiveModelContext loadYinModules(final URI resourceDirectory)
78 throws ReactorException, SAXException, IOException {
79 final BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
81 for (File file : new File(resourceDirectory).listFiles()) {
82 reactor.addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(
83 YinTextSchemaSource.forFile(file))));
86 return reactor.buildEffective();
89 public static Module loadYinModule(final YinTextSchemaSource source) throws ReactorException, SAXException,
91 return RFC7950Reactors.defaultReactor().newBuild()
92 .addSource(YinStatementStreamSource.create(YinTextToDomTransformer.transformSource(source)))
94 .getModules().iterator().next();
97 public static ModuleImport findImport(final Collection<? extends ModuleImport> imports, final String prefix) {
98 for (ModuleImport moduleImport : imports) {
99 if (moduleImport.getPrefix().equals(prefix)) {
106 public static TypeDefinition<?> findTypedef(final Collection<? extends TypeDefinition<?>> typedefs,
108 for (TypeDefinition<?> td : typedefs) {
109 if (td.getQName().getLocalName().equals(name)) {
117 * Test if node has augmenting flag set to expected value. In case this is
118 * DataNodeContainer/ChoiceNode, check its child nodes/case nodes too.
125 public static void checkIsAugmenting(final DataSchemaNode node, final boolean expected) {
126 assertEquals(expected, node.isAugmenting());
127 if (node instanceof DataNodeContainer) {
128 for (DataSchemaNode child : ((DataNodeContainer) node)
130 checkIsAugmenting(child, expected);
132 } else if (node instanceof ChoiceSchemaNode) {
133 for (CaseSchemaNode caseNode : ((ChoiceSchemaNode) node).getCases()) {
134 checkIsAugmenting(caseNode, expected);
139 public static List<Module> findModules(final Collection<? extends Module> modules, final String moduleName) {
140 List<Module> result = new ArrayList<>();
141 for (Module module : modules) {
142 if (module.getName().equals(moduleName)) {
149 public static EffectiveModelContext parseYangSources(final StatementStreamSource... sources)
150 throws ReactorException {
151 return RFC7950Reactors.defaultReactor().newBuild().addSources(sources).buildEffective();
154 public static EffectiveModelContext parseYangSources(final File... files)
155 throws ReactorException, IOException, YangSyntaxErrorException {
157 StatementStreamSource[] sources = new StatementStreamSource[files.length];
159 for (int i = 0; i < files.length; i++) {
160 sources[i] = YangStatementStreamSource.create(YangTextSchemaSource.forFile(files[i]));
163 return parseYangSources(sources);
166 public static EffectiveModelContext parseYangSources(final Collection<File> files)
167 throws ReactorException, IOException, YangSyntaxErrorException {
168 return parseYangSources(files.toArray(new File[files.size()]));
171 public static EffectiveModelContext parseYangSources(final String yangSourcesDirectoryPath)
172 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
174 URL resourceDir = StmtTestUtils.class.getResource(yangSourcesDirectoryPath);
175 File testSourcesDir = new File(resourceDir.toURI());
177 return parseYangSources(testSourcesDir.listFiles(
178 (dir, name) -> name.endsWith(YangConstants.RFC6020_YANG_FILE_EXTENSION)));
181 public static EffectiveModelContext parseYangSource(final String yangSourceFilePath)
182 throws ReactorException, URISyntaxException, IOException, YangSyntaxErrorException {
184 URL resourceFile = StmtTestUtils.class.getResource(yangSourceFilePath);
185 File testSourcesFile = new File(resourceFile.toURI());
187 return parseYangSources(testSourcesFile);